--config
or local rector.php
The iteration of files, nodes and Rectors respects this lifecycle:
<?php
use Rector\Contract\Rector\PhpRectorInterface;
use PhpParser\Parser;
/** @var SplFileInfo[] $fileInfos */
foreach ($fileInfos as $fileInfo) {
// 1 file => nodes
/** @var Parser $phpParser */
$nodes = $phpParser->parse(file_get_contents($fileInfo->getRealPath()));
// nodes => 1 node
foreach ($nodes as $node) { // rather traverse all of them
/** @var PhpRectorInterface[] $rectors */
foreach ($rectors as $rector) {
foreach ($rector->getNodeTypes() as $nodeType) {
if (is_a($node, $nodeType, true)) {
$rector->refactor($node);
}
}
}
}
}
nikic/php-parser
, 4.0 that supports writing modified tree back to a fileStandaloneTraverseNodeTraverser
to prepare their metadata, e.g. the class name, the method node the node is in, the namespace name etc. added by $node->setAttribute('key', 'value')
.$rector->getNodeTypes()
method to see if this Rector should do some work on it, e.g. is this class name called OldClassName
?$rector->refactor($node)
method is calledE.g. rectors for Class_
node always run before rectors for ClassMethod
in one class.
E.g. in this case, first the @expectedException
annotation will be changed to a method,
then the setExpectedException
method will be changed to expectedException
.
<?php
use Rector\PHPUnit\Rector\ClassMethod\ExceptionAnnotationRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\ValueObject\MethodCallRename;
return RectorConfig::configure()
->withRules([ExceptionAnnotationRector::class])
->withConfiguredRule(Rector\Renaming\Rector\MethodCall\RenameMethodRector::class, [
new MethodCallRename('PHPUnit\Framework\TestClass', 'setExpectedException', 'expectedException'),
new MethodCallRename('PHPUnit\Framework\TestClass', 'setExpectedExceptionRegExp', 'expectedException'),
]);
--dry-run
option is on, it will store the git-like diff thanks to GeckoPackages/GeckoDiffOutputBuilder
--dry-run
option the diff of these files