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 PHP & YAML code of your application, with focus on open-source projects:
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...
...just look at overview of all available Rectors with before/after diffs and configuration examples. It's all you really need to use build your own sets.
Install
Do you have conflicts on composer require?
Install prefixed version with isolated dependencies.
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:
- '%kernel.project_dir%/vendor/squizlabs/php_codesniffer/autoload.php'
- '%kernel.project_dir%/vendor/project-without-composer'
You can also exclude files or directories - use regex or fnmatch:
parameters:
exclude_paths:
- '*/src/*/Tests/*'
How to Reconstruct your Code
A. Prepared Sets
Featured open-source projects have prepared sets. You'll find them in /config/level.
Do you need to upgrade to Symfony 4.0, for example?
-
Run rector on your
/srcdirectory:vendor/bin/rector process src --level symfony40Which is a shortcut for using complete path with
--configoption:vendor/bin/rector process src --config vendor/rector/rector/src/config/level/symfony/symfony40.ymlYou can also use your own config file:
vendor/bin/rector process src --config your-own-config.yml -
Do you want to see the preview of changes first?
Use the
--dry-runoption:vendor/bin/rector process src --level symfony33 --dry-run -
To process just specific subdirectories, you can use fnmatch pattern with
*:vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run -
What levels are on the board?
vendor/bin/rector levels
B. Custom Sets
-
Create
rector.ymlwith desired Rectors:services:
Rector\Nette\Rector\Application\InjectPropertyRector: ~ -
Try Rector on your
/srcdirectory:vendor/bin/rector process src --dry-run -
Apply the changes if you like them:
vendor/bin/rector process src
How to Apply Coding Standards?
Rector uses EasyCodingStandard to improve the code style of changed files, like import namespaces, make 1 empty line between class elements etc.
It's always better to use own project's prepared set, but if you don't have the option yet, 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.