Skip to content

Commit e7b92c2

Browse files
committed
try to remove breaking service from container
1 parent 2d045f6 commit e7b92c2

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nikic/php-parser": "^5.6.2",
2424
"ondram/ci-detector": "^4.2",
2525
"phpstan/phpdoc-parser": "^2.3",
26-
"phpstan/phpstan": "2.1.19 as 2.1.32",
26+
"phpstan/phpstan": "^2.1.32",
2727
"react/event-loop": "^1.6",
2828
"react/promise": "^3.3",
2929
"react/socket": "^1.17",

tests/PhpParser/Printer/PHPStanPrinterTest.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
namespace Rector\Tests\PhpParser\Printer;
66

77
use PhpParser\PrettyPrinter\Standard;
8+
use PHPStan\DependencyInjection\MemoizingContainer;
9+
use PHPStan\DependencyInjection\Nette\NetteContainer;
10+
use PHPStan\Parser\AnonymousClassVisitor;
11+
use PHPStan\Parser\ArrayMapArgVisitor;
812
use PHPStan\Parser\Parser;
913
use PHPStan\Parser\RichParser;
1014
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
11-
<<<<<<< HEAD
15+
use Rector\Util\Reflection\PrivatesAccessor;
1216
use 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
*/
2024
final 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

Comments
 (0)