Error when removing a MethodCall
node, in a custom rule
#8376
-
Bug Report
Hello, I'm trying to remove a node with a custom rector rule, I've checked what is done in The node I want to remove is a simple method call: $this->something(); I'm basically returning And I got the following error:
Minimal PHP Code Causing Issuefinal class MyRectorRule extends AbstractRector
{
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
}
/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node): Node|int|null
{
if (/** some condition **/) {
return NodeTraverser::REMOVE_NODE;
}
return null;
} I'm 100% sure my condition here is good, and I can see thanks to Expected Behaviourno error, and the node removed 😁 Thanks for your answer and your awesome tool! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You need to remove whole First, define the node types as public function getNodeTypes(): array
{
return [\PhpParser\Node\Stmt\Expression::class];
} Next, verify if the node expr is match with /**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof \PhpParser\Node\Expr\MethodCall) {
return null;
}
if ( /** some condition **/) {
return NodeTraverser::REMOVE_NODE;
}
return null;
} Above is pseudo code, but you have the idea :) |
Beta Was this translation helpful? Give feedback.
-
yes, this makes sense! thanks for this quick answer! |
Beta Was this translation helpful? Give feedback.
You need to remove whole
stmt
for that,eg, if it insideExpression
, then do:First, define the node types as
Expression
stmt:Next, verify if the node expr is match with
MethodCall
Above is pseudo code, but you have the idea :)