Skip to content

Commit c71d9c5

Browse files
committed
[DI] Autodiscover PhpParserNodeMapper, match most specific over registration order
1 parent e8e1c7a commit c71d9c5

3 files changed

Lines changed: 18 additions & 32 deletions

File tree

‎.github/workflows/code_analysis.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
-
5454
name: 'Active Classes'
5555
run: |
56-
vendor/bin/class-leak check bin config src rules utils --skip-suffix "Rector" --skip-type="Rector\\Utils\\Compiler\\Unprefixer" --skip-type="Rector\\NodeCollector\\BinaryOpConditionsCollector" --skip-type="Rector\\Set\\Contract\\SetListInterface" --skip-type="Rector\\DependencyInjection\\PHPStan\\RichParserFactory" --skip-type="Rector\\Utils\\PHPStan\\Tests\\Rule\\SingleServiceRegistrationRule\\Source\\DuplicateRegistrationFactory" --skip-type="Rector\\Utils\\PHPStan\\Tests\\Rule\\SingleServiceRegistrationRule\\Source\\SingleRegistrationFactory" --skip-type="Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface" --skip-type="Rector\\NodeTypeResolver\\Contract\\NodeTypeResolverInterface" --skip-type="Rector\\CodingStyle\\Contract\\ClassNameImport\\ClassNameImportSkipVoterInterface" --skip-type="Rector\\Php80\\Contract\\ConverterAttributeDecoratorInterface" --skip-type="Rector\\NodeNameResolver\\Contract\\NodeNameResolverInterface" --skip-type="Rector\\StaticTypeMapper\\Contract\\PhpDocParser\\PhpDocTypeMapperInterface" --skip-type="Rector\\PhpAttribute\\Contract\\AnnotationToAttributeMapperInterface"
56+
vendor/bin/class-leak check bin config src rules utils --skip-suffix "Rector" --skip-type="Rector\\Utils\\Compiler\\Unprefixer" --skip-type="Rector\\NodeCollector\\BinaryOpConditionsCollector" --skip-type="Rector\\Set\\Contract\\SetListInterface" --skip-type="Rector\\DependencyInjection\\PHPStan\\RichParserFactory" --skip-type="Rector\\Utils\\PHPStan\\Tests\\Rule\\SingleServiceRegistrationRule\\Source\\DuplicateRegistrationFactory" --skip-type="Rector\\Utils\\PHPStan\\Tests\\Rule\\SingleServiceRegistrationRule\\Source\\SingleRegistrationFactory" --skip-type="Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface" --skip-type="Rector\\NodeTypeResolver\\Contract\\NodeTypeResolverInterface" --skip-type="Rector\\CodingStyle\\Contract\\ClassNameImport\\ClassNameImportSkipVoterInterface" --skip-type="Rector\\Php80\\Contract\\ConverterAttributeDecoratorInterface" --skip-type="Rector\\NodeNameResolver\\Contract\\NodeNameResolverInterface" --skip-type="Rector\\StaticTypeMapper\\Contract\\PhpDocParser\\PhpDocTypeMapperInterface" --skip-type="Rector\\PhpAttribute\\Contract\\AnnotationToAttributeMapperInterface" --skip-type="Rector\\StaticTypeMapper\\Contract\\PhpParser\\PhpParserNodeMapperInterface"
5757
5858
-
5959
name: 'Compatible PHPStan versions'

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,6 @@
108108
use Rector\Rector\AbstractRector;
109109
use Rector\Skipper\Skipper\Skipper;
110110
use Rector\Skipper\Skipper\UsedSkipCollector;
111-
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
112-
use Rector\StaticTypeMapper\PhpParser\ExprNodeMapper;
113-
use Rector\StaticTypeMapper\PhpParser\FullyQualifiedNodeMapper;
114-
use Rector\StaticTypeMapper\PhpParser\IdentifierNodeMapper;
115-
use Rector\StaticTypeMapper\PhpParser\IntersectionTypeNodeMapper;
116-
use Rector\StaticTypeMapper\PhpParser\NameNodeMapper;
117-
use Rector\StaticTypeMapper\PhpParser\NullableTypeNodeMapper;
118-
use Rector\StaticTypeMapper\PhpParser\StringNodeMapper;
119-
use Rector\StaticTypeMapper\PhpParser\UnionTypeNodeMapper;
120111
use Symfony\Component\Console\Application;
121112
use Symfony\Component\Console\Style\SymfonyStyle;
122113
use Webmozart\Assert\Assert;
@@ -210,20 +201,6 @@ final class LazyContainerFactory
210201
PhpVersionFactory::class,
211202
];
212203

213-
/**
214-
* @var array<class-string<PhpParserNodeMapperInterface>>
215-
*/
216-
private const array PHP_PARSER_NODE_MAPPER_CLASSES = [
217-
FullyQualifiedNodeMapper::class,
218-
IdentifierNodeMapper::class,
219-
IntersectionTypeNodeMapper::class,
220-
NameNodeMapper::class,
221-
NullableTypeNodeMapper::class,
222-
StringNodeMapper::class,
223-
UnionTypeNodeMapper::class,
224-
ExprNodeMapper::class,
225-
];
226-
227204
/**
228205
* @api used as next rectorConfig factory
229206
*/
@@ -370,11 +347,7 @@ static function (AbstractRector $rector) use ($rectorConfig): void {
370347
}
371348
);
372349

373-
$this->registerTagged(
374-
$rectorConfig,
375-
self::PHP_PARSER_NODE_MAPPER_CLASSES,
376-
PhpParserNodeMapperInterface::class
377-
);
350+
$rectorConfig->autodiscover(__DIR__ . '/../StaticTypeMapper/PhpParser');
378351

379352
$this->registerTagged(
380353
$rectorConfig,

‎src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,27 @@ public function __construct(
2121

2222
public function mapToPHPStanType(Node $node): Type
2323
{
24+
$matchedNodeMapper = null;
25+
$matchedNodeType = null;
26+
2427
foreach ($this->phpParserNodeMappers as $phpParserNodeMapper) {
25-
if (! is_a($node, $phpParserNodeMapper->getNodeType())) {
28+
$nodeType = $phpParserNodeMapper->getNodeType();
29+
if (! is_a($node, $nodeType)) {
2630
continue;
2731
}
2832

29-
return $phpParserNodeMapper->mapToPHPStan($node);
33+
// pick the most specific mapper: a mapper for a child node wins over one for its
34+
// parent node, regardless of registration order
35+
if ($matchedNodeType === null || is_a($nodeType, $matchedNodeType, true)) {
36+
$matchedNodeType = $nodeType;
37+
$matchedNodeMapper = $phpParserNodeMapper;
38+
}
39+
}
40+
41+
if (! $matchedNodeMapper instanceof PhpParserNodeMapperInterface) {
42+
throw new NotImplementedYetException($node::class);
3043
}
3144

32-
throw new NotImplementedYetException($node::class);
45+
return $matchedNodeMapper->mapToPHPStan($node);
3346
}
3447
}

0 commit comments

Comments
 (0)