Rector Got 3x Faster
On PHP 8.3+, if your project took 60 s to process on Rector 2.7, it now takes about 20 s. The speed-up comes from 2 sources:
- 67 performance-focused PRs in Rector itself make it ~1.36x faster on any PHP version
- on PHP 8.3+, PHPStan Turbo (parts of PHPStan running in C++) makes it another ~2.4x faster
Turbo needs phpstan/phpstan 2.3.2+ and turns on automatically in Rector's parallel workers - no config needed.
We measured every Rector release since December 2025 on the same laravel/framework commit (Container, Database and Foundation directories), PHP 8.4 and 24 CPU cores:
| Rector | Released | Time | vs 2.3.0 |
|---|
We ran this benchmark on PHP 8.4, so the 2.8 result includes PHPStan Turbo. From 84 s on Rector 2.3 to 14 s on Rector 2.8. That's 6× faster in less than a year. So make sure to update your Rector dependency.
The performance improvements can be summarized into a few categories. Some of those tips can be useful for writing or improving your own custom rules.
Focused on Class_ Rules
In the very first version of Rector, the rules were focused on as narrow a node as possible. E.g. when you want to check if a method has Symfony @Route and convert it, it would hook into ClassMethod. This has a few caveats:
- We still need to get information about the
Class_node and figure out whether we're inside a controller. - Every single method triggers this rule, even methods in traits, enums, interfaces or private methods (those cannot be controller methods)
Instead, hook into the Class_ node, check if it's a controller and iterate methods inside.
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Rector\AbstractRector;
final class SomeControllerRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isController($node)) {
return null;
}
$hasChanged = false;
foreach ($node->getMethods() as $classMethod) {
if (! $classMethod->isPublic()) {
continue;
}
// do the work here
$hasChanged = true;
}
if (! $hasChanged) {
return null;
}
return $node;
}
}
This way:
- Only controller classes are checked
- The rule triggers once per class, instead of all class methods.
Modify File in a Single Pass
The Rector processor ran on a file until it was fully fixed. Let's say one rule added native return type declaration:
/**
* @return string
*/
-public function getName()
+public function getName(): string
{
}
Oh, the file was changed, let's run all the rules on the whole file again. Now another rule changes the file:
-/**
- * @return string
- */
public function getName(): string
{
}
Oh, the file was changed again, so let's run all the rules again.
3 full runs just for 2 changes? That's way too expensive and poor architecture. The file processor was running until no rule changed the file contents anymore. On top of that, it hid rules that were not finished in a single run - e.g. converting nested closures one level per run - silently relying on the loop to run them again.
Now each rule must finish its job in a single run. Tests enforce it: if a rule changes the file again in the next run, the test fails (#8719). Rules that relied on running again now handle nested and chained cases right away, e.g. nested closures to arrow functions (#8716).
If one rule needs another rule's result, it can say so with the new OrderedRectorInterface, so both run in the right order in the same run (#8846).
The re-run loop is now only a safety net, not part of the design (#8679, #8722, #8811). Infinite loops, e.g. when 2 rules swap args back and forth, are better caught in tests directly.
In Rector 2.8.4 we went one step further: post rectors, like name importing, now run only once the rules stop changing the file. Before, a rule could see an already imported short name like Directory, and resolve it to the global \Directory class instead of your own one (#8909).
Detect Change by Convention, not by Full-File Print
The improvement above is related to another glitch we had. We detected the file changes by printing the file and comparing diffs. In the example above, the 1st rule would run and change the return type declaration. Rector would print the file and find out the file was changed.
But there already is a way to find out if file was changed by a rule. If a rule returns null in its refactor() method, no change. If it returns a node, it was modified. There were 9 rules out of ~700 that modified a node but still returned null, so we patched them (core, PHPUnit, Doctrine, Symfony, downgrade).
public function refactor(Node $node): MethodCall|StaticCall|null
{
if (...) {
$node->args = $args;
$this->renameMethod($node);
+
+ return $node;
}
return null;
}
Without the return $node, Rector would see no change and skip printing the file - the change would be lost.
One Rule for Explicit instanceof Checks
We used to have 5 different rules for nullable object checks, each covering a single syntax:
FlipTypeControlToUseExclusiveTypeRector-$item === nulland$item !== nullObjectExplicitBoolCompareRector-if ($item)andif (! $item)EmptyOnNullableObjectToInstanceOfRector-empty($item)WhileNullableToInstanceofRector-while ($item !== null)BinaryOpNullableToInstanceofRector-$item && $otherItem
These 5 rules often shared services that did the same job. You either use all of them, or none of them. The choice was only confusing, so we made it simpler.
Now they're all handled by a single ExplicitNullableObjectInstanceRector rule. It turns any implicit nullable object check into an explicit instanceof:
final class SomeController
{
public function run(?Item $item, ?Item $otherItem)
{
- if ($item === null) {
+ if (! $item instanceof Item) {
return 'empty';
}
- if (empty($item)) {
+ if (! $item instanceof Item) {
return 'empty';
}
- if ($item && $otherItem) {
+ if ($item instanceof Item && $otherItem instanceof Item) {
return 'both';
}
- while ($item !== null) {
+ while ($item instanceof Item) {
$item = $item->getParent();
}
}
}
One rule to enable, same result whatever syntax the code uses. The old rules are deprecated.
Less Granular Sets
The same goes for sets. We used to have tiny sets like earlyReturn, instanceOf or if, with just a handful of rules each. Their rules mostly belonged to code quality anyway, so we moved them to the codeQuality set and deprecated the tiny ones (#8301, #8408, #8414):
return RectorConfig::configure()
->withPreparedSets(
codeQuality: true,
- if: true,
- earlyReturn: true,
- instanceOf: true,
);
These 3 sets are now empty, so their flags do nothing. If you used them without codeQuality: true, enable it to keep getting their rules.
There is one more benefit. The tiny sets had to be applied all at once. Now their rules are part of the code quality level, so you can add them one by one:
return RectorConfig::configure()
->withCodeQualityLevel(10);
Deprecating Naming and Transform Rules
We also deprecated 2 niche groups of rules that were rarely used in the wild. One of them renamed all variables/properties to match a type. E.g.:
-public function __construct(EntityManager $em)
+public function __construct(EntityManager $entityManager)
{
}
This seems the right way to go, until:
- there are multiple instances of a single type
- explicit naming is overridden to "just type" with no value
- there is no clear border when to rename, and where to keep
Even agents can do a better job at context-aware naming. Time to let this set go.
The next set, or rather group of rules, was the transform group. It was one of the first rules back in 2018, that simply changed one node to another, e.g. FuncCall to a StaticCall or FuncCall to New_. These were rather promo rules to showcase what abstract syntax tree transformations can do in general. They didn't have any context, just old name and new name.
That's why we deprecated 10 of them. After these deprecations, Rector still provides 778 rules. If you still use them, Rector prints a warning and skips them - they no longer change your code. Write a custom rule for your exact case instead.
Composer-Based Sets Detected for You
Since Rector 2.8.2, withComposerBased() called with no argument picks the sets of packages installed in your project. No need to list them by hand anymore:
return RectorConfig::configure()
- ->withComposerBased(laravel: true, phpunit: true, symfony: true)
+ ->withComposerBased()
It works the same way as withPhpSets(), which reads the PHP version from composer.json. Passing any argument keeps the old behavior - only the passed sets are loaded (#8748).
Find Skips You No Longer Need
Over time, the withSkip() list grows. Some skips are no longer needed, because the code changed or the rule got smarter. reportUnusedSkips() used to report only a skip whose path matched no file.
Since Rector 2.8.6, it also reports a rule skip that matched a file, but the rule would not change that file anyway (#8853, #8957):
return RectorConfig::configure()
->withSkip([
SomeRector::class => [__DIR__ . '/src/Legacy'],
])
->reportUnusedSkips();
Upgrade Now
Upgrade to Rector 2.8.6, check the output for deprecated rule warnings, and if you write custom rules - hook into Class_, finish in a single pass and return the node you changed.