Rector requires PHP 7.2+. It can work with PHP 5.x and 8.x code. It works best with OOP, typed code with separated PHP and template layers.
composer require rector/rector --dev
You can run it from your bin directory:
vendor/bin/rector
Rector works with rector.php
config file. You can create it manually, or Rector handle it for you:
vendor/bin/rector
No "rector.php" config found. Should we generate it for you? [yes]:
> yes
[OK] The config is added now. Re-run command to make Rector do the work!
In rector.php
you can define paths, rules and sets you want to run on your code:
<?php
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPreparedSets(deadCode: true);
If you're on PHP 7.x, you can use
withSets()
instead, so for thedeadCode
set, you can define:- ->withPreparedSets(deadCode: true); + ->withSets([SetList::DEAD_CODE]);
To see preview of suggested changed, run process
command with --dry-run
option:
vendor/bin/rector process --dry-run
To make changes happen, run bare command:
vendor/bin/rector process