Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/Application/RectorApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class Configuration
/**
* @var string|null
*/
private $rule;
private $onlyRector;

/**
* @var bool
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class Option
/**
* @var string
*/
public const OPTION_RULE = 'rule';
public const OPTION_ONLY = 'only';

/**
* @var string
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down