Skip to content

Use NodeTraverser from phpstan/phpdoc-parser - #8855

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
ondrejmirtes:use-phpdoc-parser-node-traverser
Oct 26, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
ondrejmirtes:use-phpdoc-parser-node-traverser

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Contributor

This PR removes the copy of the node traverser. Rector now uses the traverser from phpstan/phpdoc-parser.

This also fixes a slowdown. 3 decorators used the same PhpDocNodeTraverser service. Each traverseWithCallable() call added one more visitor to it, so each docblock was slower to parse than the one before it. Now each call creates a new traverser. Measurement: parsing 2,000 docblocks took 18.6 s before this change and 0.2 s after it.

Removed:

  • PhpDocNodeTraverser
  • AbstractPhpDocNodeVisitor
  • PhpDocNodeVisitorInterface
  • InvalidTraverseException

traverseWithCallable() is now new NodeTraverser([new CallablePhpDocNodeVisitor(...)]).

The visitors from rector-doctrine, rector-symfony, rector-phpunit and rector-downgrade-php do not use the removed classes.

Changes in behavior

  • The upstream traverser also visits the root node. IntersectionTypeMapper now skips the root node, so it continues to visit the child nodes.
  • PhpDocInfo::removeByType() does not go into @psalm-* and @phpstan-* tags. Before this change, it returned true for these tags, but it did not remove them.
  • If leaveNode() returns REMOVE_NODE for a node in an array, the node is now removed. The old traverser did not do this.

Remove PhpDocNodeTraverser, AbstractPhpDocNodeVisitor,
PhpDocNodeVisitorInterface and InvalidTraverseException.
Use NodeTraverser, AbstractNodeVisitor and NodeVisitor
from phpstan/phpdoc-parser.

Replace traverseWithCallable() with CallablePhpDocNodeVisitor.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17930-0285-73c5-a957-4325c77aa1b2
@TomasVotruba

Copy link
Copy Markdown
Member

Very nice, thanks for addition Ondra 👏 Great to have this right from the core library.

Lets ship it 🚀

@TomasVotruba
TomasVotruba merged commit ec71117 into rectorphp:main Oct 26, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants