nikic / PHP-Parser

A PHP parser written in PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Appending new statements

sergio-fferreira opened this issue · comments

Let's assume I have the following code:

func1();
func2();

After parsing and finding the first statement, func1();, I want to append a sibling statement. For example:

func1();
anotherFunc();
func2();

Is this possible? As far as I know, $node->getAttribute('next') only stores the next sibling node in an attribute, which is useful if I want to modify the next sibling. However, appending a new statement after the current statement appears not to be possible.

Thank you in advance.

There's indeed no straightforward way to do this. You'd have to go to the parent node, find where next is located in it, and insert the new node there.

Thank you!