Skip to content

Commit b46ed9d

Browse files
authored
Merge pull request #550 from asgrim/merge-up-1.4.x
Merge up 1.4.x - fixes for architecture parsing and PHP 8.5 warnings
2 parents 3137195 + dac084b commit b46ed9d

8 files changed

Lines changed: 91 additions & 50 deletions

File tree

box.json.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"alias": "pie.phar",
33
"output": "pie.phar",
44
"git": "pie_version",
5+
"stub": "phar-stub.php",
56
"force-autodiscovery": true,
67
"directories": [
78
"resources"

phar-stub.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
// echo "James says hello :)\n";
7+
8+
// Workaround for https://github.com/php/pie/issues/537 and https://github.com/box-project/box/issues/1577
9+
error_reporting(error_reporting() & ~E_DEPRECATED);
10+
11+
Phar::mapPhar('pie.phar');
12+
13+
require 'phar://pie.phar/.box/bin/check-requirements.php';
14+
15+
$_SERVER['SCRIPT_FILENAME'] = 'phar://pie.phar/bin/pie';
16+
require 'phar://pie.phar/bin/pie';
17+
18+
// phpcs:ignore Generic.Files.InlineHTML.Found
19+
__HALT_COMPILER(); ?>

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,6 @@ parameters:
300300
count: 4
301301
path: src/Platform/TargetPhp/PhpBinaryPath.php
302302

303-
-
304-
message: '#^Call to function array_key_exists\(\) with 2 and array\{non\-falsy\-string, string, non\-falsy\-string\} will always evaluate to true\.$#'
305-
identifier: function.alreadyNarrowedType
306-
count: 1
307-
path: src/Platform/TargetPlatform.php
308-
309-
-
310-
message: '#^Strict comparison using \!\=\= between non\-falsy\-string and '''' will always evaluate to true\.$#'
311-
identifier: notIdentical.alwaysTrue
312-
count: 1
313-
path: src/Platform/TargetPlatform.php
314-
315303
-
316304
message: '#^Dead catch \- Php\\Pie\\SelfManage\\Verify\\GithubCliNotAvailable is never thrown in the try block\.$#'
317305
identifier: catch.neverThrown

src/Platform/TargetPhp/PhpBinaryPath.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,35 @@ public function minorVersion(): int
385385

386386
public function machineType(): Architecture
387387
{
388+
/**
389+
* On Windows, this will be x32 or x64; should not be used on other platforms
390+
*
391+
* Based on xdebug.org wizard, copyright Derick Rethans, used under MIT licence
392+
*
393+
* @link https://github.com/xdebug/xdebug.org/blob/aff649f2c3ca303ad471e6ed9dd29c0db16d3e22/src/XdebugVersion.php#L186-L190
394+
*/
395+
if (
396+
$this->operatingSystem() === OperatingSystem::Windows
397+
&& preg_match('/Architecture([ =>\t]*)(x[0-9]*)/', $this->phpinfo(), $m)
398+
) {
399+
return Architecture::parseArchitecture($m[2]);
400+
}
401+
388402
$phpMachineType = self::cleanWarningAndDeprecationsFromOutput(Process::run([
389403
$this->phpBinaryPath,
390404
'-r',
391405
'echo php_uname("m");',
392406
]));
393407
Assert::stringNotEmpty($phpMachineType, 'Could not determine PHP machine type');
394408

395-
return Architecture::parseArchitecture($phpMachineType);
409+
$unameArchitecture = Architecture::parseArchitecture($phpMachineType);
410+
411+
// If we're not on ARM, a more reliable way of determining 32-bit/64-bit is to use PHP_INT_SIZE
412+
if ($unameArchitecture !== Architecture::arm64) {
413+
return $this->phpIntSize() === 4 ? Architecture::x86 : Architecture::x86_64;
414+
}
415+
416+
return $unameArchitecture;
396417
}
397418

398419
public function phpIntSize(): int

src/Platform/TargetPlatform.php

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

11-
use function array_key_exists;
1211
use function explode;
1312
use function function_exists;
1413
use function posix_getuid;
@@ -52,31 +51,10 @@ public static function isRunningAsRoot(): bool
5251

5352
public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null $makeParallelJobs, PhpizePath|null $phpizePath): self
5453
{
55-
$os = $phpBinaryPath->operatingSystem();
56-
$osFamily = $phpBinaryPath->operatingSystemFamily();
57-
58-
$phpinfo = $phpBinaryPath->phpinfo();
59-
60-
$architecture = $phpBinaryPath->machineType();
61-
62-
// If we're not on ARM, a more reliable way of determining 32-bit/64-bit is to use PHP_INT_SIZE
63-
if ($architecture !== Architecture::arm64) {
64-
$architecture = $phpBinaryPath->phpIntSize() === 4 ? Architecture::x86 : Architecture::x86_64;
65-
}
66-
67-
/**
68-
* Based on xdebug.org wizard, copyright Derick Rethans, used under MIT licence
69-
*
70-
* @link https://github.com/xdebug/xdebug.org/blob/aff649f2c3ca303ad471e6ed9dd29c0db16d3e22/src/XdebugVersion.php#L186-L190
71-
*/
72-
if (
73-
preg_match('/Architecture([ =>\t]*)(x[0-9]*)/', $phpinfo, $m)
74-
&& array_key_exists(2, $m)
75-
&& $m[2] !== ''
76-
) {
77-
$architecture = Architecture::parseArchitecture($m[2]);
78-
}
79-
54+
$os = $phpBinaryPath->operatingSystem();
55+
$osFamily = $phpBinaryPath->operatingSystemFamily();
56+
$phpinfo = $phpBinaryPath->phpinfo();
57+
$architecture = $phpBinaryPath->machineType();
8058
$windowsCompiler = null;
8159
$threadSafety = ThreadSafetyMode::ThreadSafe;
8260

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,25 @@
3030
use function array_map;
3131
use function array_unique;
3232
use function assert;
33+
use function chmod;
3334
use function count;
3435
use function defined;
3536
use function dirname;
3637
use function file_exists;
38+
use function file_put_contents;
3739
use function get_loaded_extensions;
3840
use function ini_get;
3941
use function is_dir;
4042
use function is_executable;
4143
use function mkdir;
42-
use function php_uname;
4344
use function phpversion;
4445
use function sprintf;
4546
use function strtolower;
4647
use function sys_get_temp_dir;
48+
use function tempnam;
4749
use function trim;
4850
use function uniqid;
51+
use function unlink;
4952

5053
use const DIRECTORY_SEPARATOR;
5154
use const PHP_INT_SIZE;
@@ -241,15 +244,46 @@ public function testMajorMinorVersion(): void
241244
);
242245
}
243246

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

255289
public function testPhpIntSize(): void

test/unit/Platform/TargetPlatformTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testWindowsPlatformFromPhpInfo(): void
2929
->willReturn(OperatingSystemFamily::Windows);
3030
$phpBinaryPath->expects(self::any())
3131
->method('machineType')
32-
->willReturn(Architecture::x86);
32+
->willReturn(Architecture::x86_64);
3333
$phpBinaryPath->expects(self::any())
3434
->method('phpinfo')
3535
->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)