77use PhpParser \PrettyPrinter \Standard ;
88use PHPStan \DependencyInjection \MemoizingContainer ;
99use PHPStan \DependencyInjection \Nette \NetteContainer ;
10+ use PHPStan \Parser \AnonymousClassVisitor ;
11+ use PHPStan \Parser \ArrayMapArgVisitor ;
1012use PHPStan \Parser \Parser ;
1113use PHPStan \Parser \RichParser ;
1214use Rector \Testing \PHPUnit \AbstractLazyTestCase ;
1517/**
1618 * Test case for: https://github.com/rectorphp/rector/issues/9492
1719 * Most likely caused by https://github.com/phpstan/phpstan-src/pull/3763
20+ *
21+ * @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/ArrayMapArgVisitor.php
1822 */
1923final class PHPStanPrinterTest extends AbstractLazyTestCase
2024{
@@ -23,6 +27,30 @@ public function testAddingCommentOnSomeNodesFail(): void
2327 /** @var RichParser $phpstanParser */
2428 $ phpstanParser = $ this ->make (Parser::class);
2529
30+ $ this ->removeNodeVisitorFromPHPStanParser ($ phpstanParser , [ArrayMapArgVisitor::class]);
31+
32+ $ stmts = $ phpstanParser ->parseFile (__DIR__ . '/Fixture/some_array_map.php ' );
33+
34+ // get private property "parser"
35+ $ parserReflectionProperty = new \ReflectionProperty (RichParser::class, 'parser ' );
36+
37+ /** @var \PhpParser\Parser $innerParser */
38+ $ innerParser = $ parserReflectionProperty ->getValue ($ phpstanParser );
39+ $ tokens = $ innerParser ->getTokens ();
40+
41+ $ standardPrinter = new Standard ([
42+ 'newline ' => "\n" ,
43+ ]);
44+ $ printerContents = $ standardPrinter ->printFormatPreserving ($ stmts , $ stmts , $ tokens );
45+
46+ $ this ->assertStringEqualsFile (__DIR__ . '/Fixture/some_array_map.php ' , $ printerContents );
47+ }
48+
49+ /**
50+ * @param class-string[] $nodeVisitorsToRemove
51+ */
52+ private function removeNodeVisitorFromPHPStanParser (RichParser $ phpstanParser , array $ nodeVisitorsToRemove ): void
53+ {
2654 // the only way now seems to access container early and remove unwanted services
2755 // here https://github.com/phpstan/phpstan-src/blob/522421b007cbfc674bebb93e823c774167ac78cd/src/Parser/RichParser.php#L90-L92
2856 $ privatesAccessor = new PrivatesAccessor ();
@@ -36,31 +64,15 @@ public function testAddingCommentOnSomeNodesFail(): void
3664 /** @var NetteContainer $originalContainer */
3765 $ deeperContainer = $ privatesAccessor ->getPrivateProperty ($ originalContainer , 'container ' );
3866
39- // @todo
40-
4167 // get tags property
4268 $ tags = $ privatesAccessor ->getPrivateProperty ($ deeperContainer , 'tags ' );
4369
44- dump ($ tags );
45- die;
46-
47- dump ($ container ->getServicesByTag (RichParser::VISITOR_SERVICE_TAG ));
48- die;
49-
50- $ stmts = $ phpstanParser ->parseFile (__DIR__ . '/Fixture/some_array_map.php ' );
51-
52- // get private property "parser"
53- $ parserReflectionProperty = new \ReflectionProperty (RichParser::class, 'parser ' );
54-
55- /** @var \PhpParser\Parser $innerParser */
56- $ innerParser = $ parserReflectionProperty ->getValue ($ phpstanParser );
57- $ tokens = $ innerParser ->getTokens ();
70+ // keep only the anonymous class visitor
71+ // remove all the rest, https://github.com/phpstan/phpstan-src/tree/1d86de8bb9371534983a8dbcd879e057d2ff028f/src/Parser
72+ $ tags [RichParser::VISITOR_SERVICE_TAG ] = [
73+ $ container ->findServiceNamesByType (AnonymousClassVisitor::class)[0 ] => true ,
74+ ];
5875
59- $ standardPrinter = new Standard ([
60- 'newline ' => "\n" ,
61- ]);
62- $ printerContents = $ standardPrinter ->printFormatPreserving ($ stmts , $ stmts , $ tokens );
63-
64- $ this ->assertStringEqualsFile (__DIR__ . '/Fixture/some_array_map.php ' , $ printerContents );
76+ $ privatesAccessor ->setPrivateProperty ($ deeperContainer , 'tags ' , $ tags );
6577 }
6678}
0 commit comments