diff --git a/phpstan.neon b/phpstan.neon index 25ce690b3d5..c154214183a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -99,6 +99,11 @@ parameters: - src/StaticTypeMapper/Naming/NameScopeFactory.php - utils/phpstan/src/Rule/NoResolveClassReflectionInForeachRule.php + # services are instantiated by the container via reflection, which shipmonk cannot see + - + identifier: shipmonk.deadMethod + message: '#::__construct$#' + # called magically by parent - message: '#^Unused Rector\\PhpParser\\Printer\\BetterStandardPrinter\:\:(pInterpolatedStringPart|pStmt_FileNode)$#' diff --git a/src/Application/ApplicationFileProcessor.php b/src/Application/ApplicationFileProcessor.php index 42877400060..585090607eb 100644 --- a/src/Application/ApplicationFileProcessor.php +++ b/src/Application/ApplicationFileProcessor.php @@ -27,7 +27,6 @@ use Rector\ValueObject\FileProcessResult; use Rector\ValueObject\ProcessResult; use Rector\ValueObject\Reporting\FileDiff; -use Symfony\Component\Console\Input\InputInterface; use Throwable; final class ApplicationFileProcessor @@ -54,7 +53,7 @@ public function __construct( ) { } - public function run(Configuration $configuration, InputInterface $input): ProcessResult + public function run(Configuration $configuration): ProcessResult { // scope the cache to this run's --only / --only-suffix selection before any cache read/write $this->changedFilesDetector->setActiveScope($configuration->getOnlyRules(), $configuration->getOnlySuffix(), $configuration->getFilters()); @@ -97,7 +96,7 @@ public function run(Configuration $configuration, InputInterface $input): Proces } if ($configuration->isParallel()) { - $processResult = $this->runParallel($filePaths, $input, $postFileCallback); + $processResult = $this->runParallel($filePaths, $configuration, $postFileCallback); } else { $processResult = $this->processFiles($filePaths, $configuration, $preFileCallback, $postFileCallback); } @@ -260,7 +259,7 @@ private function restoreErrorHandler(): void */ private function runParallel( array $filePaths, - InputInterface $input, + Configuration $configuration, callable $postFileCallback, ): ProcessResult { $mainScript = $this->resolveCalledRectorBinary(); @@ -277,7 +276,7 @@ private function runParallel( $schedule = $this->scheduleFactory->create($cpuCores, $jobSize, $maxNumberOfProcesses, $filePaths); // mimics see https://github.com/phpstan/phpstan-src/commit/9124c66dcc55a222e21b1717ba5f60771f7dda92#diff-387b8f04e0db7a06678eb52ce0c0d0aff73e0d7d8fc5df834d0a5fbec198e5daR139 - return $this->parallelFileProcessor->process($schedule, $mainScript, $postFileCallback, $input); + return $this->parallelFileProcessor->process($schedule, $mainScript, $postFileCallback, $configuration); } /** diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index d6563193ef1..8d628bdf3ad 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -172,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int // MAIN PHASE // 2. run Rector - $processResult = $this->applicationFileProcessor->run($configuration, $input); + $processResult = $this->applicationFileProcessor->run($configuration); // REPORTING PHASE // 3. reporting phaseRunning 2nd time with collectors data diff --git a/src/Parallel/Application/ParallelFileProcessor.php b/src/Parallel/Application/ParallelFileProcessor.php index c0ddefc879c..e3af99810bc 100644 --- a/src/Parallel/Application/ParallelFileProcessor.php +++ b/src/Parallel/Application/ParallelFileProcessor.php @@ -12,7 +12,6 @@ use React\Socket\TcpServer; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; -use Rector\Console\Command\ProcessCommand; use Rector\Console\ExitCode; use Rector\Parallel\Command\WorkerCommandLineFactory; use Rector\Parallel\Enum\Action; @@ -24,9 +23,9 @@ use Rector\Parallel\ValueObject\ParallelProcess; use Rector\Parallel\ValueObject\ProcessPool; use Rector\Parallel\ValueObject\Schedule; +use Rector\ValueObject\Configuration; use Rector\ValueObject\Error\SystemError; use Rector\ValueObject\ProcessResult; -use Symfony\Component\Console\Input\InputInterface; use Throwable; /** @@ -58,7 +57,7 @@ public function process( Schedule $schedule, string $mainScript, callable $postFileCallback, - InputInterface $input + Configuration $configuration ): ProcessResult { // reversed, so that array_pop() hands out the jobs in bucket order $jobsPerWorker = array_map( @@ -157,7 +156,7 @@ public function process( &$systemErrorsCount, &$reachedSystemErrorsCountLimit, $mainScript, - $input, + $configuration, $serverPort, $streamSelectLoop, $timeoutInSeconds, @@ -172,9 +171,8 @@ public function process( $workerCommandLine = $this->workerCommandLineFactory->create( $mainScript, - ProcessCommand::class, 'worker', - $input, + $configuration, $processIdentifier, $serverPort, ); diff --git a/src/Parallel/Command/WorkerCommandLineFactory.php b/src/Parallel/Command/WorkerCommandLineFactory.php index 99161845e71..0b16ce677b9 100644 --- a/src/Parallel/Command/WorkerCommandLineFactory.php +++ b/src/Parallel/Command/WorkerCommandLineFactory.php @@ -5,86 +5,55 @@ namespace Rector\Parallel\Command; use PHPStan\Turbo\TurboExtensionSelector; +use Rector\Bootstrap\CliOptionValueResolver; use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\Configuration\Option; use Rector\FileSystem\FilePathHelper; -use Rector\Parallel\Exception\ParallelShouldNotHappenException; -use Rector\Parallel\Reflection\CommandFromReflectionFactory; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; +use Rector\ValueObject\Configuration; /** * @see \Rector\Tests\Parallel\Command\WorkerCommandLineFactoryTest - * @todo possibly extract to symplify/easy-parallel */ final readonly class WorkerCommandLineFactory { private const string OPTION_DASHES = '--'; public function __construct( - private CommandFromReflectionFactory $commandFromReflectionFactory, private FilePathHelper $filePathHelper ) { } /** - * @param class-string $mainCommandClass + * Build the worker sub-process command line from the resolved Configuration */ public function create( string $mainScript, - string $mainCommandClass, string $workerCommandName, - InputInterface $input, + Configuration $configuration, string $identifier, int $port ): string { - $commandArguments = array_slice($_SERVER['argv'], 1); - - // add implicit "process" command name if missing - if ($commandArguments !== [] && ($commandArguments[0] !== 'process' && $commandArguments[0] !== 'p') && ! defined( - 'PHPUNIT_COMPOSER_INSTALL' - )) { - $commandArguments = array_merge(['process'], $commandArguments); - } - - $args = [PHP_BINARY, ...$this->createTurboExtensionArguments(), $mainScript, ...$commandArguments]; - $workerCommandArray = []; - - $mainCommand = $this->commandFromReflectionFactory->create($mainCommandClass); - - if ($mainCommand->getName() === null) { - $errorMessage = sprintf('The command name for "%s" is missing', $mainCommand::class); - throw new ParallelShouldNotHappenException($errorMessage); - } - - $mainCommandName = $mainCommand->getName(); - $mainCommandNames = [$mainCommandName, $mainCommandName[0]]; - - foreach ($args as $arg) { - // skip command name - if (in_array($arg, $mainCommandNames, true)) { - break; - } - - $workerCommandArray[] = escapeshellarg((string) $arg); - } - - $workerCommandArray[] = $workerCommandName; + $workerCommandArray = [ + escapeshellarg(PHP_BINARY), + ...array_map(escapeshellarg(...), $this->createTurboExtensionArguments()), + escapeshellarg($mainScript), + $workerCommandName, + ]; - $mainCommandOptionNames = $this->getCommandOptionNames($mainCommand); - $workerCommandOptions = $this->mirrorCommandOptions($input, $mainCommandOptionNames); - $workerCommandArray = array_merge($workerCommandArray, $workerCommandOptions); + $workerCommandArray = [ + ...$workerCommandArray, + ...$this->createFlagOptions($configuration), + ...$this->createValueOptions($configuration), + ]; // for TCP local server - $workerCommandArray[] = '--port'; - $workerCommandArray[] = $port; + $workerCommandArray[] = self::OPTION_DASHES . Option::PARALLEL_PORT; + $workerCommandArray[] = (string) $port; - $workerCommandArray[] = '--identifier'; + $workerCommandArray[] = self::OPTION_DASHES . Option::PARALLEL_IDENTIFIER; $workerCommandArray[] = escapeshellarg($identifier); - /** @var string[] $paths */ - $paths = $input->getArgument(Option::SOURCE); - foreach ($paths as $path) { + foreach ($configuration->getPaths() as $path) { $workerCommandArray[] = escapeshellarg($path); } @@ -96,49 +65,90 @@ public function create( // @see https://github.com/symfony/symfony/issues/1238 $workerCommandArray[] = '--no-ansi'; - // Only pass --config if explicitly set via command line - // If not set, the worker will resolve config using RectorConfigsResolver fallback mechanism - if ($input->hasOption(Option::CONFIG)) { - $configValue = $input->getOption(Option::CONFIG); - if (is_string($configValue) && $configValue !== '') { - $workerCommandArray[] = '--config'; - /** - * On parallel, the command is generated with `--config` addition - * Using escapeshellarg() to ensure the --config path escaped, even when it has a space. - * - * eg: - * --config /path/e2e/parallel with space/rector.php - * - * that can cause error: - * - * File /rector-src/e2e/parallel\" was not found - * - * the escaped result is: - * - * --config '/path/e2e/parallel with space/rector.php' - * - * tested in macOS and Ubuntu (github action) - */ - $config = $configValue; - $workerCommandArray[] = escapeshellarg($this->filePathHelper->relativePath($config)); - } + // only pass --config if explicitly set via command line, otherwise the worker + // resolves it through the RectorConfigsResolver fallback, same as the main process + $config = CliOptionValueResolver::resolve($_SERVER['argv'] ?? [], ['--config', '-c']); + if ($config !== null && $config !== '') { + $workerCommandArray[] = self::OPTION_DASHES . Option::CONFIG; + // escaped, so a path with space stays one argument + $workerCommandArray[] = escapeshellarg($this->filePathHelper->relativePath($config)); } - if ((bool) $input->getOption(Option::COMPOSER_BASED)) { - $workerCommandArray[] = self::OPTION_DASHES . Option::COMPOSER_BASED; + return implode(' ', $workerCommandArray); + } + + /** + * @return string[] + */ + private function createFlagOptions(Configuration $configuration): array + { + $flagOptions = [ + Option::DRY_RUN => $configuration->isDryRun(), + Option::CLEAR_CACHE => $configuration->shouldClearCache(), + Option::NO_DIFFS => ! $configuration->shouldShowDiffs(), + Option::COMPOSER_BASED => $configuration->isComposerBased(), + Option::PHP => $configuration->isPhpOnly(), + Option::DEBUG => $configuration->isDebug(), + // read from argv, as it is handled in bin/rector.php before the configuration exists + Option::XDEBUG => in_array(self::OPTION_DASHES . Option::XDEBUG, $_SERVER['argv'] ?? [], true), + ]; + + $workerOptions = []; + foreach ($flagOptions as $optionName => $isEnabled) { + if ($isEnabled) { + $workerOptions[] = self::OPTION_DASHES . $optionName; + } } - return implode(' ', $workerCommandArray); + return $workerOptions; } - private function shouldSkipOption(InputInterface $input, string $optionName): bool + /** + * @return string[] + */ + private function createValueOptions(Configuration $configuration): array { - if (! $input->hasOption($optionName)) { - return true; + $workerOptions = []; + + // read from argv, as it is autoloaded in bin/rector.php before the configuration exists; + // the worker needs the space-separated form there + $autoloadFile = CliOptionValueResolver::resolve($_SERVER['argv'] ?? [], ['--autoload-file', '-a']); + if ($autoloadFile !== null && $autoloadFile !== '') { + $workerOptions[] = self::OPTION_DASHES . Option::AUTOLOAD_FILE; + $workerOptions[] = escapeshellarg($autoloadFile); + } + + // repeated --only, one flag per value + foreach ($configuration->getOnlyRules() as $onlyRule) { + $workerOptions[] = self::OPTION_DASHES . Option::ONLY; + $workerOptions[] = escapeshellarg($onlyRule); + } + + $onlySuffix = $configuration->getOnlySuffix(); + if ($onlySuffix !== null && $onlySuffix !== '') { + $workerOptions[] = self::OPTION_DASHES . Option::ONLY_SUFFIX; + $workerOptions[] = escapeshellarg($onlySuffix); + } + + // the worker scopes the cache by filters too + if ($configuration->getFilters() !== []) { + $workerOptions[] = self::OPTION_DASHES . Option::FILTER; + $workerOptions[] = escapeshellarg(implode(',', $configuration->getFilters())); } - // skip output format and config, handled separately in create() - return $optionName === Option::OUTPUT_FORMAT || $optionName === Option::CONFIG; + $memoryLimit = $configuration->getMemoryLimit(); + if ($memoryLimit !== null && $memoryLimit !== '') { + // "=" form, symfony/console does not accept -1 as value without assign + $workerOptions[] = self::OPTION_DASHES . Option::MEMORY_LIMIT . '=' . escapeshellarg($memoryLimit); + } + + $maxChanges = $configuration->getMaxChanges(); + if ($maxChanges !== null) { + $workerOptions[] = self::OPTION_DASHES . Option::MAX_CHANGES; + $workerOptions[] = escapeshellarg((string) $maxChanges); + } + + return $workerOptions; } /** @@ -161,74 +171,4 @@ private function createTurboExtensionArguments(): array TurboExtensionSelector::EXTENSION_PATH_INI . "='" . $extensionPath . "'", ]; } - - /** - * @return string[] - */ - private function getCommandOptionNames(Command $command): array - { - $inputDefinition = $command->getDefinition(); - - $optionNames = []; - foreach ($inputDefinition->getOptions() as $inputOption) { - $optionNames[] = $inputOption->getName(); - } - - return $optionNames; - } - - /** - * Keeps all options that are allowed in check command options - * - * @param string[] $mainCommandOptionNames - * @return string[] - */ - private function mirrorCommandOptions(InputInterface $input, array $mainCommandOptionNames): array - { - $workerCommandOptions = []; - - foreach ($mainCommandOptionNames as $mainCommandOptionName) { - if ($this->shouldSkipOption($input, $mainCommandOptionName)) { - continue; - } - - /** @var bool|string|string[]|null $optionValue */ - $optionValue = $input->getOption($mainCommandOptionName); - - // skip clutter - if ($optionValue === null) { - continue; - } - - if (is_bool($optionValue)) { - if ($optionValue) { - $workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName; - } - - continue; - } - - // array options (e.g. repeated --only) are mirrored one flag per value - if (is_array($optionValue)) { - foreach ($optionValue as $singleOptionValue) { - $workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName; - $workerCommandOptions[] = \escapeshellarg($singleOptionValue); - } - - continue; - } - - if ($mainCommandOptionName === 'memory-limit') { - // symfony/console does not accept -1 as value without assign - $workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName . '=' . \escapeshellarg( - $optionValue - ); - } else { - $workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName; - $workerCommandOptions[] = \escapeshellarg($optionValue); - } - } - - return $workerCommandOptions; - } } diff --git a/src/Parallel/Reflection/CommandFromReflectionFactory.php b/src/Parallel/Reflection/CommandFromReflectionFactory.php deleted file mode 100644 index f233ad09714..00000000000 --- a/src/Parallel/Reflection/CommandFromReflectionFactory.php +++ /dev/null @@ -1,37 +0,0 @@ - $className - */ - public function create(string $className): Command - { - $commandReflectionClass = new ReflectionClass($className); - - $command = $commandReflectionClass->newInstanceWithoutConstructor(); - $parentClassReflection = $commandReflectionClass->getParentClass(); - - if (! $parentClassReflection instanceof ReflectionClass) { - throw new ParallelShouldNotHappenException(); - } - - $parentConstructorReflectionMethod = $parentClassReflection->getConstructor(); - if (! $parentConstructorReflectionMethod instanceof ReflectionMethod) { - throw new ParallelShouldNotHappenException(); - } - - $parentConstructorReflectionMethod->invoke($command); - - return $command; - } -} diff --git a/tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/tests/Parallel/Command/WorkerCommandLineFactoryTest.php index 5b7d0f7d6eb..bddd00d77ea 100644 --- a/tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -7,201 +7,138 @@ use Iterator; use PHPStan\Turbo\TurboExtensionSelector; use PHPUnit\Framework\Attributes\DataProvider; -use Rector\ChangesReporting\Output\ConsoleOutputFormatter; -use Rector\Configuration\Option; -use Rector\Console\Command\ProcessCommand; use Rector\Parallel\Command\WorkerCommandLineFactory; use Rector\Testing\PHPUnit\AbstractLazyTestCase; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputDefinition; +use Rector\ValueObject\Configuration; final class WorkerCommandLineFactoryTest extends AbstractLazyTestCase { - private const string COMMAND = 'command'; - private const string DUMMY_MAIN_SCRIPT = 'main_script'; private const string SPACED_DUMMY_MAIN_SCRIPT = 'C:\Users\P\Desktop\Web Dev\vendor\bin\rector'; private WorkerCommandLineFactory $workerCommandLineFactory; - private ProcessCommand $processCommand; + /** + * @var array + */ + private array $originalArgv = []; protected function setUp(): void { parent::setUp(); $this->workerCommandLineFactory = $this->make(WorkerCommandLineFactory::class); - $this->processCommand = $this->make(ProcessCommand::class); + $this->originalArgv = $_SERVER['argv']; } - /** - * @param array $inputParameters - */ - #[DataProvider('provideDataSpacedMainScript')] - public function testSpacedMainScript(array $inputParameters, string $expectedCommand): void + protected function tearDown(): void { - $inputDefinition = $this->prepareProcessCommandDefinition(); - $arrayInput = new ArrayInput($inputParameters, $inputDefinition); + $_SERVER['argv'] = $this->originalArgv; + } + + public function testSpacedMainScript(): void + { + $_SERVER['argv'] = ['bin/rector', 'process', 'src']; $workerCommandLine = $this->workerCommandLineFactory->create( self::SPACED_DUMMY_MAIN_SCRIPT, - ProcessCommand::class, 'worker', - $arrayInput, + new Configuration(paths: ['src']), 'identifier', 2000 ); - $expectedCommand = $this->normalizeExpectedCommandOutput($expectedCommand); - $expectedCommand = $this->cleanUpEmptyQuoteExpectedCommandOutput($expectedCommand); + $expectedCommand = $this->normalizeExpectedCommandOutput( + "'" . PHP_BINARY . "' " . $this->turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi" + ); $this->assertSame($expectedCommand, $workerCommandLine); } /** - * @return Iterator>|string[]> - */ - public static function provideDataSpacedMainScript(): Iterator - { - $cliInputOptions = array_slice($_SERVER['argv'], 1); - $cliInputOptionsAsString = implode("' '", $cliInputOptions); - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", - ]; - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", - ]; - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::DEBUG => true, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", - ]; - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::DEBUG => true, - '--' . Option::XDEBUG => true, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", - ]; - } - - /** - * @param array $inputParameters + * @param array $argv */ #[DataProvider('provideData')] - public function test(array $inputParameters, string $expectedCommand): void + public function test(array $argv, Configuration $configuration, string $expectedOptions): void { - $inputDefinition = $this->prepareProcessCommandDefinition(); - $arrayInput = new ArrayInput($inputParameters, $inputDefinition); + $_SERVER['argv'] = $argv; $workerCommandLine = $this->workerCommandLineFactory->create( self::DUMMY_MAIN_SCRIPT, - ProcessCommand::class, 'worker', - $arrayInput, + $configuration, 'identifier', 2000 ); - $expectedCommand = $this->normalizeExpectedCommandOutput($expectedCommand); - $expectedCommand = $this->cleanUpEmptyQuoteExpectedCommandOutput($expectedCommand); + $expectedCommand = $this->normalizeExpectedCommandOutput( + "'" . PHP_BINARY . "' " . $this->turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' worker " . $expectedOptions + ); $this->assertSame($expectedCommand, $workerCommandLine); } /** - * @return Iterator>|string[]> + * @return Iterator, Configuration, string}> */ public static function provideData(): Iterator { - $cliInputOptions = array_slice($_SERVER['argv'], 1); - $cliInputOptionsAsString = implode("' '", $cliInputOptions); - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + $defaultArgv = ['bin/rector', 'process', 'src']; + $suffix = "--port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi"; + + yield 'no options' => [$defaultArgv, new Configuration(paths: ['src']), $suffix]; + + yield 'flags' => [ + [...$defaultArgv, '--xdebug'], + new Configuration( + isDryRun: true, + shouldClearCache: true, + paths: ['src'], + showDiffs: false, + isDebug: true, + isComposerBased: true, + isPhpOnly: true, + ), + '--dry-run --clear-cache --no-diffs --composer-based --php --debug --xdebug ' . $suffix, ]; - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + yield 'autoload file' => [ + [...$defaultArgv, '--autoload-file=some/autoload.php'], + new Configuration(paths: ['src']), + "--autoload-file 'some/autoload.php' " . $suffix, ]; - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::DEBUG => true, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + yield 'short autoload file' => [ + [...$defaultArgv, '-a', 'some/autoload.php'], + new Configuration(paths: ['src']), + "--autoload-file 'some/autoload.php' " . $suffix, ]; - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::DEBUG => true, - '--' . Option::XDEBUG => true, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + yield 'value options' => [ + $defaultArgv, + new Configuration( + paths: ['src'], + memoryLimit: '-1', + onlyRules: ['Rector\SomeRector', 'Rector\AnotherRector'], + onlySuffix: 'Controller.php', + filters: ['/Controller/', '*Repository.php'], + maxChanges: 5, + ), + "--only 'Rector\SomeRector' --only 'Rector\AnotherRector' --only-suffix 'Controller.php' --filter '/Controller/,*Repository.php' --memory-limit='-1' --max-changes '5' " . $suffix, ]; - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::MEMORY_LIMIT => '-1', - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --memory-limit='-1' --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", - ]; - - yield [ - [ - self::COMMAND => 'process', - Option::SOURCE => ['src'], - '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, - '--' . Option::PHP => true, - ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --php --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + yield 'explicit config' => [ + [...$defaultArgv, '--config', 'some config/rector.php'], + new Configuration(paths: ['src']), + $suffix . " --config 'some config/rector.php'", ]; } /** * The worker gets PHPStan Turbo through two -d options, when PHPStan ships a binary for this runtime */ - private static function turboExtensionArguments(): string + private function turboExtensionArguments(): string { $extensionPath = TurboExtensionSelector::findExtensionForWorkers(); if ($extensionPath === null) { @@ -218,20 +155,11 @@ private static function turboExtensionArguments(): string return implode(' ', array_map(escapeshellarg(...), $arguments)) . ' '; } - private function cleanUpEmptyQuoteExpectedCommandOutput(string $result): string - { - if ($this->isWindows()) { - return str_replace(' "" ', ' ', $result); - } - - return str_replace(" '' ", ' ', $result); - } - private function normalizeExpectedCommandOutput(string $command): string { - if ($this->isWindows()) { + if (DIRECTORY_SEPARATOR === '\\') { // the turbo arguments are escaped by escapeshellarg() already, keep the single quotes inside them - $turboExtensionArguments = self::turboExtensionArguments(); + $turboExtensionArguments = $this->turboExtensionArguments(); return str_replace( str_replace("'", '"', $turboExtensionArguments), @@ -242,19 +170,4 @@ private function normalizeExpectedCommandOutput(string $command): string return $command; } - - private function prepareProcessCommandDefinition(): InputDefinition - { - // clone the object as we should not modify a object taken from the DI container - $inputDefinition = clone $this->processCommand->getDefinition(); - - // not sure why, but the 1st argument "command" is missing; this is needed for a command name - $arguments = $inputDefinition->getArguments(); - $commandInputArgument = new InputArgument(self::COMMAND, InputArgument::REQUIRED); - $arguments = [$commandInputArgument, ...$arguments]; - - $inputDefinition->setArguments($arguments); - - return $inputDefinition; - } }