From 7162e1b958a4079f1b9d28d7dd49c8f6a43f9950 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 18 Dec 2024 15:53:00 +0100 Subject: [PATCH] Trim warnings and deprecations from output when invoking PHP in `PhpBinaryPath` --- src/Platform/TargetPhp/PhpBinaryPath.php | 50 +++++++++++++------ .../Platform/TargetPhp/PhpBinaryPathTest.php | 5 ++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/Platform/TargetPhp/PhpBinaryPath.php b/src/Platform/TargetPhp/PhpBinaryPath.php index d9b1e02..f7d7154 100644 --- a/src/Platform/TargetPhp/PhpBinaryPath.php +++ b/src/Platform/TargetPhp/PhpBinaryPath.php @@ -20,6 +20,7 @@ use function dirname; use function explode; use function file_exists; +use function implode; use function is_dir; use function is_executable; use function preg_match; @@ -58,7 +59,7 @@ private static function assertValidLookingPhpBinary(string $phpBinaryPath): void // This is somewhat of a rudimentary check that the target PHP really is a PHP instance; not sure why you // WOULDN'T want to use a real PHP, but this should stop obvious hiccups at least (rather than for security) - $testOutput = Process::run([$phpBinaryPath, '-r', 'echo "PHP";']); + $testOutput = self::cleanWarningAndDeprecationsFromOutput(Process::run([$phpBinaryPath, '-r', 'echo "PHP";'])); if ($testOutput !== 'PHP') { throw Exception\InvalidPhpBinaryPath::fromInvalidPhpBinary($phpBinaryPath); @@ -116,7 +117,7 @@ public function extensionPath(): string */ public function extensions(): array { - $extVersionsList = Process::run([ + $extVersionsList = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', <<<'PHP' @@ -139,7 +140,7 @@ static function ($k, $v) { $extVersions )); PHP, - ]); + ])); $pairs = array_map( static fn (string $row) => explode(':', $row), @@ -154,11 +155,11 @@ static function ($k, $v) { public function operatingSystem(): OperatingSystem { - $winOrNot = Process::run([ + $winOrNot = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', 'echo \\defined(\'PHP_WINDOWS_VERSION_BUILD\') ? \'win\' : \'not\';', - ]); + ])); Assert::stringNotEmpty($winOrNot, 'Could not determine PHP version'); return $winOrNot === 'win' ? OperatingSystem::Windows : OperatingSystem::NonWindows; @@ -167,11 +168,11 @@ public function operatingSystem(): OperatingSystem /** @return non-empty-string */ public function version(): string { - $phpVersion = Process::run([ + $phpVersion = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION . "." . PHP_RELEASE_VERSION;', - ]); + ])); Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version'); // normalizing the version will throw an exception if it is not a valid version @@ -183,11 +184,11 @@ public function version(): string /** @return non-empty-string */ public function majorMinorVersion(): string { - $phpVersion = Process::run([ + $phpVersion = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;', - ]); + ])); Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version'); // normalizing the version will throw an exception if it is not a valid version @@ -198,11 +199,11 @@ public function majorMinorVersion(): string public function machineType(): Architecture { - $phpMachineType = Process::run([ + $phpMachineType = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', 'echo php_uname("m");', - ]); + ])); Assert::stringNotEmpty($phpMachineType, 'Could not determine PHP machine type'); return Architecture::parseArchitecture($phpMachineType); @@ -210,11 +211,11 @@ public function machineType(): Architecture public function phpIntSize(): int { - $phpIntSize = Process::run([ + $phpIntSize = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-r', 'echo PHP_INT_SIZE;', - ]); + ])); Assert::stringNotEmpty($phpIntSize, 'Could not fetch PHP_INT_SIZE'); Assert::same($phpIntSize, (string) (int) $phpIntSize, 'PHP_INT_SIZE was not an integer processed %2$s from %s'); @@ -224,10 +225,12 @@ public function phpIntSize(): int /** @return non-empty-string */ public function phpinfo(): string { - $phpInfo = Process::run([ + $phpInfo = self::cleanWarningAndDeprecationsFromOutput(Process::run([ $this->phpBinaryPath, '-i', - ]); + ])); + + var_dump(json_encode($phpInfo)); Assert::stringNotEmpty($phpInfo, sprintf('Could not run phpinfo using %s', $this->phpBinaryPath)); @@ -248,7 +251,7 @@ public function phpConfigPath(): string|null /** @param non-empty-string $phpConfig */ public static function fromPhpConfigExecutable(string $phpConfig): self { - $phpExecutable = Process::run([$phpConfig, '--php-binary']); + $phpExecutable = self::cleanWarningAndDeprecationsFromOutput(Process::run([$phpConfig, '--php-binary'])); Assert::stringNotEmpty($phpExecutable, 'Could not find path to PHP executable.'); self::assertValidLookingPhpBinary($phpExecutable); @@ -273,4 +276,19 @@ public static function fromCurrentProcess(): self return new self($phpExecutable, null); } + + private static function cleanWarningAndDeprecationsFromOutput(string $testOutput): string + { + $testOutput = explode("\n", $testOutput); + + foreach ($testOutput as $key => $line) { + if (! preg_match('/^(Deprecated|Warning):/', $line)) { + continue; + } + + unset($testOutput[$key]); + } + + return implode("\n", $testOutput); + } } diff --git a/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php b/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php index 01cd1da..ca3d1f9 100644 --- a/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php +++ b/test/unit/Platform/TargetPhp/PhpBinaryPathTest.php @@ -164,6 +164,11 @@ public function testOperatingSystem(): void ); } + public function testOperatingSystemWithInvalidConfiguration(): void + { + $this->fail(); + } + public function testMajorMinorVersion(): void { self::assertSame(