rector/README.md

469 lines
12 KiB
Markdown
Raw Normal View History

2017-12-19 23:14:51 +01:00
# Rector - Upgrade your Legacy App to Modern Codebase
2017-07-15 19:20:20 +02: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 17:51:41 +01:00
When you're gonna move from manual work to **instant upgrades**?
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)
2017-11-13 15:31:35 +01: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 19:20:20 +02:00
2018-01-13 16:01:31 +01:00
![Rector-showcase](docs/images/rector-showcase.gif)
2017-10-30 16:08:28 +01:00
Rector **upgrades your application** for you, 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">
2017-11-21 11:48:32 +01:00
<a href="/src/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>
2017-11-21 11:48:32 +01:00
<a href="/src/config/level/nette"><img src="/docs/images/nette.png" height="50"></a>
2017-12-10 02:52:28 +01:00
<img src="/docs/images/space.png" width=20>
2017-11-21 11:48:32 +01:00
<a href="/src/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>
2017-12-02 21:59:03 +01:00
<a href="/src/config/level/twig"><img src="/docs/images/twig.png"></a>
2017-10-30 16:00:28 +01:00
</p>
2017-08-20 17:40:19 +02:00
2018-01-13 16:01:31 +01:00
2017-12-28 17:58:35 -02:00
Rector can:
2017-12-26 22:43:37 +01:00
2017-12-31 14:22:41 -02:00
- [Rename classes](#replace-a-class-name)
- [Rename class' methods](#change-a-method-name)
- [Rename partial namespace](#eplace-some-part-of-the-namespace)
- [Rename pseudo-namespace to namespace](#replace-the-underscore-naming-_-with-namespaces-)
- [Add, replace or remove arguments](#change-a-argument-value)
- [Add typehints based on new types of parent class or interface](#remove-a-value-object-and-use-simple-type)
2017-12-28 17:58:35 -02:00
- And much more...
2017-07-15 19:20:20 +02:00
## Install
```bash
composer require --dev rector/rector:'dev-master' nikic/php-parser:'4.0.x-dev'
2017-07-15 19:20:20 +02:00
```
### Do you Have Conflicts?
2018-01-14 14:24:28 -02: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 01:16:54 +01:00
Rector relies on project and autoloading of its classes. To specify own autoload file, use `--autoload-file` option:
```bash
2018-02-08 15:27:14 +01:00
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
```
2017-12-28 17:58:35 -02:00
## How to Reconstruct your Code
2017-10-30 14:33:09 +01:00
### A. Prepared Sets
2017-12-02 22:03:10 +01:00
Featured open-source projects have **prepared sets**. You'll find them in [`/src/config/level`](/src/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-12-28 17:58:35 -02:00
1. Run rector on your `/src` directory:
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-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-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-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-03-03 11:35:57 +01:00
Rector\Rector\Contrib\Nette\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
2017-12-28 17:58:35 -02:00
## Simple setup with Dynamic Rectors
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.
Instead you can use prepared **Dynamic Rectors** directly in `*.yml` config:
You can:
### Replace a class name
```yml
# phpunit60.yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\ClassReplacerRector:
2018-03-03 11:35:57 +01:00
$oldToNewClasses:
# old class: new class
'PHPUnit_Framework_TestCase': 'PHPUnit\Framework\TestCase'
```
2017-10-07 22:34:30 +02:00
### Replace some part of the namespace
```yml
# better-reflection20.yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\NamespaceReplacerRector:
2018-03-03 11:35:57 +01:00
$oldToNewNamespaces:
# old namespace: new namespace
'BetterReflection': 'Roave\BetterReflection'
```
### Change a method name
2017-10-22 00:04:15 +02:00
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\MethodNameReplacerRector:
2018-03-03 11:35:57 +01:00
$perClassOldToNewMethods:
# class
'Nette\Utils\Html':
# old method: new method
'add': 'addHtml'
# or in case of static methods calls
# class
'Nette\Bridges\FormsLatte\FormMacros':
# old method: [new class, new method]
'renderFormBegin': ['Nette\Bridges\FormsLatte\Runtime', 'renderFormBegin']
```
2017-10-20 20:35:04 +02:00
### Change a property name
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\PropertyNameReplacerRector:
2018-03-03 11:35:57 +01:00
$perClassOldToNewProperties:
# class:
'PhpParser\Node\Param':
# old property: new property
'name': 'var'
```
### Change a class constant name
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\ClassConstantReplacerRector:
2018-03-03 11:35:57 +01:00
$oldToNewConstantsByClass:
# class
'Symfony\Component\Form\FormEvents':
# old constant: new constant
'PRE_BIND': 'PRE_SUBMIT'
'BIND': 'SUBMIT'
'POST_BIND': 'POST_SUBMIT'
```
2017-11-01 13:07:59 +01:00
### Change parameters type hinting according to the parent type
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\ParentTypehintedArgumentRector:
2018-03-03 11:35:57 +01:00
$typehintForArgumentByMethodAndClass:
# class
'PhpParser\Parser':
# method
'parse':
# parameter: typehint
'code': 'string'
```
2017-10-30 14:33:09 +01:00
2018-02-13 22:28:08 +01:00
### Change argument value or remove argument
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\ArgumentReplacerRector:
2018-03-03 11:35:57 +01:00
$argumentChangesByMethodAndType:
-
class: 'Symfony\Component\DependencyInjection\ContainerBuilder'
method: 'compile'
position: 0
# change default value
type: 'changed'
default_value: false
# or remove
type: 'removed'
# or replace old default value by new one
type: 'replace_default_value'
replace_map:
'Symfony\Component\DependencyInjection\ContainerBuilder::SCOPE_PROTOTYPE': false
```
### Replace the underscore naming `_` with namespaces `\`
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector:
2018-03-03 11:35:57 +01:00
$configuration:
# old namespace prefix
- 'PHPUnit_'
# exclude classes
- '!PHPUnit_Framework_MockObject_MockObject'
```
2017-11-18 16:51:57 +01:00
### Modify a property to method
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\PropertyToMethodRector:
2018-03-03 11:35:57 +01:00
$perClassPropertyToMethods:
# type
'Symfony\Component\Translation\Translator':
# property to replace
'locale':
# (prepared key): get method name
'get': 'getLocale'
# (prepared key): set method name
'set': 'setLocale'
```
2017-11-18 16:51:57 +01:00
### Remove a value object and use simple type
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\Dynamic\ValueObjectRemoverRector:
2018-03-03 11:35:57 +01:00
$valueObjectsToSimpleTypes:
# type: new simple type
'ValueObject\Name': 'string'
```
2017-12-28 17:58:35 -02:00
For example:
```diff
2018-02-09 01:16:54 +01:00
- $value = new ValueObject\Name('Tomas');
+ $value = 'Tomas';
```
2017-12-28 17:58:35 -02:00
```diff
/**
2018-02-09 01:16:54 +01:00
-* @var ValueObject\Name
+* @var string
*/
private $name;
```
2017-12-28 17:58:35 -02:00
```diff
2018-02-09 01:16:54 +01:00
- public function someMethod(ValueObject\Name $name) { ...
+ public function someMethod(string $name) { ...
```
2017-11-18 16:51:57 +01:00
2018-02-09 11:17:43 +01:00
## Replace Property and Method Annotations
2018-02-09 01:16:54 +01:00
```yml
2018-03-03 11:35:57 +01:00
services:
2018-02-09 01:16:54 +01:00
Rector\Rector\Dynamic\AnnotationReplacerRector:
2018-03-03 11:35:57 +01:00
$classToAnnotationMap:
# type
PHPUnit\Framework\TestCase:
# old annotation: new annotation
scenario: test
2018-02-09 01:16:54 +01:00
```
```diff
final class SomeTest extends PHPUnit\Framework\TestCase
{
/**
- * @scenario
+ * @test
*/
public function test()
{
}
}
```
2017-12-28 17:58:35 -02:00
## Turn Magic to Methods
### Replace `get/set` magic methods with real ones
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
2018-03-03 11:35:57 +01:00
$typeToMethodCalls:
# class
'Nette\DI\Container':
# magic method (prepared keys): new real method
'get': 'getService'
'set': 'addService'
```
2017-12-28 17:58:35 -02:00
For example:
2017-11-18 15:03:38 +01:00
```diff
- $result = $container['key'];
+ $result = $container->getService('key');
```
2017-12-28 17:58:35 -02:00
```diff
- $container['key'] = $value;
+ $container->addService('key', $value);
```
2017-12-28 17:58:35 -02:00
### Replace `isset/unset` magic methods with real ones
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
2018-03-03 11:35:57 +01:00
$typeToMethodCalls:
# class
'Nette\DI\Container':
# magic method (prepared keys): new real method
'isset': 'hasService'
'unset': 'removeService'
```
For example:
2017-11-18 15:03:38 +01:00
```diff
- isset($container['key']);
+ $container->hasService('key');
```
2017-12-28 17:58:35 -02:00
```diff
- unset($container['key']);
+ $container->removeService('key');
```
### Replace `toString` magic method with real one
2017-11-18 15:42:15 +01:00
```yml
2018-03-03 11:35:57 +01:00
services:
Rector\Rector\MagicDisclosure\ToStringToMethodCallRector:
2018-03-03 11:35:57 +01:00
$typeToMethodCalls:
# class
'Symfony\Component\Config\ConfigCache':
# magic method (prepared key): new real method
'toString': 'getPath'
```
2017-12-28 17:58:35 -02:00
For example:
2017-12-28 17:58:35 -02:00
```diff
- $result = (string) $someValue;
2018-01-07 23:10:10 -02:00
+ $result = $someValue->getPath();
2018-01-02 00:25:27 -02:00
```
2017-11-23 17:04:36 +01:00
```diff
- $result = $someValue->__toString();
2018-01-07 23:10:10 -02:00
+ $result = $someValue->getPath();
```
2017-11-18 15:42:15 +01:00
2018-01-26 10:34:43 +01:00
### Remove [Fluent Interface](https://ocramius.github.io/blog/fluent-interfaces-are-evil/)
2018-01-26 10:34:27 +01:00
```yml
2018-03-03 11:35:57 +01:00
services:
2018-01-26 10:34:27 +01:00
Rector\Rector\Dynamic\FluentReplaceRector: ~
```
```diff
class SomeClass
{
public function setValue($value)
{
$this->value = $value;
- return $this;
}
2018-02-01 07:12:22 -02:00
2018-01-26 10:34:27 +01:00
public function setAnotherValue($anontherValue)
{
$this->anotherValue = $anotherValue;
- return $this;
}
}
$someClass = new SomeClass();
- $someClass->setValue(5)
+ $someClass->setValue(5);
- ->setAnotherValue(10);
+ $someClass->setAnotherValue(10);
}
```
2017-12-28 17:58:35 -02:00
## Coding Standards are Outsourced
2017-11-02 18:31:57 +01:00
2018-02-24 12:01:52 +01: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 18:31:57 +01:00
2017-11-18 16:19:47 +01:00
```bash
2018-02-23 18:04:08 +01:00
# install
composer require --dev symplify/easy-coding-standard
2017-11-02 18:31:57 +01:00
# check
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon
2017-11-18 16:19:47 +01:00
2017-11-02 18:31:57 +01:00
# fix
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon --fix
```
2017-12-28 17:58:35 -02:00
## More Detailed Documentation
2017-10-15 11:07:53 +02:00
- [How Rector Works?](/docs/HowItWorks.md)
2018-02-26 13:30:04 +01:00
- [How to Create Rector with Fluent Builder](/docs/FluentBuilderRector.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
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.