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:
Rector can:
- 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.
Install
Do you have conflicts on composer require or on run?
- use Docker image or
- install prefixed version with isolated dependencies (currently looking for maintainer)
Extra Autoloading
Rector relies on project and autoloading of its classes. To specify own autoload file, use --autoload-file option:
Or make use of rector.yml config:
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):
parameters:
exclude_paths:
- '*/src/*/Tests/*'
Do you want to use whole set, except that one rule? Exclude it:
parameters:
exclude_rectors:
- 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'
Running Rector
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 /src directory:
vendor/bin/rector process src --level symfony40 --dry-run
vendor/bin/rector process src --level symfony40
Tip: To process just specific subdirectories, you can use fnmatch pattern:
B. Custom Sets
-
Create
rector.ymlwith 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
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.
Just use --with-style option to handle these basic cases:
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:
composer complete-checkDon you need to fix coding standards? Run:
composer fix-cs
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: