Skip to content

Commit 3d0786d

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

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
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",

rules/CodingStyle/Rector/Enum_/EnumCaseToPascalCaseRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ private function convertToPascalCase(string $name): string
208208
fn ($part): string =>
209209
// If part is all uppercase, convert to ucfirst(strtolower())
210210
// If part is already mixed or PascalCase, keep as is except ucfirst
211-
ctype_upper($part)
212-
? ucfirst(strtolower($part))
213-
: ucfirst($part),
211+
ctype_upper((string) $part)
212+
? ucfirst(strtolower((string) $part))
213+
: ucfirst((string) $part),
214214
$parts
215215
)
216216
);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DependencyInjection\PHPStan;
6+
7+
use PHPStan\DependencyInjection\MemoizingContainer;
8+
use PHPStan\DependencyInjection\Nette\NetteContainer;
9+
use PHPStan\Parser\AnonymousClassVisitor;
10+
use PHPStan\Parser\RichParser;
11+
use Rector\Util\Reflection\PrivatesAccessor;
12+
13+
/**
14+
* Helper service to modify PHPStan container
15+
* To avoid issues caused by node replacement, like @see https://github.com/rectorphp/rector/issues/9492
16+
*/
17+
final class PHPStanContainerMemento
18+
{
19+
public static function removeRichVisitors(RichParser $richParser): void
20+
{
21+
// the only way now seems to access container early and remove unwanted services
22+
// here https://github.com/phpstan/phpstan-src/blob/522421b007cbfc674bebb93e823c774167ac78cd/src/Parser/RichParser.php#L90-L92
23+
$privatesAccessor = new PrivatesAccessor();
24+
25+
/** @var MemoizingContainer $container */
26+
$container = $privatesAccessor->getPrivateProperty($richParser, 'container');
27+
28+
/** @var NetteContainer $originalContainer */
29+
$originalContainer = $privatesAccessor->getPrivateProperty($container, 'originalContainer');
30+
31+
/** @var NetteContainer $originalContainer */
32+
$deeperContainer = $privatesAccessor->getPrivateProperty($originalContainer, 'container');
33+
34+
// get tags property
35+
$tags = $privatesAccessor->getPrivateProperty($deeperContainer, 'tags');
36+
37+
// keep only the anonymous class visitor
38+
// remove all the rest, https://github.com/phpstan/phpstan-src/tree/1d86de8bb9371534983a8dbcd879e057d2ff028f/src/Parser
39+
$tags[RichParser::VISITOR_SERVICE_TAG] = [
40+
$container->findServiceNamesByType(AnonymousClassVisitor::class)[0] => true,
41+
];
42+
43+
$privatesAccessor->setPrivateProperty($deeperContainer, 'tags', $tags);
44+
}
45+
}

src/PhpParser/Parser/RectorParser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88
use PhpParser\ParserFactory;
99
use PhpParser\PhpVersion;
1010
use PHPStan\Parser\Parser;
11+
use PHPStan\Parser\RichParser;
12+
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
1113
use Rector\PhpParser\ValueObject\StmtsAndTokens;
1214
use Rector\Util\Reflection\PrivatesAccessor;
1315

1416
final readonly class RectorParser
1517
{
18+
/**
19+
* @param RichParser $parser
20+
*/
1621
public function __construct(
1722
private Parser $parser,
1823
private PrivatesAccessor $privatesAccessor
1924
) {
25+
26+
PHPStanContainerMemento::removeRichVisitors($parser);
2027
}
2128

2229
/**

tests/PhpParser/Printer/PHPStanPrinterTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use PhpParser\PrettyPrinter\Standard;
88
use PHPStan\Parser\Parser;
99
use PHPStan\Parser\RichParser;
10+
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
1011
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
11-
<<<<<<< HEAD
1212
use ReflectionProperty;
13-
=======
14-
>>>>>>> ea9a6ad8a5 ([issue 9492] create reproducer for modified array_map args, as creates changed args on print)
1513

1614
/**
1715
* Test case for: https://github.com/rectorphp/rector/issues/9492
1816
* Most likely caused by https://github.com/phpstan/phpstan-src/pull/3763
17+
*
18+
* @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/ArrayMapArgVisitor.php
1919
*/
2020
final class PHPStanPrinterTest extends AbstractLazyTestCase
2121
{
@@ -24,33 +24,22 @@ public function testAddingCommentOnSomeNodesFail(): void
2424
/** @var RichParser $phpstanParser */
2525
$phpstanParser = $this->make(Parser::class);
2626

27+
PHPStanContainerMemento::removeRichVisitors($phpstanParser);
28+
2729
$stmts = $phpstanParser->parseFile(__DIR__ . '/Fixture/some_array_map.php');
2830

2931
// get private property "parser"
30-
<<<<<<< HEAD
3132
$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)
3533

3634
/** @var \PhpParser\Parser $innerParser */
3735
$innerParser = $parserReflectionProperty->getValue($phpstanParser);
3836
$tokens = $innerParser->getTokens();
3937

40-
<<<<<<< HEAD
4138
$standard = new Standard([]);
4239
$printerContents = $standard->printFormatPreserving($stmts, $stmts, $tokens);
4340

4441
$newlineNormalizedContents = str_replace("\r\n", PHP_EOL, $printerContents);
4542

4643
$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)
5544
}
5645
}

0 commit comments

Comments
 (0)