Closed shivampaw closed 1 year ago
You need to extends AbstractScopeAwareRector
and implements public function refactorWithScope(Node $node, Scope $scope)
for that:
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class TestRector extends AbstractScopeAwareRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Hello!', [
new CodeSample(
<<<'PHP'
return;
PHP,
<<<'PHP'
return;
PHP,
),
]);
}
public function getNodeTypes(): array
{
return [Variable::class];
}
public function refactorWithScope(Node $node, Scope $scope)
{
return $node;
}
}
Thanks @samsonasik, I've tried that but get a similar error:
[ERROR] Could not process
"x.php"
file, due to:
"System error: "Scope not available on "PhpParser\Node\Expr\Variable" node with parent node of
"PhpParser\Node\Expr\ArrayItem", but is required by a refactorWithScope() method of
"CodeRector\CustomRules\CamelCaseLocalVariableNames" rule. Fix scope refresh on changed nodes first"
Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On
line: 53
I see, that's bug on PHPStanNodeScopeResolver
on foreach value with ArrayItem, I will try to provide a fix :)
Bug Report
If I create a basic rule (MVP is just returning the $node back) and the variable seems to be a destructured array, it is failing.
Minimal PHP Code Causing Issue
Custom Rule:
PHP Code:
I'm getting an error when it gets to the $a_b variable
Expected Behaviour
No error in this case