Rector is a reconstructor tool - it does instant upgrades and instant refactoring of your code. Why doing it manually if 80% Rector can handle for you?
Rector grows faster with your help, the more you help the more work it saves you. Check out Rector's Patreon. One-time donation is welcomed trough PayPal.
Rector instantly upgrades and instantly refactors PHP code of your application. It covers many open-source projects and PHP changes itself:
- Rename classes, methods, properties, namespaces or constants
- Complete parameter, var or return type declarations based on static analysis of your code
- Upgrade your code from PHP 5.3 to PHP 7.4
- Migrate your project from Nette to Symfony
- Complete PHP 7.4 property type declarations
- Turn Laravel static to Dependency Injection
- And much more...
...look at overview of all available Rectors with before/after diffs and configuration examples. You can use them to build your own sets.
composer require rector/rector --devDo you have conflicts on composer require or on run?
- use Docker image or
- install prefixed version with isolated dependencies (currently looking for maintainer)
Rector relies on project and autoloading of its classes. To specify own autoload file, use --autoload-file option:
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.phpOr make use of rector.yaml config:
# rector.yamlparameters:
autoload_paths:
- 'vendor/squizlabs/php_codesniffer/autoload.php'
- 'vendor/project-without-composer'You can also exclude files or directories (with regex or fnmatch):
# rector.yamlparameters:
exclude_paths:
- '*/src/*/Tests/*'Do you want to use whole set, except that one rule? Exclude it:
# rector.yamlparameters:
exclude_rectors:
- 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'By default Rector uses language features of your PHP version. If you you want to use different PHP version than your system, put it in config:
parameters:
php_version_features: '7.2'# your version 7.3Featured open-source projects have prepared sets. You'll find them in /config/level or by calling:
vendor/bin/rector levelsLet's say you pick symfony40 level and you want to upgrade your /src directory:
# show known changes in Symfony 4.0
vendor/bin/rector process src --level symfony40 --dry-run# apply
vendor/bin/rector process src --level symfony40Create
rector.yamlwith desired Rectors:services: Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector: $annotation: "inject"
Run on your
/srcdirectory:vendor/bin/rector process src --dry-run # apply vendor/bin/rector process src
AST that Rector uses doesn't deal with coding standards very well, so it's better to let coding standard tools do that. Your project doesn't have one? Rector ships with EasyCodingStandard set that covers namespaces import, 1 empty line between class elements etc.
Just use --with-style option to handle these basic cases:
vendor/bin/rector process src --with-styleFirst, make sure it's not covered by any existing Rectors yet.
Let's say we want to change method calls from set* to change*.
$user = new User();
-$user->setPassword('123456');+$user->changePassword('123456');Create class that extends Rector\Rector\AbstractRector. It has useful methods like checking node type and name. Just run $this-> and let PHPStorm show you all possible methods.
<?phpdeclare(strict_types=1);
namespaceApp\Rector;
useNette\Utils\Strings;
usePhpParser\Node;
usePhpParser\Node\Identifier;
usePhpParser\Node\Expr\MethodCall;
useRector\Rector\AbstractRector;
useRector\RectorDefinition\CodeSample;
useRector\RectorDefinition\RectorDefinition;
finalclass MyFirstRector extends AbstractRector
{
publicfunctiongetDefinition(): RectorDefinition
{
// what does this do?// minimalistic before/after sample - to explain in codereturnnewRectorDefinition('Change method calls from set* to change*.', [
newCodeSample('$user->setPassword("123456");', '$user->changePassword("123456");')
]);
}
/** * @return string[] */publicfunctiongetNodeTypes(): array
{
// what node types we look for?// pick any node from https://github.com/rectorphp/rector/blob/master/docs/NodesOverview.mdreturn [MethodCall::class];
}
/** * @param MethodCall $node - we can add "MethodCall" type here, because only this node is in "getNodeTypes()" */publicfunctionrefactor(Node$node): ?Node
{
// we only care about "set*" method namesif (! $this->isName($node, 'set*')) {
// return null to skip itreturnnull;
}
$methodCallName = $this->getName($node);
$newMethodCallName = Strings::replace($methodCallName, '#^set#', 'change');
$node->name = newIdentifier($newMethodCallName);
// return $node if you modified itreturn$node;
}
}# rector.yamlservices:
App\Rector\MyFirstRector: ~# see the diff first
vendor/bin/rector process src --dry-run
# if it's ok, apply
vendor/bin/rector process srcThat's it!
Just follow 3 rules:
1 feature per pull-request
New feature needs tests
Tests, coding standards and PHPStan checks must pass:
composer complete-check
Don you need to fix coding standards? Run:
composer fix-cs
We would be happy to merge your feature then.
With this command, you can process your project with rector from docker:
docker run -v $(pwd):/project rector/rector:latest process /project/src --level symfony40 --dry-run
# Note that a volume is mounted from `pwd` into `/project` which can be accessed later.Using rector.yaml:
docker run -v $(pwd):/project rector/rector:latest process /project/app --config /project/rector.yaml --autoload-file /project/vendor/autoload.php --dry-runDo you use Rector to upgrade your code? Share it here:
- drupal8-rector/drupal8-rector by @mxr576









