From b7aba7b60fa62fd8cb4c9569c66125b4c371987b Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Mon, 13 Jan 2020 11:44:26 +0100 Subject: [PATCH 1/4] Rename --rule argument into --filter-rector, add documentation. --- README.md | 14 ++++++++++++++ src/Application/RectorApplication.php | 6 +++--- src/Configuration/Configuration.php | 24 ++++++++++++------------ src/Configuration/Option.php | 2 +- src/Console/Command/ProcessCommand.php | 7 ++++++- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 27716a26e4d5..1b6978a57891 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,20 @@ 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 `--filter-rector` argument allows that, for example : + +```bash +vendor/bin/rector process --set solid --filter-rector "Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector" src/ +``` + +Will only run `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..633916ec7420 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->getFilteredRector() !== null) { + $filteredRector = $this->configuration->getFilteredRector(); + $this->enabledRectorsProvider->addEnabledRector($filteredRector); } // 2. change nodes with Rectors diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index dbf5f7a057fe..62aa18f1f59d 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -37,7 +37,7 @@ final class Configuration /** * @var string|null */ - private $rule; + private $filteredRector; /** * @var bool @@ -59,7 +59,7 @@ 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)); + $this->setFilteredRector($input->getOption(Option::OPTION_FILTER_RECTOR)); } public function setFirstResolverConfig(?string $firstResolvedConfig): void @@ -102,11 +102,6 @@ public function showProgressBar(): bool return $this->showProgressBar; } - public function getRule(): ?string - { - return $this->rule; - } - public function areAnyPhpRectorsLoaded(): bool { if (PHPUnitEnvironment::isPHPUnitRun()) { @@ -133,13 +128,18 @@ private function canShowProgressBar(InputInterface $input): bool return ! $noProgressBar && $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME; } - private function setRule(?string $rule): void + public function getFilteredRector(): ?string + { + return $this->filteredRector; + } + + private function setFilteredRector(?string $rector): void { - if ($rule) { - $this->ensureIsValidRectorClass($rule); - $this->rule = $rule; + if ($rector) { + $this->ensureIsValidRectorClass($rector); + $this->filteredRector = $rector; } else { - $this->rule = null; + $this->filteredRector = null; } } diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index d32b633303c0..77ca5c1ee72d 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_FILTER_RECTOR = 'filter-rector'; /** * @var string diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 2e7307f91a63..4fcdc990ffde 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_FILTER_RECTOR, + 'r', + InputOption::VALUE_REQUIRED, + 'Run only one single Rector from the loaded Rectors (in services, sets, etc).' + ); $availableOutputFormatters = $this->outputFormatterCollector->getNames(); $this->addOption( From 961ad5493341b27a0dd55a6c16826d8b7c4d4764 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Tue, 14 Jan 2020 20:54:38 +0100 Subject: [PATCH 2/4] Rename option to --only. --- README.md | 6 +++--- src/Application/RectorApplication.php | 6 +++--- src/Configuration/Configuration.php | 14 +++++++------- src/Configuration/Option.php | 2 +- src/Console/Command/ProcessCommand.php | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1b6978a57891..a2d9c44a8986 100644 --- a/README.md +++ b/README.md @@ -204,13 +204,13 @@ 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 `--filter-rector` argument allows that, for example : +Rector from them. The `--only` argument allows that, for example : ```bash -vendor/bin/rector process --set solid --filter-rector "Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector" src/ +vendor/bin/rector process --set solid --only "Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector" src/ ``` -Will only run `FinalizeClassesWithoutChildrenRector`. +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). diff --git a/src/Application/RectorApplication.php b/src/Application/RectorApplication.php index 633916ec7420..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->getFilteredRector() !== null) { - $filteredRector = $this->configuration->getFilteredRector(); - $this->enabledRectorsProvider->addEnabledRector($filteredRector); + 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 62aa18f1f59d..1106c69a5f2f 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -37,7 +37,7 @@ final class Configuration /** * @var string|null */ - private $filteredRector; + private $onlyRector; /** * @var bool @@ -59,7 +59,7 @@ public function resolveFromInput(InputInterface $input): void $this->mustMatchGitDiff = (bool) $input->getOption(Option::MATCH_GIT_DIFF); $this->showProgressBar = $this->canShowProgressBar($input); - $this->setFilteredRector($input->getOption(Option::OPTION_FILTER_RECTOR)); + $this->setOnlyRector($input->getOption(Option::OPTION_ONLY)); } public function setFirstResolverConfig(?string $firstResolvedConfig): void @@ -128,18 +128,18 @@ private function canShowProgressBar(InputInterface $input): bool return ! $noProgressBar && $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME; } - public function getFilteredRector(): ?string + public function getOnlyRector(): ?string { - return $this->filteredRector; + return $this->onlyRector; } - private function setFilteredRector(?string $rector): void + private function setOnlyRector(?string $rector): void { if ($rector) { $this->ensureIsValidRectorClass($rector); - $this->filteredRector = $rector; + $this->onlyRector = $rector; } else { - $this->filteredRector = null; + $this->onlyRector = null; } } diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index 77ca5c1ee72d..7a7b592f93c9 100644 --- a/src/Configuration/Option.php +++ b/src/Configuration/Option.php @@ -44,7 +44,7 @@ final class Option /** * @var string */ - public const OPTION_FILTER_RECTOR = 'filter-rector'; + public const OPTION_ONLY = 'only'; /** * @var string diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 4fcdc990ffde..6034423ed4ce 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -156,7 +156,7 @@ protected function configure(): void ); $this->addOption( - Option::OPTION_FILTER_RECTOR, + Option::OPTION_ONLY, 'r', InputOption::VALUE_REQUIRED, 'Run only one single Rector from the loaded Rectors (in services, sets, etc).' From 7e3697c2bd373a5f9483d1b2ae0ebff1763d3f4e Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Tue, 14 Jan 2020 21:08:05 +0100 Subject: [PATCH 3/4] Fix ECS/Phpstan inspections. --- src/Configuration/Configuration.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 1106c69a5f2f..17a47e780480 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -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->setOnlyRector($input->getOption(Option::OPTION_ONLY)); + /** @var string|null $onlyRector */ + $onlyRector = $input->getOption(Option::OPTION_ONLY); + + $this->setOnlyRector($onlyRector); } public function setFirstResolverConfig(?string $firstResolvedConfig): void @@ -121,13 +124,6 @@ public function mustMatchGitDiff(): bool return $this->mustMatchGitDiff; } - 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; - } - public function getOnlyRector(): ?string { return $this->onlyRector; @@ -143,6 +139,13 @@ private function setOnlyRector(?string $rector): void } } + 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 From 7162243f4765776910eb11632d02d5427c6e32b0 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Tue, 14 Jan 2020 21:46:19 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a2d9c44a8986..ee4bbf5da524 100644 --- a/README.md +++ b/README.md @@ -203,8 +203,7 @@ 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 : +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/ @@ -212,8 +211,7 @@ vendor/bin/rector process --set solid --only "Rector\SOLID\Rector\Class_\Finaliz 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). +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