Skip to content

Error when removing a MethodCall node, in a custom rule #8376

Closed Answered by samsonasik
nikophil asked this question in Q&A
Discussion options

You must be logged in to vote

You need to remove whole stmt for that,eg, if it inside Expression, then do:

First, define the node types as Expression stmt:

    public function getNodeTypes(): array
    {
        return [\PhpParser\Node\Stmt\Expression::class];
    }

Next, verify if the node expr is match with MethodCall

    /**
     * @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 :)

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by samsonasik
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #8375 on December 28, 2023 11:38.