2

Im using https://github.com/nikic/PHP-Parser. What is a good strategy when wanting to INSERT a node in the AST? With the traverser I can UPDATE and DELETE nodes easily using a NodeTraverser class. But how can I "INSERT before" or "INSERT after" a node?

Example: When traversing an AST namespace I want to INSERT a Use statement just before the first non-use statement.

I started working with beforeTraverse and afterTraverse to find indexes of arrays but it seems overly complicated. Any ideas?

1
  • I suppose you need enterNode and check it's type. Commented Dec 21, 2019 at 10:17

1 Answer 1

2

It is possible to replace one node with multiple nodes. This only works inside leaveNode and only if the parent structure is an array.

public function leaveNode(Node $node) {
    if ($node instanceof Node\Stmt\Return_ && $node->expr !== null) {
        // Convert "return foo();" into "$retval = foo(); return $retval;"
        $var = new Node\Expr\Variable('retval');
        return [
            new Node\Stmt\Expression(new Node\Expr\Assign($var, $node->expr)),
            new Node\Stmt\Return_($var),
        ];
    }
}

See last section in Modyfing the AST

Sign up to request clarification or add additional context in comments.

1 Comment

Insufficient in my case. I need to add a use statement in the namespace, but ONLY IF the alias will actually be used in the class defined later inside that same Namespace_ node. At the moment of leave()ing the Use_ node, I haven't even begun visiting the Class_ node and determining whether I will be making changes to it that will make use of the class I might want to Use_. So I must record that information in the visitor, and apply it when leaving the Namespace_ node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.