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 --dev
Do 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.php
Or 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 levels
Let'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 symfony40
Tip: To process just specific subdirectories, you can use fnmatch pattern:
vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run
-
Create
rector.yml
with desired Rectors:services: Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector: $annotation: "inject"
-
Run on your
/src
directory: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-style
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