Skip to content

Commit b0dafcc

Browse files
committed
Allow registering of custom rich parser node visitors
1 parent bf4bd86 commit b0dafcc

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

conf/config.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ services:
360360

361361
-
362362
class: PhpParser\NodeVisitor\NodeConnectingVisitor
363+
tags:
364+
- phpstan.parser.richParserNodeVisitor
363365

364366
-
365367
class: PhpParser\PrettyPrinter\Standard
@@ -601,6 +603,8 @@ services:
601603

602604
-
603605
class: PHPStan\NodeVisitor\StatementOrderVisitor
606+
tags:
607+
- phpstan.parser.richParserNodeVisitor
604608

605609
-
606610
class: PHPStan\Parallel\ParallelAnalyser

src/DependencyInjection/ConditionalTagsExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Nette\Schema\Expect;
88
use PHPStan\Analyser\TypeSpecifierFactory;
99
use PHPStan\Broker\BrokerFactory;
10+
use PHPStan\Parser\RichParser;
1011
use PHPStan\PhpDoc\TypeNodeResolverExtension;
1112
use PHPStan\Rules\RegistryFactory;
1213
use PHPStan\ShouldNotHappenException;
@@ -31,6 +32,7 @@ public function getConfigSchema(): Nette\Schema\Schema
3132
TypeSpecifierFactory::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
3233
TypeSpecifierFactory::METHOD_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
3334
TypeSpecifierFactory::STATIC_METHOD_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
35+
RichParser::VISITOR_SERVICE_TAG => $bool,
3436
])->min(1));
3537
}
3638

src/Parser/RichParser.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
use PhpParser\Node;
88
use PhpParser\NodeTraverser;
99
use PhpParser\NodeVisitor\NameResolver;
10-
use PhpParser\NodeVisitor\NodeConnectingVisitor;
10+
use PHPStan\DependencyInjection\Container;
1111
use PHPStan\File\FileReader;
12-
use PHPStan\NodeVisitor\StatementOrderVisitor;
1312
use PHPStan\ShouldNotHappenException;
1413
use function is_string;
1514
use function strpos;
@@ -20,12 +19,13 @@
2019
class RichParser implements Parser
2120
{
2221

22+
public const VISITOR_SERVICE_TAG = 'phpstan.parser.richParserNodeVisitor';
23+
2324
public function __construct(
2425
private \PhpParser\Parser $parser,
2526
private Lexer $lexer,
2627
private NameResolver $nameResolver,
27-
private NodeConnectingVisitor $nodeConnectingVisitor,
28-
private StatementOrderVisitor $statementOrderVisitor,
28+
private Container $container,
2929
)
3030
{
3131
}
@@ -60,8 +60,10 @@ public function parseString(string $sourceCode): array
6060

6161
$nodeTraverser = new NodeTraverser();
6262
$nodeTraverser->addVisitor($this->nameResolver);
63-
$nodeTraverser->addVisitor($this->nodeConnectingVisitor);
64-
$nodeTraverser->addVisitor($this->statementOrderVisitor);
63+
64+
foreach ($this->container->getServicesByTag(self::VISITOR_SERVICE_TAG) as $visitor) {
65+
$nodeTraverser->addVisitor($visitor);
66+
}
6567

6668
/** @var array<Node\Stmt> */
6769
$nodes = $nodeTraverser->traverse($nodes);

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
497497
new Php7($lexer),
498498
$lexer,
499499
new NameResolver(),
500-
new NodeConnectingVisitor(),
501-
new StatementOrderVisitor(),
500+
self::getContainer(),
502501
),
503502
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, $printer)),
504503
$reportUnmatchedIgnoredErrors,

0 commit comments

Comments
 (0)