2021-05-10 18:06:54 +00:00
# Rector - Instant Upgrades and Automated Refactoring
2017-07-15 19:20:20 +02:00
2021-05-10 18:06:54 +00:00
[![Downloads ](https://img.shields.io/packagist/dt/rector/rector.svg?style=flat-square )](https://packagist.org/packages/rector/rector)
2020-10-29 10:44:07 +01:00
2021-05-10 18:06:54 +00:00
< br >
2020-10-29 10:44:07 +01:00
2022-01-25 08:37:12 +00:00
Rector instantly upgrades and refactors the PHP code of your application. It can help you in 2 major areas:
2021-05-10 18:06:54 +00:00
2021-05-23 15:11:41 +00:00
### 1. Instant Upgrades
2021-05-10 18:06:54 +00:00
2024-12-10 10:32:16 +00:00
Rector now supports upgrades from PHP 5.3 to 8.4 and major open-source projects like [Symfony ](https://github.com/rectorphp/rector-symfony ), [PHPUnit ](https://github.com/rectorphp/rector-phpunit ), and [Doctrine ](https://github.com/rectorphp/rector-doctrine ). Do you want to **be constantly on the latest PHP and Framework without effort** ?
2021-05-10 18:06:54 +00:00
2021-05-23 15:11:41 +00:00
Use Rector to handle **instant upgrades** for you.
2021-05-10 18:06:54 +00:00
2021-05-23 15:11:41 +00:00
### 2. Automated Refactoring
2021-05-10 18:06:54 +00:00
2022-01-25 10:05:47 +00:00
Do you have code quality you need, but struggle to keep it with new developers in your team? Do you want to see smart code-reviews even when every senior developers sleeps?
2021-05-10 18:06:54 +00:00
2021-05-23 15:11:41 +00:00
Add Rector to your CI and let it **continuously refactor your code** and keep the code quality high.
2021-05-10 18:06:54 +00:00
2023-04-17 11:35:06 +00:00
Read our [blogpost ](https://getrector.com/blog/new-setup-ci-command-to-let-rector-work-for-you ) to see how to set up automated refactoring.
2017-07-15 19:20:20 +02:00
## Install
```bash
2018-10-07 01:28:57 +08:00
composer require rector/rector --dev
2017-07-15 19:20:20 +02:00
```
2021-05-10 18:06:54 +00:00
## Running Rector
2022-01-25 08:37:12 +00:00
There are 2 main ways to use Rector:
2021-05-10 18:06:54 +00:00
- a *single rule* , to have the change under control
- or group of rules called *sets*
To use them, create a `rector.php` in your root directory:
2020-10-29 10:44:07 +01:00
```bash
2023-06-22 10:02:01 +00:00
vendor/bin/rector
2021-05-10 18:06:54 +00:00
```
2020-10-29 10:44:07 +01:00
2021-05-10 18:06:54 +00:00
And modify it:
```php
2022-04-12 11:52:08 +00:00
use Rector\Config\RectorConfig;
2022-12-03 11:10:19 +00:00
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
2021-05-10 18:06:54 +00:00
2024-03-03 02:43:03 +00:00
return RectorConfig::configure()
2022-12-03 11:10:19 +00:00
// register single rule
2024-03-03 02:43:03 +00:00
->withRules([
TypedPropertyFromStrictConstructorRector::class
])
// here we can define, what prepared sets of rules will be applied
->withPreparedSets(
deadCode: true,
codeQuality: true
);
2021-05-10 18:06:54 +00:00
```
Then dry run Rector:
```bash
2020-08-24 23:25:26 +02:00
vendor/bin/rector process src --dry-run
2021-05-10 18:06:54 +00:00
```
Rector will show you diff of files that it *would* change. To *make* the changes, drop `--dry-run` :
2019-11-08 17:49:03 +01:00
2021-05-10 18:06:54 +00:00
```bash
2020-05-03 14:19:09 +02:00
vendor/bin/rector process src
```
2021-05-10 18:06:54 +00:00
2022-12-03 11:10:19 +00:00
## Documentation
2021-05-10 18:06:54 +00:00
2023-08-06 15:47:57 +00:00
* Find [full documentation here ](https://getrector.com/documentation/ ).
2024-08-08 13:54:22 +00:00
* [Explore Rector Rules ](https://getrector.com/find-rule )
2021-07-21 08:58:31 +00:00
2021-05-10 18:06:54 +00:00
< br >
2022-12-03 11:10:19 +00:00
## Learn Faster with a Book
2021-05-10 18:06:54 +00:00
2022-12-03 11:10:19 +00:00
Are you curious, how Rector works internally, how to create your own rules and test them and why Rector was born?
Read [Rector - The Power of Automated Refactoring ](https://leanpub.com/rector-the-power-of-automated-refactoring ) that will take you step by step through the Rector setup and how to create your own rules.
2021-05-10 18:06:54 +00:00
< br >
2022-12-03 11:10:19 +00:00
## Empowered by Community :heart:
2022-08-10 09:53:54 +00:00
The Rector community is powerful thanks to active maintainers who take care of Rector sets for particular projects.
Among there projects belong:
* [palantirnet/drupal-rector ](https://github.com/palantirnet/drupal-rector )
* [craftcms/rector ](https://github.com/craftcms/rector )
* [FriendsOfShopware/shopware-rector ](https://github.com/FriendsOfShopware/shopware-rector )
* [sabbelasichon/typo3-rector ](https://github.com/sabbelasichon/typo3-rector )
* [sulu/sulu-rector ](https://github.com/sulu/sulu-rector )
* [efabrica-team/rector-nette ](https://github.com/efabrica-team/rector-nette )
* [Sylius/SyliusRector ](https://github.com/Sylius/SyliusRector )
2022-08-10 10:06:27 +00:00
* [CoditoNet/rector-money ](https://github.com/CoditoNet/rector-money )
* [laminas/laminas-servicemanager-migration ](https://github.com/laminas/laminas-servicemanager-migration )
2022-10-03 07:25:38 +00:00
* [cakephp/upgrade ](https://github.com/cakephp/upgrade )
2022-11-02 14:48:20 +00:00
* [driftingly/rector-laravel ](https://github.com/driftingly/rector-laravel )
2024-01-22 12:43:37 +00:00
* [contao/contao-rector ](https://github.com/contao/contao-rector )
2024-04-01 16:51:34 +00:00
* [php-static-analysis/rector-rule ](https://github.com/php-static-analysis/rector-rule )
2022-08-10 09:53:54 +00:00
< br >
2022-08-10 10:06:27 +00:00
## Hire us to get Job Done :muscle:
2021-05-23 15:11:41 +00:00
2023-08-06 15:47:57 +00:00
Rector is a tool that [we develop ](https://getrector.com/ ) and share for free, so anyone can automate their refactoring. But not everyone has dozens of hours to understand complexity of abstract-syntax-tree in their own time. **That's why we provide commercial support - to save your time** .
2021-05-23 15:11:41 +00:00
2023-08-06 15:47:57 +00:00
Would you like to apply Rector on your code base but don't have time for the struggle with your project? [Hire us ](https://getrector.com/contact ) to get there faster.
2021-05-23 15:11:41 +00:00
< br >
2021-05-10 18:06:54 +00:00
## How to Contribute
2022-01-21 10:58:31 +00:00
See [the contribution guide ](/CONTRIBUTING.md ) or go to development repository [rector/rector-src ](https://github.com/rectorphp/rector-src ).
2021-05-10 18:06:54 +00:00
< br >
2021-05-23 15:11:41 +00:00
## Debugging
2021-05-10 18:06:54 +00:00
You can use `--debug` option, that will print nested exceptions output:
```bash
vendor/bin/rector process src/Controller --dry-run --debug
```
Or with Xdebug:
1. Make sure [Xdebug ](https://xdebug.org/ ) is installed and configured
2. Add `--xdebug` option when running Rector
```bash
vendor/bin/rector process src/Controller --dry-run --xdebug
```
2023-02-07 13:11:11 +00:00
To assist with simple debugging Rector provides 2 helpers to pretty-print AST-nodes:
```php
use PhpParser\Node\Scalar\String_;
$node = new String_('hello world!');
// prints node to string, as PHP code displays it
print_node($node);
```
< br >
2021-05-10 18:06:54 +00:00
## Known Drawbacks
2023-09-15 07:10:20 +00:00
* Rector uses [nikic/php-parser ](https://github.com/nikic/PHP-Parser/ ), built on technology called an *abstract syntax tree* (AST). An AST doesn't know about spaces and when written to a file it produces poorly formatted code in both PHP and docblock annotations.
2024-01-02 02:40:38 +00:00
* Rector in parallel mode will work most of the times for most OS. On Windows, you may encounter issues unresolvable despite of following the [Troubleshooting Parallel ](https://getrector.com/documentation/troubleshooting-parallel ) guide. In such case, check if you are using Powershell 7 (pwsh). Change your terminal to command prompt (cmd) or bash for Windows.
2023-07-25 11:19:04 +00:00
2021-05-10 18:06:54 +00:00
### How to Apply Coding Standards?
2023-07-25 11:19:04 +00:00
**Your project needs to have a coding standard tool** and a set of formatting rules, so it can make Rector's output code nice and shiny again.
2021-05-10 18:06:54 +00:00
2021-06-17 09:46:43 +00:00
We're using [ECS ](https://github.com/symplify/easy-coding-standard ) with [this setup ](https://github.com/rectorphp/rector-src/blob/main/ecs.php ).
2023-07-25 11:19:04 +00:00
### May cause unexpected output on File with mixed PHP+HTML content
2024-12-10 10:32:16 +00:00
When you apply changes to files with PHP + HTML content, you may need to manually verify the changed file after apply the changes.