rector/UPGRADING.md
Tomas Votruba e5d26b0955 Updated Rector to commit 1e028eba0d8b7c131423f1d98cd2fde5f0838b03
1e028eba0d [upgrading] link phpstan + php-parser upgrade guides (#6542)
2024-12-10 10:32:16 +00:00

2.5 KiB

Upgrading from Rector 1.x to 2.0

PHP version requirements

Rector now uses PHP 7.4 or newer to run.


Rector now uses PHP-Parser 5

See upgrading guide for PHP-Parser.


Rector now uses PHPStan 2

See upgrading guide for PHPStan.


Upgrade for custom Rules writers

1. AbstractScopeAwareRector is removed, use AbstractRector instead

The Rector\Rector\AbstractScopeAwareRector was too granular to fetch single helper object. It made creating new custom rules ambiguous, one layer more complex and confusing. This class has been removed in favor of standard AbstractRector. The Scope object can be fetched via ScopeFetcher.

Before

use Rector\Rector\AbstractScopeAwareRector;

final class SimpleRector extends AbstractScopeAwareRector
{
    public function refactorWithScope(Node $node, Scope $scope): ?Node
    {
        // ...
    }
}

After

use Rector\Rector\AbstractRector;
use Rector\PHPStan\ScopeFetcher;

final class SimpleRector extends AbstractRector
{
    public function refactor(Node $node): ?Node
    {
        if (...) {
            // this allow to fetch scope only when needed
            $scope = ScopeFetcher::fetch($node);
        }

        // ...
    }
}

2. AbstractRector get focused on code, the getRuleDefinition() is no longer required

Core rules need documentation, so people can read their feature and search through them. Yet for writing custom rules and local rules, its not necessary. People often filled it empty, just to make Rector happy.

This is no longer needed. Now The getRuleDefinition() method has been removed:

 use Rector\Rector\AbstractRector;
-use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
-use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

 final class SimpleRector extends AbstractRector
 {
-    public function getRuleDefinition(): RuleDefinition
-    {
-        return new RuleDefinition('// @todo fill the description', [
-            new CodeSample(
-                <<<'CODE_SAMPLE'
-// @todo fill code before
-CODE_SAMPLE
-                ,
-                <<<'CODE_SAMPLE'
-// @todo fill code after
-CODE_SAMPLE
-            ),
-        ]);
-    }

     // valuable code here
 }

If you need description yourself to understand rule after many months, use the common place for documentation - docblock above class.