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...
...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?
Install prefixed version with isolated dependencies.
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.yml config:
# rector.yml
parameters:
autoload_paths:
- 'vendor/squizlabs/php_codesniffer/autoload.php'
- 'vendor/project-without-composer'You can also exclude files or directories (with regex or fnmatch):
# rector.yml
parameters:
exclude_paths:
- '*/src/*/Tests/*'Featured 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 symfony40Tip: To process just specific subdirectories, you can use fnmatch pattern:
vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run-
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
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-styleJust 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.