2017-12-19 23:14:51 +01:00
# Rector - Upgrade your Legacy App to 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.
2018-09-11 19:40:08 +02:00
I mean, why do it manually if 80 % can Rector handle for you?
2018-02-02 17:51:41 +01:00
2017-11-13 02:58:21 +01:00
[![Build Status ](https://img.shields.io/travis/rectorphp/rector/master.svg?style=flat-square )](https://travis-ci.org/rectorphp/rector)
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)
2017-07-15 19:20:20 +02:00
2018-01-13 16:01:31 +01:00
![Rector-showcase ](docs/images/rector-showcase.gif )
2018-07-27 19:44:24 +02:00
Rector **instantly upgrades PHP & YAML code of your application** , with focus on open-source projects:
2017-07-15 19:20:20 +02:00
2017-10-30 16:00:28 +01:00
< p align = "center" >
2018-10-21 11:00:07 +02:00
< a href = "/config/level/php" > < img src = "/docs/images/php.png" > < / a >
< img src = "/docs/images/space.png" width = 20 >
< a href = "/config/level/cakephp" > < img src = "/docs/images/cakephp.png" > < / a >
< img src = "/docs/images/space.png" width = 20 >
2018-03-18 18:40:13 -03:00
< a href = "/config/level/symfony" > < img src = "/docs/images/symfony.png" > < / a >
2017-12-10 02:52:28 +01:00
< img src = "/docs/images/space.png" width = 20 >
2018-05-26 15:39:01 -03:00
< a href = "/config/level/sylius" > < img src = "/docs/images/sylius.png" > < / a >
2018-05-22 17:43:36 +02:00
< img src = "/docs/images/space.png" width = 20 >
2018-03-18 18:40:13 -03:00
< a href = "/config/level/phpunit" > < img src = "/docs/images/phpunit.jpg" > < / a >
2017-12-10 02:52:28 +01:00
< img src = "/docs/images/space.png" width = 20 >
2018-03-18 18:40:13 -03:00
< a href = "/config/level/twig" > < img src = "/docs/images/twig.png" > < / a >
2018-09-26 21:26:04 +08:00
< img src = "/docs/images/space.png" width = 20 >
2018-09-27 01:15:10 +08:00
< a href = "/config/level/silverstripe" > < img src = "/docs/images/silverstripe.jpg" > < / a >
2017-10-30 16:00:28 +01:00
< / p >
2017-08-20 17:40:19 +02:00
2018-08-01 22:29:01 +02:00
**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
2017-12-28 17:58:35 -02:00
- And much more...
2017-07-15 19:20:20 +02:00
2018-08-01 22:29:01 +02:00
...just **look at overview of [all available Rectors](/docs/AllRectorsOverview.md)** with before/after diffs and configuration examples. It's all you really need to use build your own sets.
2018-04-29 11:03:47 +02: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
```
2018-07-29 12:41:48 +02:00
**Do you have conflicts on `composer require` ?**
2018-01-06 16:50:22 +01:00
2018-07-29 12:41:48 +02:00
Install [prefixed version ](https://github.com/rectorphp/rector-prefixed ) with isolated dependencies.
2018-01-06 16:50:22 +01:00
2018-02-08 15:25:45 +01:00
### Extra Autoloading
2018-02-09 01:16:54 +01:00
Rector relies on project and autoloading of its classes. To specify own autoload file, use `--autoload-file` option:
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
```
2018-04-13 21:14:07 +02:00
Or make use of `rector.yml` config:
```yaml
# rector.yml
parameters:
2018-09-26 15:03:04 +08:00
autoload_paths:
2018-04-13 21:14:07 +02:00
- '%kernel.project_dir%/vendor/squizlabs/php_codesniffer/autoload.php'
- '%kernel.project_dir%/vendor/project-without-composer'
```
2018-09-26 15:03:04 +08:00
You can also **exclude files or directories** - use regex or [fnmatch ](http://php.net/manual/en/function.fnmatch.php ):
2018-08-29 22:45:09 +02:00
```yaml
# rector.yml
parameters:
exclude_paths:
- '*/src/*/Tests/*'
```
2017-12-28 17:58:35 -02:00
## How to Reconstruct your Code
2017-10-30 14:33:09 +01:00
### A. Prepared Sets
2018-03-18 18:40:13 -03:00
Featured open-source projects have **prepared sets** . You'll find them in [`/config/level` ](/config/level ).
2017-10-30 14:33:09 +01:00
2017-12-28 17:58:35 -02:00
Do you need to upgrade to **Symfony 4.0** , for example?
2017-09-23 23:51:04 +02:00
2017-12-28 17:58:35 -02:00
1. Run rector on your `/src` directory:
2017-09-23 23:51:04 +02:00
2017-12-20 02:39:06 +01:00
```bash
vendor/bin/rector process src --level symfony40
```
2017-10-30 14:33:09 +01:00
2017-12-28 17:58:35 -02:00
Which is a shortcut for using complete path with `--config` option:
2017-12-20 02:39:06 +01:00
```bash
vendor/bin/rector process src --config vendor/rector/rector/src/config/level/symfony/symfony40.yml
```
2017-11-09 12:17:31 +01:00
2017-12-20 02:39:06 +01:00
You can also use your **own config file** :
2017-10-30 14:33:09 +01:00
2017-12-20 02:39:06 +01:00
```bash
vendor/bin/rector process src --config your-own-config.yml
```
2017-09-23 23:51:04 +02:00
2017-12-28 17:58:35 -02:00
2. Do you want to see the preview of changes first?
2017-12-20 02:17:34 +01:00
2017-12-28 17:58:35 -02:00
Use the `--dry-run` option:
```bash
2017-12-20 02:39:06 +01:00
vendor/bin/rector process src --level symfony33 --dry-run
```
2017-09-23 23:51:04 +02:00
2018-08-30 10:50:03 +02:00
3. To process just specific subdirectories, you can use [fnmatch ](http://php.net/manual/en/function.fnmatch.php ) pattern with `*` :
```bash
vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run
```
4. What levels are on the board?
2018-03-06 21:22:19 +01:00
```bash
vendor/bin/rector levels
```
2017-10-30 14:33:09 +01:00
### B. Custom Sets
2017-09-27 23:58:14 +02:00
2017-12-28 17:58:35 -02:00
1. Create `rector.yml` with desired Rectors:
2017-09-27 23:58:14 +02:00
2017-12-20 02:39:06 +01:00
```yml
2018-03-03 21:08:25 +01:00
services:
2018-06-02 13:06:44 +02:00
Rector\Nette\Rector\Application\InjectPropertyRector: ~
2017-12-20 02:39:06 +01:00
```
2017-09-27 23:58:14 +02:00
2017-12-28 17:58:35 -02:00
2. Try Rector on your `/src` directory:
2017-07-15 19:20:20 +02:00
2017-12-20 02:39:06 +01:00
```bash
vendor/bin/rector process src --dry-run
```
2017-09-27 23:58:14 +02:00
2017-12-28 17:58:35 -02:00
3. Apply the changes if you like them:
2017-09-27 23:58:14 +02:00
2017-12-28 17:58:35 -02:00
```bash
2017-12-20 02:39:06 +01:00
vendor/bin/rector process src
```
2017-07-15 19:20:20 +02:00
2018-07-01 18:37:04 +02:00
## How to Apply Coding Standards?
2017-11-02 18:31:57 +01:00
2018-07-01 18:37:04 +02:00
Rector uses [EasyCodingStandard ](https://github.com/Symplify/EasyCodingStandard ) to improve the code style of changed files, like import namespaces, make 1 empty line between class elements etc.
2017-11-02 18:31:57 +01:00
2018-07-01 18:37:04 +02:00
It's always better to use own project's prepared set, but if you don't have the option yet, just use `--with-style` option to handle these basic cases:
2017-11-18 16:19:47 +01:00
2018-07-01 18:37:04 +02:00
```bash
vendor/bin/rector process src --with-style
2017-11-02 18:31:57 +01:00
```
2017-12-28 17:58:35 -02:00
## More Detailed Documentation
2017-10-15 11:07:53 +02:00
2018-01-06 16:05:19 +01:00
- [How Rector Works? ](/docs/HowItWorks.md )
2017-11-18 16:19:47 +01:00
- [How to Create Own Rector ](/docs/HowToCreateOwnRector.md )
2017-10-15 11:07:53 +02:00
- [Service Name to Type Provider ](/docs/ServiceNameToTypeProvider.md )
2017-12-28 17:58:35 -02:00
## How to Contribute
2017-07-15 19:20:20 +02:00
Just follow 3 rules:
- **1 feature per pull-request**
2017-08-21 12:12:51 +02:00
- **New feature needs tests**
2017-12-28 18:37:43 -02:00
- Tests, coding standards and PHPStan **checks must pass** :
2017-07-15 19:20:20 +02:00
```bash
2017-12-28 18:27:16 -02:00
composer complete-check
2017-07-15 19:20:20 +02:00
```
2017-08-21 12:12:51 +02:00
Don you need to fix coding standards? Run:
2017-07-15 19:20:20 +02:00
```bash
2017-08-21 12:12:51 +02:00
composer fix-cs
2017-07-15 19:20:20 +02:00
```
We would be happy to merge your feature then.