Skip to content

Commit bb8b16f

Browse files
authored
Merge pull request #549 from asgrim/542-improve-architecture-parsing
542: improve architecture parsing and more test cases
2 parents 4fa1169 + ec58093 commit bb8b16f

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,6 @@ parameters:
312312
count: 1
313313
path: src/Platform/TargetPhp/PhpizePath.php
314314

315-
-
316-
message: '#^Call to function array_key_exists\(\) with 2 and array\{non\-falsy\-string, string, non\-falsy\-string\} will always evaluate to true\.$#'
317-
identifier: function.alreadyNarrowedType
318-
count: 1
319-
path: src/Platform/TargetPlatform.php
320-
321-
-
322-
message: '#^Strict comparison using \!\=\= between non\-falsy\-string and '''' will always evaluate to true\.$#'
323-
identifier: notIdentical.alwaysTrue
324-
count: 1
325-
path: src/Platform/TargetPlatform.php
326-
327315
-
328316
message: '#^Dead catch \- Php\\Pie\\SelfManage\\Verify\\GithubCliNotAvailable is never thrown in the try block\.$#'
329317
identifier: catch.neverThrown

src/Platform/TargetPhp/PhpBinaryPath.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,35 @@ public function majorMinorVersion(): string
327327

328328
public function machineType(): Architecture
329329
{
330+
/**
331+
* On Windows, this will be x32 or x64; should not be used on other platforms
332+
*
333+
* Based on xdebug.org wizard, copyright Derick Rethans, used under MIT licence
334+
*
335+
* @link https://github.com/xdebug/xdebug.org/blob/aff649f2c3ca303ad471e6ed9dd29c0db16d3e22/src/XdebugVersion.php#L186-L190
336+
*/
337+
if (
338+
$this->operatingSystem() === OperatingSystem::Windows
339+
&& preg_match('/Architecture([ =>\t]*)(x[0-9]*)/', $this->phpinfo(), $m)
340+
) {
341+
return Architecture::parseArchitecture($m[2]);
342+
}
343+
330344
$phpMachineType = self::cleanWarningAndDeprecationsFromOutput(Process::run([
331345
$this->phpBinaryPath,
332346
'-r',
333347
'echo php_uname("m");',
334348
]));
335349
Assert::stringNotEmpty($phpMachineType, 'Could not determine PHP machine type');
336350

337-
return Architecture::parseArchitecture($phpMachineType);
351+
$unameArchitecture = Architecture::parseArchitecture($phpMachineType);
352+
353+
// If we're not on ARM, a more reliable way of determining 32-bit/64-bit is to use PHP_INT_SIZE
354+
if ($unameArchitecture !== Architecture::arm64) {
355+
return $this->phpIntSize() === 4 ? Architecture::x86 : Architecture::x86_64;
356+
}
357+
358+
return $unameArchitecture;
338359
}
339360

340361
public function phpIntSize(): int

src/Platform/TargetPlatform.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Fidry\CpuCoreCounter\CpuCoreCounter;
88
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
99

10-
use function array_key_exists;
1110
use function explode;
1211
use function function_exists;
1312
use function posix_getuid;
@@ -39,31 +38,10 @@ public static function isRunningAsRoot(): bool
3938

4039
public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null $makeParallelJobs): self
4140
{
42-
$os = $phpBinaryPath->operatingSystem();
43-
$osFamily = $phpBinaryPath->operatingSystemFamily();
44-
45-
$phpinfo = $phpBinaryPath->phpinfo();
46-
47-
$architecture = $phpBinaryPath->machineType();
48-
49-
// If we're not on ARM, a more reliable way of determining 32-bit/64-bit is to use PHP_INT_SIZE
50-
if ($architecture !== Architecture::arm64) {
51-
$architecture = $phpBinaryPath->phpIntSize() === 4 ? Architecture::x86 : Architecture::x86_64;
52-
}
53-
54-
/**
55-
* Based on xdebug.org wizard, copyright Derick Rethans, used under MIT licence
56-
*
57-
* @link https://github.com/xdebug/xdebug.org/blob/aff649f2c3ca303ad471e6ed9dd29c0db16d3e22/src/XdebugVersion.php#L186-L190
58-
*/
59-
if (
60-
preg_match('/Architecture([ =>\t]*)(x[0-9]*)/', $phpinfo, $m)
61-
&& array_key_exists(2, $m)
62-
&& $m[2] !== ''
63-
) {
64-
$architecture = Architecture::parseArchitecture($m[2]);
65-
}
66-
41+
$os = $phpBinaryPath->operatingSystem();
42+
$osFamily = $phpBinaryPath->operatingSystemFamily();
43+
$phpinfo = $phpBinaryPath->phpinfo();
44+
$architecture = $phpBinaryPath->machineType();
6745
$windowsCompiler = null;
6846
$threadSafety = ThreadSafetyMode::ThreadSafe;
6947

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@
2929
use function array_map;
3030
use function array_unique;
3131
use function assert;
32+
use function chmod;
3233
use function count;
3334
use function defined;
3435
use function dirname;
3536
use function file_exists;
37+
use function file_put_contents;
3638
use function get_loaded_extensions;
3739
use function ini_get;
3840
use function is_dir;
3941
use function is_executable;
4042
use function mkdir;
41-
use function php_uname;
4243
use function phpversion;
4344
use function sprintf;
4445
use function strtolower;
4546
use function sys_get_temp_dir;
47+
use function tempnam;
4648
use function trim;
4749
use function uniqid;
50+
use function unlink;
4851

4952
use const DIRECTORY_SEPARATOR;
5053
use const PHP_INT_SIZE;
@@ -240,15 +243,46 @@ public function testMajorMinorVersion(): void
240243
);
241244
}
242245

243-
public function testMachineType(): void
246+
/** @return list<array{0: OperatingSystem, 1: string, 2: string, 3: int, 4: Architecture}> */
247+
public static function machineTypeProvider(): array
244248
{
245-
$myUnameMachineType = php_uname('m');
246-
assert($myUnameMachineType !== '');
247-
self::assertSame(
248-
Architecture::parseArchitecture($myUnameMachineType),
249-
PhpBinaryPath::fromCurrentProcess()
250-
->machineType(),
251-
);
249+
return [
250+
// x86 (32-bit)
251+
[OperatingSystem::Windows, 'Architecture => x32', '', 4, Architecture::x86],
252+
[OperatingSystem::NonWindows, 'Architecture => x86', 'x86', 4, Architecture::x86],
253+
[OperatingSystem::NonWindows, '', 'x86', 4, Architecture::x86],
254+
255+
// x86_64 (64-bit)
256+
[OperatingSystem::Windows, 'Architecture => x64', 'AMD64', 8, Architecture::x86_64],
257+
[OperatingSystem::Windows, 'Architecture => x64', '', 8, Architecture::x86_64],
258+
[OperatingSystem::NonWindows, 'Architecture => x86_64', 'x86_64', 8, Architecture::x86_64],
259+
[OperatingSystem::NonWindows, '', 'x86_64', 8, Architecture::x86_64],
260+
261+
// arm64
262+
[OperatingSystem::NonWindows, 'Architecture => arm64', 'arm64', 8, Architecture::arm64],
263+
[OperatingSystem::NonWindows, '', 'arm64', 8, Architecture::arm64],
264+
[OperatingSystem::NonWindows, 'Architecture => aarch64', 'aarch64', 8, Architecture::arm64],
265+
[OperatingSystem::NonWindows, '', 'aarch64', 8, Architecture::arm64],
266+
];
267+
}
268+
269+
#[RequiresOperatingSystemFamily('Linux')]
270+
#[DataProvider('machineTypeProvider')]
271+
public function testMachineType(OperatingSystem $os, string $phpinfo, string $uname, int $phpIntSize, Architecture $expectedArchitecture): void
272+
{
273+
$tmpSh = tempnam(sys_get_temp_dir(), uniqid('pie_machine_type_test'));
274+
file_put_contents($tmpSh, "#!/usr/bin/env bash\necho \"" . $uname . "\";\n");
275+
chmod($tmpSh, 0777);
276+
277+
$phpBinary = $this->createPartialMock(PhpBinaryPath::class, ['operatingSystem', 'phpinfo', 'phpIntSize']);
278+
(new ReflectionMethod($phpBinary, '__construct'))->invoke($phpBinary, $tmpSh, null);
279+
280+
$phpBinary->method('operatingSystem')->willReturn($os);
281+
$phpBinary->method('phpinfo')->willReturn($phpinfo);
282+
$phpBinary->method('phpIntSize')->willReturn($phpIntSize);
283+
284+
self::assertEquals($expectedArchitecture, $phpBinary->machineType());
285+
unlink($tmpSh);
252286
}
253287

254288
public function testPhpIntSize(): void

test/unit/Platform/TargetPlatformTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testWindowsPlatformFromPhpInfo(): void
2828
->willReturn(OperatingSystemFamily::Windows);
2929
$phpBinaryPath->expects(self::any())
3030
->method('machineType')
31-
->willReturn(Architecture::x86);
31+
->willReturn(Architecture::x86_64);
3232
$phpBinaryPath->expects(self::any())
3333
->method('phpinfo')
3434
->willReturn(<<<'TEXT'

test/unit/SelfManage/Update/ReleaseIsNewerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
final class ReleaseIsNewerTest extends TestCase
1616
{
1717
/** @return array<non-empty-string, array{0: Channel, 1: non-empty-string, 2: non-empty-string, 3: bool}> */
18-
public function provider(): array
18+
public static function provider(): array
1919
{
2020
return [
2121
'stable-oldstable-to-newstable' => [Channel::Stable, '1.0.0', '1.0.1', true],

0 commit comments

Comments
 (0)