From 1e10a1e974a831b64dab801d09dffa6acd43bd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sat, 4 Nov 2023 18:51:11 +0100 Subject: [PATCH] feat: Update to PHP-Scoper 0.18.5 (#1142) --- composer.json | 2 +- composer.lock | 16 +- src/Box.php | 1 + src/Composer/AutoloadDumper.php | 68 ++- src/Composer/ComposerOrchestrator.php | 2 + src/Console/Command/Compile.php | 3 +- src/PhpScoper/NullScoper.php | 5 + src/PhpScoper/SerializableScoper.php | 7 +- tests/Composer/AutoloadDumperTest.php | 437 ++++++++++++++++++ ...ComposerOrchestratorComposer22TestCase.php | 155 ++++--- ...ComposerOrchestratorComposer23TestCase.php | 187 ++++---- ...ComposerOrchestratorComposer24TestCase.php | 273 +++++------ tests/Console/Command/DiffTest.php | 2 +- 13 files changed, 846 insertions(+), 312 deletions(-) create mode 100644 tests/Composer/AutoloadDumperTest.php diff --git a/composer.json b/composer.json index b7088742c..6a1cf57e4 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "composer/xdebug-handler": "^3.0.3", "fidry/console": "^0.5.3 || ^0.6.0", "fidry/filesystem": "^1.1", - "humbug/php-scoper": "^0.18.3", + "humbug/php-scoper": "^0.18.6", "justinrainbow/json-schema": "^5.2.12", "laravel/serializable-closure": "^1.2.2", "nikic/iter": "^2.2", diff --git a/composer.lock b/composer.lock index 076bd9b63..3481c4ecd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9d93c5fcbe62d3a1cc7a1ca2dd1fdfcd", + "content-hash": "e176960d081e81e8d9426176dfe8ce49", "packages": [ { "name": "amphp/amp", @@ -977,16 +977,16 @@ }, { "name": "humbug/php-scoper", - "version": "0.18.4", + "version": "0.18.6", "source": { "type": "git", "url": "https://github.com/humbug/php-scoper.git", - "reference": "d79c1486537280c21c907e9a8a610eceb391407f" + "reference": "e5a152c0f40c6cf7994fb27f14e50e47d7c4c4ff" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/humbug/php-scoper/zipball/d79c1486537280c21c907e9a8a610eceb391407f", - "reference": "d79c1486537280c21c907e9a8a610eceb391407f", + "url": "https://api.github.com/repos/humbug/php-scoper/zipball/e5a152c0f40c6cf7994fb27f14e50e47d7c4c4ff", + "reference": "e5a152c0f40c6cf7994fb27f14e50e47d7c4c4ff", "shasum": "" }, "require": { @@ -1054,9 +1054,9 @@ "description": "Prefixes all PHP namespaces in a file or directory.", "support": { "issues": "https://github.com/humbug/php-scoper/issues", - "source": "https://github.com/humbug/php-scoper/tree/0.18.4" + "source": "https://github.com/humbug/php-scoper/tree/0.18.6" }, - "time": "2023-10-20T17:14:04+00:00" + "time": "2023-11-04T16:43:20+00:00" }, { "name": "jetbrains/phpstorm-stubs", @@ -5785,5 +5785,5 @@ "platform-overrides": { "php": "8.1" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Box.php b/src/Box.php index 5b0f4ba88..710466bf1 100644 --- a/src/Box.php +++ b/src/Box.php @@ -132,6 +132,7 @@ public function endBuffering(?callable $dumpAutoload): void $dumpAutoload( $this->scoper->getSymbolsRegistry(), $this->scoper->getPrefix(), + $this->scoper->getExcludedFilePaths(), ); } diff --git a/src/Composer/AutoloadDumper.php b/src/Composer/AutoloadDumper.php index dc8468914..6a4133c88 100644 --- a/src/Composer/AutoloadDumper.php +++ b/src/Composer/AutoloadDumper.php @@ -17,6 +17,11 @@ use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator; use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\NotInstantiable; +use UnexpectedValueException; +use function array_map; +use function explode; +use function implode; +use function preg_match; use function preg_replace; use function str_replace; use const PHP_EOL; @@ -27,38 +32,85 @@ final class AutoloadDumper public static function generateAutoloadStatements( SymbolsRegistry $symbolsRegistry, + array $excludedComposerAutoloadFileHashes, string $autoloadContents, ): string { if (0 === $symbolsRegistry->count()) { return $autoloadContents; } + $autoloadContents = self::extractInlinedAutoloadContents($autoloadContents); + $scoperStatements = self::getOriginalScoperAutoloaderContents( + $symbolsRegistry, + $excludedComposerAutoloadFileHashes, + ); + + $indentedAutoloadContents = self::fixInlinedAutoloadIndent( + $autoloadContents, + self::getLoaderStatementIndent($scoperStatements), + ); + + $mergedAutoloadContents = preg_replace( + '/(\s*\\$loader \= .*autoload\.php.*)/', + $indentedAutoloadContents, + $scoperStatements, + ); + + return self::cleanupDuplicateLineReturns($mergedAutoloadContents); + } + + private static function extractInlinedAutoloadContents(string $autoloadContents): string + { $autoloadContents = str_replace('dump(); + private static function getOriginalScoperAutoloaderContents( + SymbolsRegistry $symbolsRegistry, + array $excludedComposerAutoloadFileHashes, + ): string { + $generator = new ScoperAutoloadGenerator($symbolsRegistry, $excludedComposerAutoloadFileHashes); + $scoperStatements = $generator->dump(); - $scoperStatements = preg_replace( + return preg_replace( '/scoper\-autoload\.php \@generated by PhpScoper/', '@generated by Humbug Box', $scoperStatements, ); + } - $scoperStatements = preg_replace( - '/(\s*\\$loader \= .*)/', - $autoloadContents, - $scoperStatements, + private static function getLoaderStatementIndent(string $scoperStatements): string + { + if (1 !== preg_match('/(? *)\\$loader \= .*autoload\.php.*/', $scoperStatements, $matches)) { + throw new UnexpectedValueException('Could not process the scoper autoloader statements'); + } + + return $matches['indent']; + } + + private static function fixInlinedAutoloadIndent(string $autoloadContents, string $indent): string + { + $lines = explode(PHP_EOL, $autoloadContents); + + $indentedLines = array_map( + static fn (string $line) => '' === $line ? $line : $indent.$line, + $lines, ); + return implode(PHP_EOL, $indentedLines); + } + + private static function cleanupDuplicateLineReturns(string $value): string + { return preg_replace( '/\n{2,}/m', PHP_EOL.PHP_EOL, - $scoperStatements, + $value, ); } } diff --git a/src/Composer/ComposerOrchestrator.php b/src/Composer/ComposerOrchestrator.php index d0eeee288..db259dd7c 100644 --- a/src/Composer/ComposerOrchestrator.php +++ b/src/Composer/ComposerOrchestrator.php @@ -112,6 +112,7 @@ public function dumpAutoload( SymbolsRegistry $symbolsRegistry, string $prefix, bool $excludeDevFiles, + array $excludedComposerAutoloadFileHashes, ): void { $this->dumpAutoloader(true === $excludeDevFiles); @@ -123,6 +124,7 @@ public function dumpAutoload( $autoloadContents = AutoloadDumper::generateAutoloadStatements( $symbolsRegistry, + $excludedComposerAutoloadFileHashes, $this->fileSystem->getFileContents($autoloadFile), ); diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 5958b249a..7a0f9e9e8 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -668,10 +668,11 @@ private static function commit( $box->endBuffering( $config->dumpAutoload() - ? static fn (SymbolsRegistry $symbolsRegistry, string $prefix) => $composerOrchestrator->dumpAutoload( + ? static fn (SymbolsRegistry $symbolsRegistry, string $prefix, array $excludeScoperFiles) => $composerOrchestrator->dumpAutoload( $symbolsRegistry, $prefix, $excludeDevFiles, + $excludeScoperFiles, ) : null, ); diff --git a/src/PhpScoper/NullScoper.php b/src/PhpScoper/NullScoper.php index 812246447..affe05f6f 100644 --- a/src/PhpScoper/NullScoper.php +++ b/src/PhpScoper/NullScoper.php @@ -45,4 +45,9 @@ public function getPrefix(): string { return ''; } + + public function getExcludedFilePaths(): array + { + return []; + } } diff --git a/src/PhpScoper/SerializableScoper.php b/src/PhpScoper/SerializableScoper.php index 88143b62d..152c00e87 100644 --- a/src/PhpScoper/SerializableScoper.php +++ b/src/PhpScoper/SerializableScoper.php @@ -33,7 +33,7 @@ final class SerializableScoper implements Scoper /** * @var list */ - private array $excludedFilePaths; + public array $excludedFilePaths; public function __construct( PhpScoperConfiguration $scoperConfig, @@ -114,4 +114,9 @@ private function createScoper(): PhpScoperScoper ...$this->excludedFilePaths, ); } + + public function getExcludedFilePaths(): array + { + return $this->excludedFilePaths; + } } diff --git a/tests/Composer/AutoloadDumperTest.php b/tests/Composer/AutoloadDumperTest.php new file mode 100644 index 000000000..e074eac97 --- /dev/null +++ b/tests/Composer/AutoloadDumperTest.php @@ -0,0 +1,437 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\Composer; + +use Humbug\PhpScoper\Symbol\SymbolsRegistry; +use PhpParser\Node\Name\FullyQualified; +use PHPUnit\Framework\TestCase; + +/** + * @coversNothing + * @internal + */ +final class AutoloadDumperTest extends TestCase +{ + /** + * @dataProvider autoloadProvider + */ + public function test_it_can_generate_the_autoload( + SymbolsRegistry $symbolsRegistry, + array $excludedComposerAutoloadFileHashes, + string $autoloadContents, + string $expected, + ): void { + $actual = AutoloadDumper::generateAutoloadStatements( + $symbolsRegistry, + $excludedComposerAutoloadFileHashes, + $autoloadContents, + ); + + self::assertSame($expected, $actual); + } + + public static function autoloadProvider(): iterable + { + yield 'no symbols' => [ + new SymbolsRegistry(), + [], + <<<'PHP' + [ + self::createRegistry( + [ + 'bar' => 'Humbug\bar', + 'foo' => 'Humbug\foo', + ], + [], + ), + [], + <<<'PHP' + [ + self::createRegistry( + [ + 'bar' => 'Humbug\bar', + 'foo' => 'Humbug\foo', + ], + [], + ), + ['a610a8e036135f992c6edfb10ca9f4e9', 'e252736c6babb7c097ab6692dbcb2a5a'], + <<<'PHP' + [ + self::createRegistry( + [ + 'Acme\bar' => 'Humbug\Acme\bar', + 'Acme\foo' => 'Humbug\Acme\foo', + 'Emca\baz' => 'Humbug\Emca\baz', + ], + [], + ), + [], + <<<'PHP' + $functions + * @param array $classes + */ + private static function createRegistry( + array $functions, + array $classes + ): SymbolsRegistry { + $registry = new SymbolsRegistry(); + + foreach ($functions as $original => $alias) { + $registry->recordFunction( + new FullyQualified($original), + new FullyQualified($alias), + ); + } + + foreach ($classes as $original => $alias) { + $registry->recordClass( + new FullyQualified($original), + new FullyQualified($alias), + ); + } + + return $registry; + } +} diff --git a/tests/Composer/ComposerOrchestratorComposer22TestCase.php b/tests/Composer/ComposerOrchestratorComposer22TestCase.php index 235004b19..b40878625 100644 --- a/tests/Composer/ComposerOrchestratorComposer22TestCase.php +++ b/tests/Composer/ComposerOrchestratorComposer22TestCase.php @@ -49,7 +49,7 @@ public function test_it_can_dump_the_autoloader_with_an_empty_composer_json( ): void { FS::dumpFile('composer.json', '{}'); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); $expectedPaths = [ 'composer.json', @@ -109,7 +109,7 @@ public function test_it_cannot_dump_the_autoloader_with_an_invalid_composer_json FS::dumpFile('composer.json'); try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -131,7 +131,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_with_a_depe { FS::mirror(self::FIXTURES.'/dir000', $this->tmp); - $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false); + $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false, []); $expectedPaths = [ 'composer.json', @@ -196,7 +196,7 @@ public function test_it_cannot_dump_the_autoloader_if_the_composer_json_file_is_ string $prefix, ): void { try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -313,6 +313,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in new SymbolsRegistry(), '', true, + [], ); // The fact that there is a dependency in the `composer.json` does not change anything to Composer @@ -387,7 +388,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_and_lock_wi $this->skipIfFixturesNotInstalled(self::FIXTURES.'/dir002/vendor'); FS::mirror(self::FIXTURES.'/dir002', $this->tmp); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); // The fact that there is a dependency in the `composer.json` does not change anything to Composer $expectedPaths = [ @@ -482,32 +483,35 @@ public static function composerAutoloadProvider(): iterable // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - require_once __DIR__ . '/composer/autoload_real.php'; + // autoload.php @generated by Composer - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } @@ -531,27 +535,30 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - require_once __DIR__ . '/composer/autoload_real.php'; + // autoload.php @generated by Composer - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases @@ -577,27 +584,30 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - require_once __DIR__ . '/composer/autoload_real.php'; + // autoload.php @generated by Composer - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Function aliases. For more information see: @@ -635,34 +645,37 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - require_once __DIR__ . '/composer/autoload_real.php'; + // autoload.php @generated by Composer - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases namespace { if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } diff --git a/tests/Composer/ComposerOrchestratorComposer23TestCase.php b/tests/Composer/ComposerOrchestratorComposer23TestCase.php index 6ad8ee58d..be9029338 100644 --- a/tests/Composer/ComposerOrchestratorComposer23TestCase.php +++ b/tests/Composer/ComposerOrchestratorComposer23TestCase.php @@ -49,7 +49,7 @@ public function test_it_can_dump_the_autoloader_with_an_empty_composer_json( ): void { FS::dumpFile('composer.json', '{}'); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); $expectedPaths = [ 'composer.json', @@ -109,7 +109,7 @@ public function test_it_cannot_dump_the_autoloader_with_an_invalid_composer_json FS::dumpFile('composer.json'); try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -131,7 +131,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_with_a_depe { FS::mirror(self::FIXTURES.'/dir000', $this->tmp); - $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false); + $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false, []); $expectedPaths = [ 'composer.json', @@ -201,7 +201,7 @@ public function test_it_cannot_dump_the_autoloader_if_the_composer_json_file_is_ string $prefix, ): void { try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -323,6 +323,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in new SymbolsRegistry(), '', true, + [], ); // The fact that there is a dependency in the `composer.json` does not change anything to Composer @@ -397,7 +398,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_and_lock_wi $this->skipIfFixturesNotInstalled(self::FIXTURES.'/dir002/vendor'); FS::mirror(self::FIXTURES.'/dir002', $this->tmp); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); // The fact that there is a dependency in the `composer.json` does not change anything to Composer $expectedPaths = [ @@ -497,37 +498,40 @@ public static function composerAutoloadProvider(): iterable // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - if (PHP_VERSION_ID < 50600) { - echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - exit(1); - } + // autoload.php @generated by Composer - require_once __DIR__ . '/composer/autoload_real.php'; + if (PHP_VERSION_ID < 50600) { + echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + exit(1); + } - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } @@ -551,32 +555,35 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - if (PHP_VERSION_ID < 50600) { - echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - exit(1); - } + // autoload.php @generated by Composer - require_once __DIR__ . '/composer/autoload_real.php'; + if (PHP_VERSION_ID < 50600) { + echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + exit(1); + } - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases @@ -602,32 +609,35 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - if (PHP_VERSION_ID < 50600) { - echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - exit(1); - } + // autoload.php @generated by Composer - require_once __DIR__ . '/composer/autoload_real.php'; + if (PHP_VERSION_ID < 50600) { + echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + exit(1); + } - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Function aliases. For more information see: @@ -665,39 +675,42 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // @generated by Humbug Box - if (PHP_VERSION_ID < 50600) { - echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - exit(1); - } + // autoload.php @generated by Composer - require_once __DIR__ . '/composer/autoload_real.php'; + if (PHP_VERSION_ID < 50600) { + echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + exit(1); + } - \$loader = {$composerAutoloaderName}::getLoader(); + require_once __DIR__ . '/composer/autoload_real.php'; - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + \$loader = {$composerAutoloaderName}::getLoader(); - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases namespace { if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } diff --git a/tests/Composer/ComposerOrchestratorComposer24TestCase.php b/tests/Composer/ComposerOrchestratorComposer24TestCase.php index 2b5686662..06fbdc6e9 100644 --- a/tests/Composer/ComposerOrchestratorComposer24TestCase.php +++ b/tests/Composer/ComposerOrchestratorComposer24TestCase.php @@ -48,7 +48,7 @@ public function test_it_can_dump_the_autoloader_with_an_empty_composer_json( ): void { FS::dumpFile('composer.json', '{}'); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); $expectedPaths = [ 'composer.json', @@ -108,7 +108,7 @@ public function test_it_cannot_dump_the_autoloader_with_an_invalid_composer_json FS::dumpFile('composer.json'); try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -130,7 +130,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_with_a_depe { FS::mirror(self::FIXTURES.'/dir000', $this->tmp); - $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false); + $this->composerOrchestrator->dumpAutoload(new SymbolsRegistry(), '', false, []); $expectedPaths = [ 'composer.json', @@ -213,7 +213,7 @@ public function test_it_cannot_dump_the_autoloader_if_the_composer_json_file_is_ string $prefix, ): void { try { - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); self::fail('Expected exception to be thrown.'); } catch (RuntimeException $exception) { @@ -242,7 +242,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in $this->skipIfFixturesNotInstalled(self::FIXTURES.'/dir001/vendor'); FS::mirror(self::FIXTURES.'/dir001', $this->tmp); - $this->composerOrchestrator->dumpAutoload($SymbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($SymbolsRegistry, $prefix, false, []); // The fact that there is a dependency in the `composer.json` does not change anything to Composer $expectedPaths = [ @@ -348,6 +348,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in new SymbolsRegistry(), '', true, + [], ); // The fact that there is a dependency in the `composer.json` does not change anything to Composer @@ -422,7 +423,7 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_and_lock_wi $this->skipIfFixturesNotInstalled(self::FIXTURES.'/dir002/vendor'); FS::mirror(self::FIXTURES.'/dir002', $this->tmp); - $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false); + $this->composerOrchestrator->dumpAutoload($symbolsRegistry, $prefix, false, []); // The fact that there is a dependency in the `composer.json` does not change anything to Composer $expectedPaths = [ @@ -535,50 +536,51 @@ public static function composerAutoloadProvider(): iterable // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // autoload.php @generated by Composer - if (PHP_VERSION_ID < 50600) { - if (!headers_sent()) { - header('HTTP/1.1 500 Internal Server Error'); - } - \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - if (!ini_get('display_errors')) { - if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { - fwrite(STDERR, \$err); - } elseif (!headers_sent()) { - echo \$err; + if (PHP_VERSION_ID < 50600) { + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); + } + \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + if (!ini_get('display_errors')) { + if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { + fwrite(STDERR, \$err); + } elseif (!headers_sent()) { + echo \$err; + } } + trigger_error( + \$err, + E_USER_ERROR + ); } - trigger_error( - \$err, - E_USER_ERROR - ); - } - require_once __DIR__ . '/composer/autoload_real.php'; + require_once __DIR__ . '/composer/autoload_real.php'; - \$loader = {$composerAutoloaderName}::getLoader(); + \$loader = {$composerAutoloaderName}::getLoader(); - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } @@ -602,45 +604,46 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // autoload.php @generated by Composer - if (PHP_VERSION_ID < 50600) { - if (!headers_sent()) { - header('HTTP/1.1 500 Internal Server Error'); - } - \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - if (!ini_get('display_errors')) { - if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { - fwrite(STDERR, \$err); - } elseif (!headers_sent()) { - echo \$err; + if (PHP_VERSION_ID < 50600) { + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); } + \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + if (!ini_get('display_errors')) { + if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { + fwrite(STDERR, \$err); + } elseif (!headers_sent()) { + echo \$err; + } + } + trigger_error( + \$err, + E_USER_ERROR + ); } - trigger_error( - \$err, - E_USER_ERROR - ); - } - require_once __DIR__ . '/composer/autoload_real.php'; + require_once __DIR__ . '/composer/autoload_real.php'; - \$loader = {$composerAutoloaderName}::getLoader(); + \$loader = {$composerAutoloaderName}::getLoader(); - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases @@ -666,45 +669,46 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // autoload.php @generated by Composer - if (PHP_VERSION_ID < 50600) { - if (!headers_sent()) { - header('HTTP/1.1 500 Internal Server Error'); - } - \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - if (!ini_get('display_errors')) { - if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { - fwrite(STDERR, \$err); - } elseif (!headers_sent()) { - echo \$err; + if (PHP_VERSION_ID < 50600) { + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); + } + \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + if (!ini_get('display_errors')) { + if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { + fwrite(STDERR, \$err); + } elseif (!headers_sent()) { + echo \$err; + } + } + trigger_error( + \$err, + E_USER_ERROR + ); } - } - trigger_error( - \$err, - E_USER_ERROR - ); - } - require_once __DIR__ . '/composer/autoload_real.php'; + require_once __DIR__ . '/composer/autoload_real.php'; - \$loader = {$composerAutoloaderName}::getLoader(); + \$loader = {$composerAutoloaderName}::getLoader(); - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Function aliases. For more information see: @@ -742,52 +746,53 @@ function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): voi // @generated by Humbug Box namespace { - // Backup the autoloaded Composer files - if (isset(\$GLOBALS['__composer_autoload_files'])) { - \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; - } + \$loader = (static function () { + // Backup the autoloaded Composer files + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'] ?? []; - // autoload.php @generated by Composer + // autoload.php @generated by Composer - if (PHP_VERSION_ID < 50600) { - if (!headers_sent()) { - header('HTTP/1.1 500 Internal Server Error'); - } - \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; - if (!ini_get('display_errors')) { - if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { - fwrite(STDERR, \$err); - } elseif (!headers_sent()) { - echo \$err; + if (PHP_VERSION_ID < 50600) { + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); + } + \$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; + if (!ini_get('display_errors')) { + if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { + fwrite(STDERR, \$err); + } elseif (!headers_sent()) { + echo \$err; + } + } + trigger_error( + \$err, + E_USER_ERROR + ); } - } - trigger_error( - \$err, - E_USER_ERROR - ); - } - require_once __DIR__ . '/composer/autoload_real.php'; + require_once __DIR__ . '/composer/autoload_real.php'; - \$loader = {$composerAutoloaderName}::getLoader(); + \$loader = {$composerAutoloaderName}::getLoader(); - // Ensure InstalledVersions is available - \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; - if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; + // Ensure InstalledVersions is available + \$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php'; + if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath; - // Restore the backup - if (isset(\$existingComposerAutoloadFiles)) { - \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; - } else { - unset(\$GLOBALS['__composer_autoload_files']); - } + // Restore the backup and ensure the excluded files are properly marked as loaded + \$GLOBALS['__composer_autoload_files'] = \\array_merge( + \$existingComposerAutoloadFiles, + \\array_fill_keys([], true) + ); + + return \$loader; + })(); } // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases namespace { if (!function_exists('humbug_phpscoper_expose_class')) { - function humbug_phpscoper_expose_class(string \$exposed, string \$prefixed): void { + function humbug_phpscoper_expose_class(\$exposed, \$prefixed) { if (!class_exists(\$exposed, false) && !interface_exists(\$exposed, false) && !trait_exists(\$exposed, false)) { spl_autoload_call(\$prefixed); } diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php index 10f79d74f..4b0ae5c3d 100644 --- a/tests/Console/Command/DiffTest.php +++ b/tests/Console/Command/DiffTest.php @@ -748,7 +748,7 @@ public static function GNUDiffPharsProvider(): iterable // Comparing the two archives contents (gnu diff)... Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-baz.phar/.phar - diff --exclude=.phar/meta.json simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php + diff '--exclude=.phar/meta.json' simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php 3c3 < echo "Hello world!"; ---