Run Single Rule

Sometimes, we want to apply just one rule at a time. But modifying rector.php config over and over again is tedious and can lead to mistakes.

To run single rule, we can use --only CLI option. Just add quoted class name:

vendor/bin/rector --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"

Mind the quotes, as both Win and *nix system require them to pick up the full class name string (both single and double are supported). Rector accepts:

  • the fully qualified class name: --only="Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector"
  • the short class name, if it is unique among registered rules: --only=RemoveUnusedPrivateMethodRector
  • the option repeated for several rules: --only=FirstRector --only=SecondRector

Rule must be registered in rector.php

We can run only the rules explicitly registered in rector.php or loaded sets. This way configured rules load explicit configuration the same way we would run the rule by calling vendor/bin/rector.

To narrow the run by files instead of rules, see Filter Files. All options are on the CLI options page.