Skip to content

Commit ef3aa32

Browse files
committed
fixup! try to remove breaking service from container
1 parent e7b92c2 commit ef3aa32

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
*/
16+
final class PHPStanContainerMemento
17+
{
18+
public static function removeRichVisitors(RichParser $richParser): void
19+
{
20+
// the only way now seems to access container early and remove unwanted services
21+
// here https://github.com/phpstan/phpstan-src/blob/522421b007cbfc674bebb93e823c774167ac78cd/src/Parser/RichParser.php#L90-L92
22+
$privatesAccessor = new PrivatesAccessor();
23+
24+
/** @var MemoizingContainer $container */
25+
$container = $privatesAccessor->getPrivateProperty($richParser, 'container');
26+
27+
/** @var NetteContainer $originalContainer */
28+
$originalContainer = $privatesAccessor->getPrivateProperty($container, 'originalContainer');
29+
30+
/** @var NetteContainer $originalContainer */
31+
$deeperContainer = $privatesAccessor->getPrivateProperty($originalContainer, 'container');
32+
33+
// get tags property
34+
$tags = $privatesAccessor->getPrivateProperty($deeperContainer, 'tags');
35+
36+
// keep only the anonymous class visitor
37+
// remove all the rest, https://github.com/phpstan/phpstan-src/tree/1d86de8bb9371534983a8dbcd879e057d2ff028f/src/Parser
38+
$tags[RichParser::VISITOR_SERVICE_TAG] = [
39+
$container->findServiceNamesByType(AnonymousClassVisitor::class)[0] => true,
40+
];
41+
42+
$privatesAccessor->setPrivateProperty($deeperContainer, 'tags', $tags);
43+
}
44+
}

src/PhpParser/Parser/RectorParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\ParserFactory;
99
use PhpParser\PhpVersion;
1010
use PHPStan\Parser\Parser;
11+
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
1112
use Rector\PhpParser\ValueObject\StmtsAndTokens;
1213
use Rector\Util\Reflection\PrivatesAccessor;
1314

@@ -17,6 +18,7 @@ public function __construct(
1718
private Parser $parser,
1819
private PrivatesAccessor $privatesAccessor
1920
) {
21+
PHPStanContainerMemento::removeRichVisitors($parser);
2022
}
2123

2224
/**

tests/PhpParser/Printer/PHPStanPrinterTest.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
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;
128
use PHPStan\Parser\Parser;
139
use PHPStan\Parser\RichParser;
10+
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
1411
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
15-
use Rector\Util\Reflection\PrivatesAccessor;
1612
use ReflectionProperty;
1713

1814
/**
@@ -28,7 +24,7 @@ public function testAddingCommentOnSomeNodesFail(): void
2824
/** @var RichParser $phpstanParser */
2925
$phpstanParser = $this->make(Parser::class);
3026

31-
$this->removeNodeVisitorFromPHPStanParser($phpstanParser, [ArrayMapArgVisitor::class]);
27+
PHPStanContainerMemento::removeRichVisitors($phpstanParser);
3228

3329
$stmts = $phpstanParser->parseFile(__DIR__ . '/Fixture/some_array_map.php');
3430

@@ -46,34 +42,4 @@ public function testAddingCommentOnSomeNodesFail(): void
4642

4743
$this->assertStringEqualsFile(__DIR__ . '/Fixture/some_array_map.php', $newlineNormalizedContents);
4844
}
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);
78-
}
7945
}

0 commit comments

Comments
 (0)