Skip to content

[DI] Autodiscover PhpParserNodeMapper via most-specific match - #8374

Merged
TomasVotruba merged 2 commits into
mainfrom
phpparsernodemapper-autodiscover
Aug 25, 2026
Merged

[DI] Autodiscover PhpParserNodeMapper via most-specific match#8374
TomasVotruba merged 2 commits into
mainfrom
phpparsernodemapper-autodiscover

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Second of three parallel experiments removing order-sensitive registerTagged() collections in favor of autodiscover().

PhpParserNodeMapper matched by first is_a() match in registration order, so FullyQualifiedNodeMapper had to be registered before NameNodeMapper (FullyQualified extends Name) and StringNodeMapper before ExprNodeMapper (String_ extends Expr). autodiscover() filesystem order does not honor that.

Fix removes the order dependency: the consumer picks the most specific matching mapper (deepest node type), so order stops mattering.

foreach ($this->phpParserNodeMappers as $phpParserNodeMapper) {
    $nodeType = $phpParserNodeMapper->getNodeType();
    if (! is_a($node, $nodeType)) {
        continue;
    }
    // a mapper for a child node wins over one for its parent, regardless of registration order
    if ($matchedNodeType === null || is_a($nodeType, $matchedNodeType, true)) {
        $matchedNodeType = $nodeType;
        $matchedNodeMapper = $phpParserNodeMapper;
    }
}

PHP_PARSER_NODE_MAPPER_CLASSES collapses to one autodiscover() call.

Verified directly:

Node Resolves to
FullyQualified FullyQualifiedObjectType (not the generic Name mapper)
String_ StringType (not the Expr mapper)
Name MixedType

Interface added to the class-leak skip list.

@TomasVotruba
TomasVotruba force-pushed the phpparsernodemapper-autodiscover branch 2 times, most recently from fe9f427 to c71d9c5 Compare August 25, 2026 07:32
@TomasVotruba
TomasVotruba force-pushed the phpparsernodemapper-autodiscover branch from d789756 to f10682b Compare August 25, 2026 08:08
@TomasVotruba
TomasVotruba merged commit 28b18db into main Aug 25, 2026
43 of 44 checks passed
@TomasVotruba
TomasVotruba deleted the phpparsernodemapper-autodiscover branch August 25, 2026 08:11
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