Rector - Upgrade your Legacy App to Modern Codebase
Rector is a reconstructor tool - it does instant upgrades and instant refactoring of your code. I mean, why do it manually if 80 % can Rector handle for you?
Rector instantly upgrades and instantly refactors PHP code of your application. It covers many open-source projects and PHP changes itself:
- Rename classes, methods and properties
- Rename partial namespace
- Rename pseudo-namespace to namespace
- Add, replace or remove arguments
- Add arguments or return typehint
- Change visibility of constant, property or method
- 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 --dev
Do 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
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
Or make use of
# rector.yml parameters: autoload_paths: - 'vendor/squizlabs/php_codesniffer/autoload.php' - 'vendor/project-without-composer'
Exclude Paths and Rectors
You can also exclude files or directories (with regex or fnmatch):
# rector.yml parameters: exclude_paths: - '*/src/*/Tests/*'
Do you want to use whole set, except that one rule? Exclude it:
# rector.yml parameters: exclude_rectors: - 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'
A. Prepared Sets
Featured open-source projects have prepared sets. You'll find them in
/config/level or by calling:
Let's say you pick
symfony40 level and you want to upgrade your
# show known changes in Symfony 4.0 vendor/bin/rector process src --level symfony40 --dry-run
# apply vendor/bin/rector process src --level symfony40
Tip: To process just specific subdirectories, you can use fnmatch pattern:
vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run
B. Custom Sets
rector.ymlwith desired Rectors:
services: Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector: $annotation: "inject"
Run on your
vendor/bin/rector process src --dry-run # apply vendor/bin/rector process src
How to Apply Coding Standards?
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.
--with-style option to handle these basic cases:
vendor/bin/rector process src --with-style
More Detailed Documentation
How to Contribute
Just follow 3 rules:
1 feature per pull-request
New feature needs tests
Tests, coding standards and PHPStan checks must pass:
Don you need to fix coding standards? Run:
We would be happy to merge your feature then.
Run Rector in Docker
With this command, you can process your project with rector from docker:
docker run -v $(pwd):/project rector/rector:latest