55namespace Rector \Tests \PhpParser \Printer ;
66
77use PhpParser \PrettyPrinter \Standard ;
8+ use PHPStan \DependencyInjection \MemoizingContainer ;
9+ use PHPStan \DependencyInjection \Nette \NetteContainer ;
10+ use PHPStan \Parser \AnonymousClassVisitor ;
11+ use PHPStan \Parser \ArrayMapArgVisitor ;
812use PHPStan \Parser \Parser ;
913use PHPStan \Parser \RichParser ;
1014use Rector \Testing \PHPUnit \AbstractLazyTestCase ;
11- <<<<<<< HEAD
15+ use Rector \ Util \ Reflection \ PrivatesAccessor ;
1216use ReflectionProperty ;
13- =======
14- >>>>>>> ea9a6ad8a5 ([issue 9492 ] create reproducer for modified array_map args, as creates changed args on print )
1517
1618/**
1719 * Test case for: https://github.com/rectorphp/rector/issues/9492
1820 * Most likely caused by https://github.com/phpstan/phpstan-src/pull/3763
21+ *
22+ * @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/ArrayMapArgVisitor.php
1923 */
2024final class PHPStanPrinterTest extends AbstractLazyTestCase
2125{
@@ -24,33 +28,52 @@ public function testAddingCommentOnSomeNodesFail(): void
2428 /** @var RichParser $phpstanParser */
2529 $ phpstanParser = $ this ->make (Parser::class);
2630
31+ $ this ->removeNodeVisitorFromPHPStanParser ($ phpstanParser , [ArrayMapArgVisitor::class]);
32+
2733 $ stmts = $ phpstanParser ->parseFile (__DIR__ . '/Fixture/some_array_map.php ' );
2834
2935 // get private property "parser"
30- <<<<<<< HEAD
3136 $ parserReflectionProperty = new ReflectionProperty (RichParser::class, 'parser ' );
32- =======
33- $ parserReflectionProperty = new \ReflectionProperty (RichParser::class, 'parser ' );
34- >>>>>>> ea9a6ad8a5 ([issue 9492 ] create reproducer for modified array_map args, as creates changed args on print )
3537
3638 /** @var \PhpParser\Parser $innerParser */
3739 $ innerParser = $ parserReflectionProperty ->getValue ($ phpstanParser );
3840 $ tokens = $ innerParser ->getTokens ();
3941
40- <<<<<<< HEAD
4142 $ standard = new Standard ([]);
4243 $ printerContents = $ standard ->printFormatPreserving ($ stmts , $ stmts , $ tokens );
4344
4445 $ newlineNormalizedContents = str_replace ("\r\n" , PHP_EOL , $ printerContents );
4546
4647 $ this ->assertStringEqualsFile (__DIR__ . '/Fixture/some_array_map.php ' , $ newlineNormalizedContents );
47- =======
48- $ standardPrinter = new Standard ([
49- 'newline ' => "\n" ,
50- ]);
51- $ printerContents = $ standardPrinter ->printFormatPreserving ($ stmts , $ stmts , $ tokens );
52-
53- $ this ->assertStringEqualsFile (__DIR__ . '/Fixture/some_array_map.php ' , $ printerContents );
54- >>>>>>> ea9a6ad8a5 ([issue 9492 ] create reproducer for modified array_map args, as creates changed args on print )
48+ }
49+
50+ /**
51+ * @param class-string[] $nodeVisitorsToRemove
52+ */
53+ private function removeNodeVisitorFromPHPStanParser (RichParser $ phpstanParser , array $ nodeVisitorsToRemove ): void
54+ {
55+ // the only way now seems to access container early and remove unwanted services
56+ // here https://github.com/phpstan/phpstan-src/blob/522421b007cbfc674bebb93e823c774167ac78cd/src/Parser/RichParser.php#L90-L92
57+ $ privatesAccessor = new PrivatesAccessor ();
58+
59+ /** @var MemoizingContainer $container */
60+ $ container = $ privatesAccessor ->getPrivateProperty ($ phpstanParser , 'container ' );
61+
62+ /** @var NetteContainer $originalContainer */
63+ $ originalContainer = $ privatesAccessor ->getPrivateProperty ($ container , 'originalContainer ' );
64+
65+ /** @var NetteContainer $originalContainer */
66+ $ deeperContainer = $ privatesAccessor ->getPrivateProperty ($ originalContainer , 'container ' );
67+
68+ // get tags property
69+ $ tags = $ privatesAccessor ->getPrivateProperty ($ deeperContainer , 'tags ' );
70+
71+ // keep only the anonymous class visitor
72+ // remove all the rest, https://github.com/phpstan/phpstan-src/tree/1d86de8bb9371534983a8dbcd879e057d2ff028f/src/Parser
73+ $ tags [RichParser::VISITOR_SERVICE_TAG ] = [
74+ $ container ->findServiceNamesByType (AnonymousClassVisitor::class)[0 ] => true ,
75+ ];
76+
77+ $ privatesAccessor ->setPrivateProperty ($ deeperContainer , 'tags ' , $ tags );
5578 }
5679}
0 commit comments