Set Lists
Prepared Sets
You can use particular single rules, or whole list of rules, called "set lists":
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
naming: true,
privatization: true,
typeDeclarations: true,
rectorPreset: true,
// ...
);
That way you can include group of rules that focus on certain topic, e.g. in this case on dead detection. It makes config small and clear.
Try autocomplete in your IDE to see all available prepared sets.
PHP Sets
The best practise is to use PHP version defined in composer.json. Rector will automatically pick it up with empty ->withPhpSets() method:
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPhpSets();
Are you on a legacy project and want to upgrade gradually? Use the ->withPhpLevel() option and raise the level one step at a time:
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPhpLevel(20);
Each level adds the next PHP rule on top of the previous ones. Start low, run Rector, review the diff, then bump the number - so you upgrade in small, reviewable steps instead of all at once.
If you're on PHP 8+, you can use
->withPhpSets()with named arguments:- ->withSets([SetList::PHP_74]); + ->withPhpSets(php80: true);
That way you can use all the sets that are needed to upgrade your code to the desired PHP version in single line.
Community or External Sets
How can I use Rector with community sets or my custom one?
<?php
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
return RectorConfig::configure()
->withSets([
DoctrineSetList::DOCTRINE_CODE_QUALITY,
__DIR__.'/config/rector-custom-set.php'
]);