2019-10-24 17:59:42 -07:00
# Rector - Upgrade Your Legacy App to a Modern Codebase
2017-07-15 19:20:20 +02:00
2018-09-11 20:46:11 +02:00
Rector is a **rec**onstruc**tor** tool - it does **instant upgrades** and **instant refactoring** of your code.
2020-03-21 11:41:11 +01:00
Why refactor manually if Rector can handle 80% of the task for you?
2018-02-02 17:51:41 +01:00
2018-11-09 14:45:14 +01:00
[![Coverage Status ](https://img.shields.io/coveralls/rectorphp/rector/master.svg?style=flat-square )](https://coveralls.io/github/rectorphp/rector?branch=master)
2018-07-02 15:02:27 +02:00
[![Downloads ](https://img.shields.io/packagist/dt/rector/rector.svg?style=flat-square )](https://packagist.org/packages/rector/rector)
2020-02-10 00:35:39 +01:00
[![SonarCube ](https://img.shields.io/badge/SonarCube_Debt-%3C25-brightgreen.svg?style=flat-square )](https://sonarcloud.io/dashboard?id=rectorphp_rector)
2017-07-15 19:20:20 +02:00
2020-02-21 15:26:02 +01:00
< br >
2020-04-14 21:52:27 +02:00
- **[Online DEMO ](https://getrector.org/demo )**
2020-05-03 14:32:03 +02:00
- [Overview 500+ Rector Rules ](/docs/rector_rules_overview.md )
2020-02-21 15:26:02 +01:00
2020-05-03 14:32:03 +02:00
---
2020-02-13 17:13:49 +01:00
2019-10-19 12:42:36 +02:00
![Rector-showcase ](docs/images/rector-showcase-var.gif )
2018-01-13 16:01:31 +01:00
2019-04-29 23:42:11 +02:00
< br >
## Sponsors
Rector grows faster with your help, the more you help the more work it saves you.
2020-03-21 11:41:11 +01:00
Check out [Rector's Patreon ](https://www.patreon.com/rectorphp ). One-time donations are welcome [through PayPal ](https://www.paypal.me/rectorphp ).
2019-04-29 23:42:11 +02:00
2019-08-19 15:49:41 +02:00
Thank you:
2019-08-19 15:50:06 +02:00
< a href = "https://spaceflow.io/en" > < img src = "/docs/images/spaceflow.png" > < / a >
2019-08-19 15:49:41 +02:00
2019-04-29 23:42:11 +02:00
< br >
## Open-Source First
2020-05-03 14:32:37 +02:00
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:
2017-07-15 19:20:20 +02:00
2018-10-21 11:00:39 +02:00
< br >
2017-10-30 16:00:28 +01:00
< p align = "center" >
2019-06-08 22:41:52 +02:00
< a href = "/config/set/php" > < img src = "/docs/images/php.png" > < / a >
2019-10-20 16:46:52 +02:00
< img src = "/docs/images/space.png" width = 40 >
2019-06-08 22:41:52 +02:00
< a href = "/config/set/symfony" > < img src = "/docs/images/symfony.png" > < / a >
2019-10-20 16:46:52 +02:00
< img src = "/docs/images/space.png" width = 40 >
2019-10-20 16:47:40 +02:00
< a href = "/config/set/laravel" > < img src = "/docs/images/laravel.png" > < / a >
< br >
< a href = "/config/set/cakephp" > < img src = "/docs/images/cakephp.png" > < / a >
< img src = "/docs/images/space.png" width = 40 >
2019-11-04 01:48:08 +01:00
< a href = "/config/set/phpunit" > < img src = "/docs/images/phpunit.png" > < / a >
2019-10-20 16:46:52 +02:00
< img src = "/docs/images/space.png" width = 40 >
2019-06-08 22:41:52 +02:00
< a href = "/config/set/twig" > < img src = "/docs/images/twig.png" > < / a >
2020-05-24 09:29:33 -04:00
< img src = "/docs/images/space.png" width = 40 >
< a href = "https://github.com/palantirnet/drupal-rector/tree/master/config/drupal-8" > < img src = "/docs/images/drupal.png" alt = "Drupal Rector rules" > < / a >
2017-10-30 16:00:28 +01:00
< / p >
2017-08-20 17:40:19 +02:00
2018-10-21 11:00:39 +02:00
< br >
2019-10-24 17:59:42 -07:00
## What Can Rector Do for You?
2018-08-01 22:29:01 +02:00
2019-04-29 23:42:11 +02:00
- Rename classes, methods, properties, namespaces or constants
2020-01-15 08:14:05 +05:30
- Complete [parameter, var or return type declarations ](https://www.tomasvotruba.com/blog/2019/01/03/how-to-complete-type-declarations-without-docblocks-with-rector/ ) based on static analysis of your code
2019-04-29 23:42:11 +02:00
- Upgrade your code from PHP 5.3 to PHP 7.4
2020-01-14 22:20:38 +01:00
- [Migrate your project from Nette to Symfony ](https://www.tomasvotruba.com/blog/2019/02/21/how-we-migrated-from-nette-to-symfony-in-3-weeks-part-1/ )
- [Complete PHP 7.4 property type declarations ](https://www.tomasvotruba.com/blog/2018/11/15/how-to-get-php-74-typed-properties-to-your-code-in-few-seconds/ )
- [Refactor Laravel facades to dependency injection ](https://www.tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/ )
2017-12-28 17:58:35 -02:00
- And much more...
2017-07-15 19:20:20 +02:00
2019-07-09 17:05:10 +02:00
## How to Apply Coding Standards?
2020-05-03 14:47:06 +02:00
Rector uses [nikic/php-parser ](https://github.com/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.
2019-07-09 18:17:03 +02:00
2020-05-03 14:47:06 +02:00
Don't have any coding standard tool? Add [EasyCodingStandard ](https://github.com/Symplify/EasyCodingStandard ) and use prepared [`ecs-after-rector.yaml` ](/ecs-after-rector.yaml ) set.
2019-11-08 18:02:29 +01:00
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
```
2020-05-03 14:32:03 +02:00
- Having conflicts during `composer require` ? → Use the [Rector Prefixed ](https://github.com/rectorphp/rector-prefixed )
- Using a different PHP version than Rector supports? → Use the [Docker image ](#run-rector-in-docker )
2018-01-06 16:50:22 +01:00
2019-11-08 17:49:03 +01:00
## Running Rector
2020-05-03 14:19:09 +02:00
### A. Prepared Sets
2019-11-08 17:49:03 +01:00
Featured open-source projects have **prepared sets** . You can find them in [`/config/set` ](/config/set ) or by running:
```bash
vendor/bin/rector sets
```
2020-01-10 09:43:15 +01:00
Let's say you pick the [`symfony40` ](/config/set/symfony ) set and you want to upgrade your `/src` directory:
2019-11-08 17:49:03 +01:00
```bash
# show a list of known changes in Symfony 4.0
vendor/bin/rector process src --set symfony40 --dry-run
```
2020-05-03 14:19:09 +02:00
Rector will show you diff of files that it *would* change. To *make* the changes, run same command without `--dry-run` :
2019-11-08 17:49:03 +01:00
```bash
# apply upgrades to your code
vendor/bin/rector process src --set symfony40
```
2020-05-03 14:19:09 +02:00
Some sets, such as [`code-quality` ](/config/set/code-quality ) can be used on a regular basis. **The best practise is to use config over CLI** , here in `sets` parameter:
2019-11-08 17:49:03 +01:00
```yaml
# rector.yaml
2019-11-22 16:10:19 +01:00
parameters:
sets:
2020-05-03 14:19:09 +02:00
- code-quality
2019-11-08 17:49:03 +01:00
```
2020-05-03 14:19:09 +02:00
### B. Standalone Rules
2019-11-08 17:49:03 +01:00
2020-05-03 14:22:23 +02:00
In the end, it's best to combine few of basic sets and drop [particular rules ](/docs/rector_rules_overview.md ) that you want to try:
2019-11-08 17:49:03 +01:00
2020-05-03 14:19:09 +02:00
```yaml
# rector.yaml
parameters:
sets:
- code-quality
2019-11-08 17:49:03 +01:00
2020-05-03 14:19:09 +02:00
services:
Rector\Php74\Rector\Property\TypedPropertyRector: null
```
2019-11-08 17:49:03 +01:00
2020-05-03 14:30:47 +02:00
Then let Rector refactor your code:
2019-11-08 17:49:03 +01:00
2020-05-03 14:19:09 +02:00
```bash
vendor/bin/rector process src
```
:+1:
< br >
*Note: `rector.yaml` is loaded by default. For different location, use `--config` option.*
2019-11-08 17:49:03 +01:00
## Features
2020-05-13 18:17:21 +02:00
### Paths
If you're annoyed by repeating paths in arguments, you can move them to config instead:
```yaml
# rector.yaml
parameters:
paths:
- 'src'
- 'tests'
```
2018-02-08 15:25:45 +01:00
### Extra Autoloading
2020-03-21 11:41:11 +01:00
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:
2018-02-09 01:16:54 +01:00
2018-02-08 15:25:45 +01:00
```bash
2018-02-08 15:27:14 +01:00
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
2018-02-08 15:25:45 +01:00
```
2020-03-21 11:41:11 +01:00
Or use a `rector.yaml` configuration file:
2018-04-13 21:14:07 +02:00
```yaml
2019-02-03 18:46:45 +01:00
# rector.yaml
2018-04-13 21:14:07 +02:00
parameters:
2018-09-26 15:03:04 +08:00
autoload_paths:
2018-11-09 14:58:49 +01:00
- 'vendor/squizlabs/php_codesniffer/autoload.php'
- 'vendor/project-without-composer'
2018-04-13 21:14:07 +02:00
```
2019-11-08 13:08:42 +01:00
### Exclude Paths and Rectors
2019-01-18 12:41:07 +01:00
2018-11-09 14:58:49 +01:00
You can also **exclude files or directories** (with regex or [fnmatch ](http://php.net/manual/en/function.fnmatch.php )):
2018-08-29 22:45:09 +02:00
```yaml
2019-02-03 18:46:45 +01:00
# rector.yaml
2018-08-29 22:45:09 +02:00
parameters:
exclude_paths:
- '*/src/*/Tests/*'
```
2020-05-13 18:17:21 +02:00
You can use a whole set, except 1 rule:
2019-01-18 12:41:07 +01:00
```yaml
2019-02-03 18:46:45 +01:00
# rector.yaml
2019-01-18 12:41:07 +01:00
parameters:
exclude_rectors:
- 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'
```
2020-05-13 18:17:21 +02:00
For in-file exclusion, use `@noRector \FQN name` annotation:
2019-12-29 13:00:38 +01:00
```php
class SomeClass
{
/**
* @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector
*/
public function foo()
{
/** @noRector \Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector */
round(1 + 0);
}
}
```
2019-11-08 13:08:42 +01:00
### Provide PHP Version
2019-10-24 17:59:42 -07:00
By default Rector uses the language features matching your system version of PHP. You can configure it for a different PHP version:
2019-02-23 14:03:07 +01:00
```yaml
2019-10-12 17:51:07 +02:00
# rector.yaml
2019-02-23 14:03:07 +01:00
parameters:
2019-10-24 17:59:42 -07:00
php_version_features: '7.2' # your version is 7.3
2019-02-23 14:03:07 +01:00
```
2019-11-08 13:08:42 +01:00
### Import Use Statements
2020-04-04 22:59:33 +02:00
FQN classes are not imported by default. If you don't want to do it manually after every Rector run, enable it by:
2019-10-12 17:51:07 +02:00
```yaml
# rector.yaml
parameters:
2019-12-20 07:40:36 +01:00
auto_import_names: true
2019-10-12 17:51:07 +02:00
```
2019-12-20 07:40:36 +01:00
You can also fine-tune how these imports are processed:
2019-12-12 16:05:12 +01:00
```yaml
# rector.yaml
parameters:
2019-12-20 07:40:36 +01:00
# 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
2019-12-12 16:05:12 +01:00
```
2019-11-23 21:30:52 +01:00
### Limit Execution to Changed Files
2019-11-24 01:00:46 +01:00
Execution can be limited to changed files using the `process` option `--match-git-diff` .
2019-11-23 21:30:52 +01:00
This option will filter the files included by the configuration, creating an intersection with the files listed in `git diff` .
2019-11-24 01:00:46 +01:00
```bash
vendor/bin/rector process src --match-git-diff
```
This option is useful in CI with pull-requests that only change few files.
2019-11-23 21:30:52 +01:00
2019-12-16 15:45:37 +01:00
### Symfony Container
To work with some Symfony rules, you now need to link your container XML file
```yaml
# rector.yaml
parameters:
# path to load services from
symfony_container_xml_path: 'var/cache/dev/AppKernelDevDebugContainer.xml'
```
2020-05-13 18:17:21 +02:00
< br >
2017-12-28 17:58:35 -02:00
## More Detailed Documentation
2017-10-15 11:07:53 +02:00
2020-05-03 14:22:23 +02:00
- **[All Rectors Overview ](/docs/rector_rules_overview.md )**
- [Create own Rule ](/docs/create_own_rule.md )
- [Generate Rector from Recipe ](/docs/rector_recipe.md )
- [How Does Rector Work? ](/docs/how_it_works.md )
- [PHP Parser Nodes Overview ](/docs/nodes_overview.md )
- [Add Checkstyle with your CI ](/docs/checkstyle.md )
2017-10-15 11:07:53 +02:00
2020-05-13 18:17:21 +02:00
< br >
2017-12-28 17:58:35 -02:00
## How to Contribute
2017-07-15 19:20:20 +02:00
2020-03-21 12:00:05 +01:00
See [the contribution guide ](/CONTRIBUTING.md ).
2018-12-12 10:02:10 +01:00
2020-05-13 18:17:21 +02:00
< br >
2018-12-31 20:27:38 +01:00
## Run Rector in Docker
2020-05-03 14:19:09 +02:00
2019-10-24 17:59:42 -07:00
You can run Rector on your project using Docker:
2018-12-31 20:27:38 +01:00
2018-12-12 10:02:10 +01:00
```bash
2019-08-25 12:19:03 +02:00
docker run -v $(pwd):/project rector/rector:latest process /project/src --set symfony40 --dry-run
2019-03-11 16:12:27 +02:00
2019-10-25 18:11:11 -07:00
# Note that a volume is mounted from `pwd` (the current directory) into `/project` which can be accessed later.
2018-12-11 18:22:54 +01:00
```
2019-03-11 16:27:13 +02:00
Using `rector.yaml` :
```bash
2020-03-21 14:23:09 +01:00
docker run -v $(pwd):/project rector/rector:latest process /project/app \
--config /project/rector.yaml \
--autoload-file /project/vendor/autoload.php \
--dry-run
2019-03-11 16:27:13 +02:00
```
2019-04-02 15:40:53 +02:00
2020-05-13 18:17:21 +02:00
< br >
## Community Packages
2019-04-02 15:40:53 +02:00
2019-10-24 17:59:42 -07:00
Do you use Rector to upgrade your code? Add it here:
2019-04-02 15:40:53 +02:00
2020-04-17 17:13:29 -04:00
- [palantirnet/drupal-rector ](https://github.com/palantirnet/drupal-rector ) by [Palantir.net ](https://github.com/palantirnet ) for [Drupal ](https://www.drupal.org/ )
2019-10-24 18:04:18 -07:00
- [sabbelasichon/typo3-rector ](https://github.com/sabbelasichon/typo3-rector ) for [TYPO3 ](https://typo3.org/ )