PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Suggestion: here's a "recursive traverser" and "next/prev attributes" that some might find useful ...

Open SundialServices opened this issue 7 years ago • 2 comments

RecursiveTraverser.php.zip

I developed a very simple "recursive traverser" in order to build a program that, upon encountering an include() or require() directive, could immediately traverse into what it contained. It simply modifies the regular traverser so that, if leaveNode returns an array, that array is immediately traversed.

Later on, I found that I had the need to support next and prev attributes to let me walk the completed tree – without using actual references so that print_r() wouldn't recurse itself to death. This I did by placing the references in a separate array and storing the indexes to that array as the values of the attributes.

I'd like to suggest that these simple alternatives ought to become "official." When them, I was able to build many important tools – an undefined-variables finder, a "register globals" replacement calculator, and so on. Note that they do presume that the tree structure being walked is not being changed by the walker – which your "official" walkers, of course, specifically provide for.

(The attached file was last modified in July, 2017 and won't reflect any changes you've made to the underlying classes since then.)

SundialServices avatar Mar 23 '18 14:03 SundialServices

Why not send a PR with the code? It would be much easier to review and feedback on

TomasVotruba avatar Apr 14 '18 21:04 TomasVotruba

I guess this one can be closed after 4 years.

TomasVotruba avatar May 13 '22 12:05 TomasVotruba

There already are visitors for adding parent/next/prev attributes (ParentConnectingVisitor, NodeConnectingVisitor). Traversal past include/require is something I would consider out of scope for this project, because it requires filesystem resolution, and has limited relevance nowadays (in the age of autoloading).

nikic avatar Sep 03 '22 13:09 nikic