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! |
Answered by
samsonasik
Dec 28, 2023
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 :) |
0 replies
Answer selected by
samsonasik
|
yes, this makes sense! thanks for this quick answer! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to remove whole
stmtfor that,eg, if it insideExpression, then do:First, define the node types as
Expressionstmt:Next, verify if the node expr is match with
MethodCallAbove is pseudo code, but you have the idea :)