rector/README.md

180 lines
5.7 KiB
Markdown
Raw Normal View History

2017-12-19 22:14:51 +00:00
# Rector - Upgrade your Legacy App to Modern Codebase
2017-07-15 17:20:20 +00:00
Rector is under development phase in 2018, to figure out the best way to use it in applications, polish API of Rector classes and get feedback from community. Please consider this while using it and report issues or post ideas you'll come up with while using. Thank you!
2018-02-02 16:51:41 +00:00
When you're gonna move from manual work to **instant upgrades**?
2017-11-13 01:58:21 +00:00
[![Build Status](https://img.shields.io/travis/rectorphp/rector/master.svg?style=flat-square)](https://travis-ci.org/rectorphp/rector)
2017-11-13 14:31:35 +00:00
[![Coverage Status](https://img.shields.io/coveralls/RectorPHP/Rector/master.svg?style=flat-square)](https://coveralls.io/github/rectorphp/rector?branch=master)
2017-07-15 17:20:20 +00:00
2018-01-13 15:01:31 +00:00
![Rector-showcase](docs/images/rector-showcase.gif)
2017-10-30 15:08:28 +00:00
Rector **upgrades your application** for you, with focus on open-source projects:
2017-07-15 17:20:20 +00:00
2017-10-30 15:00:28 +00:00
<p align="center">
2017-11-21 10:48:32 +00:00
<a href="/src/config/level/symfony"><img src="/docs/images/symfony.png"></a>
2017-12-10 01:52:28 +00:00
<img src="/docs/images/space.png" width=20>
2017-11-21 10:48:32 +00:00
<a href="/src/config/level/nette"><img src="/docs/images/nette.png" height="50"></a>
2017-12-10 01:52:28 +00:00
<img src="/docs/images/space.png" width=20>
2017-11-21 10:48:32 +00:00
<a href="/src/config/level/phpunit"><img src="/docs/images/phpunit.jpg"></a>
2017-12-10 01:52:28 +00:00
<img src="/docs/images/space.png" width=20>
2017-12-02 20:59:03 +00:00
<a href="/src/config/level/twig"><img src="/docs/images/twig.png"></a>
2017-10-30 15:00:28 +00:00
</p>
2017-08-20 15:40:19 +00:00
2018-01-13 15:01:31 +00:00
2017-12-28 19:58:35 +00:00
Rector can:
2017-12-26 21:43:37 +00:00
2018-03-06 19:43:10 +00:00
- [Rename classes](/docs/DynamicRectors.md#replace-a-class-name)
- [Rename class' methods](/docs/DynamicRectors.md#change-a-method-name)
- [Rename partial namespace](/docs/DynamicRectors.md#eplace-some-part-of-the-namespace)
- [Rename pseudo-namespace to namespace](/docs/DynamicRectors.md#replace-the-underscore-naming-_-with-namespaces-)
- [Add, replace or remove arguments](/docs/DynamicRectors.md#change-a-argument-value)
- [Add typehints based on new types of parent class or interface](/docs/DynamicRectors.md#remove-a-value-object-and-use-simple-type)
2017-12-28 19:58:35 +00:00
- And much more...
2017-07-15 17:20:20 +00:00
## Install
```bash
composer require --dev rector/rector:'dev-master' nikic/php-parser:'4.0.x-dev'
2017-07-15 17:20:20 +00:00
```
### Do you Have Conflicts?
2018-01-14 16:24:28 +00:00
It may also happen your dependencies are in conflict, e.g. [PHPStan](https://github.com/phpstan/phpstan) requires [PHP-Parser](https://github.com/nikic/PHP-Parser) version 3, or older PHPUnit etc. This might be solved in the future, when PHP-Parser version 4 becomes stable.
Since Rector **uses project's autoload to analyze type of elements**, it cannot be installed as project in standalone directory but **needs to be added as dependency**. Here [`bamarni/composer-bin-plugin`](https://github.com/bamarni/composer-bin-plugin) becomes useful:
```bash
composer require bamarni/composer-bin-plugin --dev
```
Then, require Rector using `composer bin`:
```bash
composer bin rector require --dev rector/rector:'dev-master' nikic/php-parser:'4.0.x-dev'
```
And Rector is accessible as:
```bash
vendor/bin/rector
```
### Extra Autoloading
2018-02-09 00:16:54 +00:00
Rector relies on project and autoloading of its classes. To specify own autoload file, use `--autoload-file` option:
```bash
2018-02-08 14:27:14 +00:00
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
```
2017-12-28 19:58:35 +00:00
## How to Reconstruct your Code
2017-10-30 13:33:09 +00:00
### A. Prepared Sets
2017-12-02 21:03:10 +00:00
Featured open-source projects have **prepared sets**. You'll find them in [`/src/config/level`](/src/config/level).
2017-10-30 13:33:09 +00:00
2017-12-28 19:58:35 +00:00
Do you need to upgrade to **Symfony 4.0**, for example?
2017-12-28 19:58:35 +00:00
1. Run rector on your `/src` directory:
2017-12-20 01:39:06 +00:00
```bash
vendor/bin/rector process src --level symfony40
```
2017-10-30 13:33:09 +00:00
2017-12-28 19:58:35 +00:00
Which is a shortcut for using complete path with `--config` option:
2017-12-20 01:39:06 +00:00
```bash
vendor/bin/rector process src --config vendor/rector/rector/src/config/level/symfony/symfony40.yml
```
2017-12-20 01:39:06 +00:00
You can also use your **own config file**:
2017-10-30 13:33:09 +00:00
2017-12-20 01:39:06 +00:00
```bash
vendor/bin/rector process src --config your-own-config.yml
```
2017-12-28 19:58:35 +00:00
2. Do you want to see the preview of changes first?
2017-12-20 01:17:34 +00:00
2017-12-28 19:58:35 +00:00
Use the `--dry-run` option:
```bash
2017-12-20 01:39:06 +00:00
vendor/bin/rector process src --level symfony33 --dry-run
```
2018-03-06 20:22:19 +00:00
3. What levels are on the board?
```bash
vendor/bin/rector levels
```
2017-10-30 13:33:09 +00:00
### B. Custom Sets
2017-09-27 21:58:14 +00:00
2017-12-28 19:58:35 +00:00
1. Create `rector.yml` with desired Rectors:
2017-09-27 21:58:14 +00:00
2017-12-20 01:39:06 +00:00
```yml
2018-03-03 20:08:25 +00:00
services:
2018-03-03 10:35:57 +00:00
Rector\Rector\Contrib\Nette\Application\InjectPropertyRector: ~
2017-12-20 01:39:06 +00:00
```
2017-09-27 21:58:14 +00:00
2017-12-28 19:58:35 +00:00
2. Try Rector on your `/src` directory:
2017-07-15 17:20:20 +00:00
2017-12-20 01:39:06 +00:00
```bash
vendor/bin/rector process src --dry-run
```
2017-09-27 21:58:14 +00:00
2017-12-28 19:58:35 +00:00
3. Apply the changes if you like them:
2017-09-27 21:58:14 +00:00
2017-12-28 19:58:35 +00:00
```bash
2017-12-20 01:39:06 +00:00
vendor/bin/rector process src
```
2017-07-15 17:20:20 +00:00
2018-03-06 19:43:10 +00:00
## Configure Rectors for your Case
You don't have to always write PHP code. Many projects change only classes or method names, so it would be too much work for a simple task.
2018-03-06 19:43:10 +00:00
- [Dynamic Rectors](/docs/DynamicRectors.md)
- [Turn Magic to Methods](/docs/MagicDisclosureRectors.md)
2018-01-26 09:34:27 +00:00
2017-12-28 19:58:35 +00:00
## Coding Standards are Outsourced
2017-11-02 17:31:57 +00:00
2018-02-24 11:01:52 +00:00
This package has no intention in formatting your code, as **coding standard tools handle this much better**. We prefer [EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) with Rector's prepared set:
2017-11-02 17:31:57 +00:00
2017-11-18 15:19:47 +00:00
```bash
2018-02-23 17:04:08 +00:00
# install
composer require --dev symplify/easy-coding-standard
2017-11-02 17:31:57 +00:00
# check
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon
2017-11-18 15:19:47 +00:00
2017-11-02 17:31:57 +00:00
# fix
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon --fix
```
2017-12-28 19:58:35 +00:00
## More Detailed Documentation
2017-10-15 09:07:53 +00:00
- [How Rector Works?](/docs/HowItWorks.md)
2018-02-26 12:30:04 +00:00
- [How to Create Rector with Fluent Builder](/docs/FluentBuilderRector.md)
2017-11-18 15:19:47 +00:00
- [How to Create Own Rector](/docs/HowToCreateOwnRector.md)
2017-10-15 09:07:53 +00:00
- [Service Name to Type Provider](/docs/ServiceNameToTypeProvider.md)
2017-12-28 19:58:35 +00:00
## How to Contribute
2017-07-15 17:20:20 +00:00
Just follow 3 rules:
- **1 feature per pull-request**
2017-08-21 10:12:51 +00:00
- **New feature needs tests**
2017-12-28 20:37:43 +00:00
- Tests, coding standards and PHPStan **checks must pass**:
2017-07-15 17:20:20 +00:00
```bash
composer complete-check
2017-07-15 17:20:20 +00:00
```
2017-08-21 10:12:51 +00:00
Don you need to fix coding standards? Run:
2017-07-15 17:20:20 +00:00
```bash
2017-08-21 10:12:51 +00:00
composer fix-cs
2017-07-15 17:20:20 +00:00
```
We would be happy to merge your feature then.