diff --git a/bin/rector.php b/bin/rector.php index 48a0cc6e8bb..07eea9a09cc 100755 --- a/bin/rector.php +++ b/bin/rector.php @@ -3,10 +3,10 @@ declare(strict_types=1); use Nette\Utils\Json; +use Rector\Bootstrap\AutoloadFileParameterResolver; use Rector\Bootstrap\RectorConfigsResolver; use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\Configuration\Option; -use Rector\Configuration\Parameter\SimpleParameterProvider; use Rector\Console\Style\SymfonyStyleFactory; use Rector\DependencyInjection\LazyContainerFactory; use Rector\DependencyInjection\RectorContainerFactory; @@ -128,13 +128,7 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void $autoloadIncluder->autoloadRectorInstalledAsGlobalDependency(); $autoloadIncluder->autoloadFromCommandLine(); -// a different extra autoload changes what PHPStan can resolve, so cached results -// must not survive it: register it before the configuration hash is computed. -// ArgvInput handles both "--autoload-file path" and "--autoload-file=path" -$autoloadFileOption = (new ArgvInput())->getParameterOption(['--autoload-file', '-a'], null); -if (is_string($autoloadFileOption) && $autoloadFileOption !== '') { - SimpleParameterProvider::setParameter(Option::AUTOLOAD_FILE, realpath($autoloadFileOption) ?: $autoloadFileOption); -} +AutoloadFileParameterResolver::resolveFromArgv($_SERVER['argv']); $rectorConfigsResolver = new RectorConfigsResolver(); diff --git a/src/Bootstrap/AutoloadFileParameterResolver.php b/src/Bootstrap/AutoloadFileParameterResolver.php new file mode 100644 index 00000000000..7aa2462e7c4 --- /dev/null +++ b/src/Bootstrap/AutoloadFileParameterResolver.php @@ -0,0 +1,37 @@ + $argv + */ + public static function resolveFromArgv(array $argv): void + { + // handles "--autoload-file path", "--autoload-file=path" and "-a path"; + // parallel workers receive the space-separated form, so all spellings must + // normalize to the same value or main process and workers would disagree + $autoloadFile = (new ArgvInput($argv))->getParameterOption(['--autoload-file', '-a'], null); + if (! is_string($autoloadFile) || $autoloadFile === '') { + return; + } + + SimpleParameterProvider::setParameter(Option::AUTOLOAD_FILE, realpath($autoloadFile) ?: $autoloadFile); + } +} diff --git a/tests/Bootstrap/AutoloadFileParameterResolverTest.php b/tests/Bootstrap/AutoloadFileParameterResolverTest.php new file mode 100644 index 00000000000..6068d1e5834 --- /dev/null +++ b/tests/Bootstrap/AutoloadFileParameterResolverTest.php @@ -0,0 +1,70 @@ + $argv + */ + #[DataProvider('provideArgvSpellings')] + public function testEverySpellingResolvesToSameRealPath(array $argv): void + { + AutoloadFileParameterResolver::resolveFromArgv($argv); + + $this->assertSame( + (string) realpath(__FILE__), + SimpleParameterProvider::provideStringParameter(Option::AUTOLOAD_FILE) + ); + } + + /** + * @return iterable}> + */ + public static function provideArgvSpellings(): iterable + { + $relativePath = 'tests/Bootstrap/' . basename(__FILE__); + + yield 'long with space' => [['bin/rector', 'process', '--autoload-file', __FILE__]]; + yield 'long with equals' => [['bin/rector', 'process', '--autoload-file=' . __FILE__]]; + yield 'short with space' => [['bin/rector', 'process', '-a', __FILE__]]; + yield 'relative path is normalized' => [['bin/rector', 'process', '-a', $relativePath]]; + } + + public function testWithoutOptionParameterStaysUntouched(): void + { + AutoloadFileParameterResolver::resolveFromArgv(['bin/rector', 'process', '--dry-run']); + + $this->assertSame('', SimpleParameterProvider::provideStringParameter(Option::AUTOLOAD_FILE)); + } + + public function testResolvedAutoloadFileChangesConfigurationHash(): void + { + $fileHashComputer = new FileHashComputer(); + $configFilePath = __DIR__ . '/config/some_config.php'; + + $hashWithout = $fileHashComputer->compute($configFilePath); + + AutoloadFileParameterResolver::resolveFromArgv(['bin/rector', 'process', '-a', __FILE__]); + + $this->assertNotSame($hashWithout, $fileHashComputer->compute($configFilePath)); + } +} diff --git a/tests/Bootstrap/config/some_config.php b/tests/Bootstrap/config/some_config.php new file mode 100644 index 00000000000..74a42ef42cc --- /dev/null +++ b/tests/Bootstrap/config/some_config.php @@ -0,0 +1,8 @@ +