diff --git a/README.md b/README.md index 27716a26e4d5..ee4bbf5da524 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,18 @@ class SomeClass } ``` +### Filter Rectors + +If you have a configuration file for Rector including many sets and Rectors, you might want at times to run only a single Rector from them. The `--only` argument allows that, for example : + +```bash +vendor/bin/rector process --set solid --only "Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector" src/ +``` + +Will only run `Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector`. + +Please note that the backslash in the Rector's fully-qualified class name needs to be properly escaped (by surrounding the string in double quotes). + ### Provide PHP Version By default Rector uses the language features matching your system version of PHP. You can configure it for a different PHP version: diff --git a/src/Application/RectorApplication.php b/src/Application/RectorApplication.php index 47baa30ce443..b5a9efabef11 100644 --- a/src/Application/RectorApplication.php +++ b/src/Application/RectorApplication.php @@ -144,9 +144,9 @@ public function runOnFileInfos(array $fileInfos): void } // active only one rule - if ($this->configuration->getRule() !== null) { - $rule = $this->configuration->getRule(); - $this->enabledRectorsProvider->addEnabledRector($rule); + if ($this->configuration->getOnlyRector() !== null) { + $onlyRector = $this->configuration->getOnlyRector(); + $this->enabledRectorsProvider->addEnabledRector($onlyRector); } // 2. change nodes with Rectors diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index dbf5f7a057fe..17a47e780480 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -37,7 +37,7 @@ final class Configuration /** * @var string|null */ - private $rule; + private $onlyRector; /** * @var bool @@ -59,7 +59,10 @@ public function resolveFromInput(InputInterface $input): void $this->mustMatchGitDiff = (bool) $input->getOption(Option::MATCH_GIT_DIFF); $this->showProgressBar = $this->canShowProgressBar($input); - $this->setRule($input->getOption(Option::OPTION_RULE)); + /** @var string|null $onlyRector */ + $onlyRector = $input->getOption(Option::OPTION_ONLY); + + $this->setOnlyRector($onlyRector); } public function setFirstResolverConfig(?string $firstResolvedConfig): void @@ -102,11 +105,6 @@ public function showProgressBar(): bool return $this->showProgressBar; } - public function getRule(): ?string - { - return $this->rule; - } - public function areAnyPhpRectorsLoaded(): bool { if (PHPUnitEnvironment::isPHPUnitRun()) { @@ -126,23 +124,28 @@ public function mustMatchGitDiff(): bool return $this->mustMatchGitDiff; } - private function canShowProgressBar(InputInterface $input): bool + public function getOnlyRector(): ?string { - $noProgressBar = (bool) $input->getOption(Option::OPTION_NO_PROGRESS_BAR); - - return ! $noProgressBar && $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME; + return $this->onlyRector; } - private function setRule(?string $rule): void + private function setOnlyRector(?string $rector): void { - if ($rule) { - $this->ensureIsValidRectorClass($rule); - $this->rule = $rule; + if ($rector) { + $this->ensureIsValidRectorClass($rector); + $this->onlyRector = $rector; } else { - $this->rule = null; + $this->onlyRector = null; } } + private function canShowProgressBar(InputInterface $input): bool + { + $noProgressBar = (bool) $input->getOption(Option::OPTION_NO_PROGRESS_BAR); + + return ! $noProgressBar && $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME; + } + private function ensureIsValidRectorClass(string $rector): void { // simple check diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index d32b633303c0..7a7b592f93c9 100644 --- a/src/Configuration/Option.php +++ b/src/Configuration/Option.php @@ -44,7 +44,7 @@ final class Option /** * @var string */ - public const OPTION_RULE = 'rule'; + public const OPTION_ONLY = 'only'; /** * @var string diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 2e7307f91a63..6034423ed4ce 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -155,7 +155,12 @@ protected function configure(): void 'Execute only on file(s) matching the git diff.' ); - $this->addOption(Option::OPTION_RULE, 'r', InputOption::VALUE_REQUIRED, 'Run only this single rule.'); + $this->addOption( + Option::OPTION_ONLY, + 'r', + InputOption::VALUE_REQUIRED, + 'Run only one single Rector from the loaded Rectors (in services, sets, etc).' + ); $availableOutputFormatters = $this->outputFormatterCollector->getNames(); $this->addOption(