Rector is a reconstructor tool - it does instant upgrades and instant refactoring of your code. Why refactor manually if Rector can handle 80% of the task for you?
Rector grows faster with your help, the more you help the more work it saves you. Check out Rector's Patreon. One-time donations are welcome through PayPal.
Thank you:
Rector instantly upgrades and instantly refactors the PHP code of your application.
It supports all versions of PHP from 5.2 and many open-source projects:
- Rename classes, methods, properties, namespaces or constants
- Complete parameter, var or return type declarations based on static analysis of your code
- Upgrade your code from PHP 5.3 to PHP 7.4
- Migrate your project from Nette to Symfony
- Complete PHP 7.4 property type declarations
- Refactor Laravel facades to dependency injection
- And much more...
Rector uses nikic/php-parser, that build on technology called abstract syntax tree) technology* (AST). AST doesn't care about spaces and produces mall-formatted code. That's why your project needs to have coding standard tool and set of rules, so it can make refactored nice and shiny again.
Don't have any coding standard tool? Add EasyCodingStandard and use prepared ecs-after-rector.yaml
set.
composer require rector/rector --dev
- Having conflicts during
composer require
? → Use the Rector Prefixed - Using a different PHP version than Rector supports? → Use the Docker image
Featured open-source projects have prepared sets. You can find them in /config/set
or by running:
vendor/bin/rector sets
Let's say you pick the symfony40
set and you want to upgrade your /src
directory:
# show a list of known changes in Symfony 4.0
vendor/bin/rector process src --set symfony40 --dry-run
Rector will show you diff of files that it would change. To make the changes, run same command without --dry-run
:
# apply upgrades to your code
vendor/bin/rector process src --set symfony40
Some sets, such as code-quality
can be used on a regular basis. The best practise is to use config over CLI, here in sets
parameter:
# rector.yaml
parameters:
sets:
- code-quality
In the end, it's best to combine few of basic sets and drop particular rules that you want to try:
# rector.yaml
parameters:
sets:
- code-quality
services:
Rector\Php74\Rector\Property\TypedPropertyRector: null
Then let Rector refactor your code:
vendor/bin/rector process src
👍
Note: rector.yaml
is loaded by default. For different location, use --config
option.
If you're annoyed by repeating paths in arguments, you can move them to config instead:
# rector.yaml
parameters:
paths:
- 'src'
- 'tests'
Rector relies on whatever autoload setup the project it is fixing is using by using the Composer autoloader as default. To specify your own autoload file, use --autoload-file
option:
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
Or use a rector.yaml
configuration file:
# rector.yaml
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.yaml
parameters:
exclude_paths:
- '*/src/*/Tests/*'
You can use a whole set, except 1 rule:
# rector.yaml
parameters:
exclude_rectors:
- 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'
For in-file exclusion, use @noRector \FQN name
annotation:
class SomeClass
{
/**
* @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector
*/
public function foo()
{
/** @noRector \Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector */
round(1 + 0);
}
}
By default Rector uses the language features matching your system version of PHP. You can configure it for a different PHP version:
# rector.yaml
parameters:
php_version_features: '7.2' # your version is 7.3
FQN classes are not imported by default. If you don't want to do it manually after every Rector run, enable it by:
# rector.yaml
parameters:
auto_import_names: true
You can also fine-tune how these imports are processed:
# rector.yaml
parameters:
# this will not import root namespace classes, like \DateTime or \Exception
import_short_classes: false
# this will not import classes used in PHP DocBlocks, like in /** @var \Some\Class */
import_doc_blocks: false
Execution can be limited to changed files using the process
option --match-git-diff
.
This option will filter the files included by the configuration, creating an intersection with the files listed in git diff
.
vendor/bin/rector process src --match-git-diff
This option is useful in CI with pull-requests that only change few files.
To work with some Symfony rules, you now need to link your container XML file
# rector.yaml
parameters:
# path to load services from
symfony_container_xml_path: 'var/cache/dev/AppKernelDevDebugContainer.xml'
- All Rectors Overview
- Create own Rule
- Generate Rector from Recipe
- How Does Rector Work?
- PHP Parser Nodes Overview
- Add Checkstyle with your CI
You can run Rector on your project using Docker:
docker run --rm -v $(pwd):/project rector/rector:latest process /project/src --set symfony40 --dry-run
# Note that a volume is mounted from `pwd` (the current directory) into `/project` which can be accessed later.
Using rector.yaml
:
docker run --rm -v $(pwd):/project rector/rector:latest process /project/app \
--config /project/rector.yaml \
--autoload-file /project/vendor/autoload.php \
--dry-run
Do you use Rector to upgrade your code? Add it here: