2018-01-05 15:39:33 +01:00
# How Does Rector Work?
(Inspired by [*How it works* in BetterReflection ](https://github.com/Roave/BetterReflection/blob/master/docs/how-it-works.md ))
## 1. Finds all files and Load Configured Rectors
2020-11-26 17:55:33 +01:00
- The application finds files in the source code you provide and registered Rectors - from `--config` or local `rector.php`
2018-01-05 15:39:33 +01:00
- Then it iterates all found files and applies relevant Rectors to them.
2020-03-21 12:10:24 +01:00
- A *Rector* in this context is 1 single class that modifies 1 thing, e.g. changes the class name
2018-01-05 15:39:33 +01:00
## 2. Parse and Reconstruct 1 File
2020-03-21 12:10:24 +01:00
The iteration of files, nodes and Rectors respects this lifecycle:
2018-01-06 15:10:18 +01:00
2018-01-05 16:19:15 +01:00
```php
2019-10-13 07:59:52 +02:00
< ?php
declare(strict_types=1);
2018-08-15 16:10:47 +02:00
use Rector\Contract\Rector\PhpRectorInterface;
use PhpParser\Parser;
/** @var SplFileInfo[] $fileInfos */
2018-01-05 16:19:15 +01:00
foreach ($fileInfos as $fileInfo) {
// 1 file => nodes
2018-08-15 16:10:47 +02:00
/** @var Parser $phpParser */
2019-05-14 23:36:24 +09:00
$nodes = $phpParser->parse(file_get_contents($fileInfo->getRealPath()));
2018-01-06 15:10:18 +01:00
// nodes => 1 node
2018-01-05 16:19:15 +01:00
foreach ($nodes as $node) { // rather traverse all of them
2018-08-15 16:10:47 +02:00
/** @var PhpRectorInterface[] $rectors */
2018-01-05 16:19:15 +01:00
foreach ($rectors as $rector) {
2018-08-15 16:10:47 +02:00
foreach ($rector->getNodeTypes() as $nodeType) {
if (is_a($node, $nodeType, true)) {
$rector->refactor($node);
}
2018-01-05 16:19:15 +01:00
}
}
}
}
```
2018-01-05 15:39:33 +01:00
### 2.1 Prepare Phase
2020-03-21 12:10:24 +01:00
- Files are parsed by [`nikic/php-parser` ](https://github.com/nikic/PHP-Parser ), 4.0 that supports writing modified tree back to a file
- Then nodes (array of objects by parser) are traversed by `StandaloneTraverseNodeTraverser` to prepare their metadata, e.g. the class name, the method node the node is in, the namespace name etc. added by `$node->setAttribute(Attribute::CLASS_NODE, 'value')` .
2018-01-05 15:39:33 +01:00
2018-01-06 15:10:18 +01:00
### 2.2 Rectify Phase
2020-03-21 12:10:24 +01:00
- When all nodes are ready, the application iterates on all active Rectors
- Each node is compared with `$rector->getNodeTypes()` method to see if this Rector should do some work on it, e.g. is this class name called `OldClassName` ?
2018-01-05 15:39:33 +01:00
- If it doesn't match, it goes to next node.
- If it matches, the `$rector->reconstruct($node)` method is called
2020-03-21 12:10:24 +01:00
- Active Rector change everything they have to and return changed nodes
2018-01-05 15:39:33 +01:00
2018-01-06 15:10:18 +01:00
### 2.2.1 Order of Rectors
2020-03-21 12:10:24 +01:00
- Rectors are run by they natural order in the configuration, meaning the first
in the configuration will be run first.
2018-01-06 15:10:18 +01:00
2020-03-21 12:10:24 +01:00
E.g. in this case, first the `@expectedException` annotation will be changed to a method,
then the `setExpectedException` method will be changed to `expectedException` .
2018-01-06 15:10:18 +01:00
2020-07-29 18:28:19 +02:00
```php
< ?php
// rector.php
declare(strict_types=1);
2018-01-06 15:10:18 +01:00
2020-07-29 18:28:19 +02:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2020-08-18 17:57:30 +02:00
$services->set(Rector\PHPUnit\Rector\ClassMethod\ExceptionAnnotationRector::class);
2020-07-29 18:28:19 +02:00
$services->set(Rector\Renaming\Rector\MethodCall\RenameMethodRector::class)
->arg('$oldToNewMethodsByClass', [
PHPUnit\Framework\TestClass::class => [
'setExpectedException' => 'expectedException',
'setExpectedExceptionRegExp' => 'expectedException',
],
]);
};
2018-01-06 15:10:18 +01:00
```
2018-01-05 15:39:33 +01:00
### 2.3 Save File/Diff Phase
2018-01-06 15:10:18 +01:00
- When work on all nodes of 1 file is done, the file will be saved if it has some changes
2018-01-05 15:39:33 +01:00
- Or if the `--dry-run` option is on, it will store the *git-like* diff thanks to [GeckoPackages/GeckoDiffOutputBuilder ](https://github.com/GeckoPackages/GeckoDiffOutputBuilder )
2020-03-21 12:10:24 +01:00
- Then Rector will go to the next file
2018-01-05 15:39:33 +01:00
## 3 Reporting
2020-03-21 12:10:24 +01:00
- After this, Rector displays the list of changed files
2018-02-01 07:12:22 -02:00
- Or with `--dry-run` option the diff of these files
2018-02-23 19:21:22 +01:00
### Similar Projects
2018-08-15 16:10:47 +02:00
- [ClangMR ](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/41342.pdf ) for C++ by Google (closed source) - almost idential workflow, developed independently though
2020-05-13 17:35:28 +02:00
- [hhast ](https://github.com/hhvm/hhast ) - HHVM AST + format preserving + mirations
2018-02-25 17:05:35 +01:00
- [facebook/jscodeshift ](https://github.com/facebook/jscodeshift ) for Javascript
2018-08-15 16:10:47 +02:00
- [silverstripe/silverstripe-upgrader ](https://github.com/silverstripe/silverstripe-upgrader ) for PHP CMS, Silverstripe
- [dereuromark/upgrade ](https://github.com/dereuromark/upgrade ) for PHP Framework, CakePHP