Skip to content

Commit fe0d47b

Browse files
authored
resolve parent class reflection by class name in Php4ConstructorRector (#8838)
1 parent 45735fb commit fe0d47b

2 files changed

Lines changed: 14 additions & 36 deletions

File tree

‎rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
use PhpParser\Node\Stmt\Class_;
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Expression;
16-
use PHPStan\Analyser\Scope;
1716
use PHPStan\Reflection\ClassReflection;
1817
use Rector\Enum\ObjectReference;
19-
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
2018
use Rector\Php70\NodeAnalyzer\MethodCallNameAnalyzer;
2119
use Rector\Php70\NodeAnalyzer\Php4ConstructorClassMethodAnalyzer;
2220
use Rector\PHPStan\ScopeFetcher;
@@ -34,7 +32,6 @@ final class Php4ConstructorRector extends AbstractRector implements MinPhpVersio
3432
{
3533
public function __construct(
3634
private readonly Php4ConstructorClassMethodAnalyzer $php4ConstructorClassMethodAnalyzer,
37-
private readonly ParentClassScopeResolver $parentClassScopeResolver,
3835
private readonly MethodCallNameAnalyzer $methodCallNameAnalyzer,
3936
) {
4037
}
@@ -102,6 +99,8 @@ public function refactor(Node $node): Class_|null
10299
return null;
103100
}
104101

102+
$parentClassReflection = $classReflection->getParentClass();
103+
105104
foreach ($node->stmts as $classStmtKey => $classStmt) {
106105
if (! $classStmt instanceof ClassMethod) {
107106
continue;
@@ -114,7 +113,7 @@ public function refactor(Node $node): Class_|null
114113
$psr4ConstructorMethod = $classStmt;
115114

116115
// process parent call references first
117-
$this->processClassMethodStatementsForParentConstructorCalls($psr4ConstructorMethod, $scope);
116+
$this->processClassMethodStatementsForParentConstructorCalls($psr4ConstructorMethod, $parentClassReflection);
118117

119118
// does it already have a __construct method?
120119
if (! $node->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) {
@@ -153,8 +152,15 @@ public function refactor(Node $node): Class_|null
153152
return null;
154153
}
155154

156-
private function processClassMethodStatementsForParentConstructorCalls(ClassMethod $classMethod, Scope $scope): void
157-
{
155+
private function processClassMethodStatementsForParentConstructorCalls(
156+
ClassMethod $classMethod,
157+
?ClassReflection $parentClassReflection
158+
): void {
159+
// no parent class
160+
if (! $parentClassReflection instanceof ClassReflection) {
161+
return;
162+
}
163+
158164
foreach ((array) $classMethod->stmts as $methodStmt) {
159165
if (! $methodStmt instanceof Expression) {
160166
continue;
@@ -165,19 +171,12 @@ private function processClassMethodStatementsForParentConstructorCalls(ClassMeth
165171
continue;
166172
}
167173

168-
$this->processParentPhp4ConstructCall($methodStmt, $scope);
174+
$this->processParentPhp4ConstructCall($methodStmt, $parentClassReflection);
169175
}
170176
}
171177

172-
private function processParentPhp4ConstructCall(StaticCall $staticCall, Scope $scope): void
178+
private function processParentPhp4ConstructCall(StaticCall $staticCall, ClassReflection $parentClassReflection): void
173179
{
174-
$parentClassReflection = $this->parentClassScopeResolver->resolveParentClassReflection($scope);
175-
176-
// no parent class
177-
if (! $parentClassReflection instanceof ClassReflection) {
178-
return;
179-
}
180-
181180
if (! $staticCall->class instanceof Name) {
182181
return;
183182
}

‎src/NodeCollector/ScopeResolver/ParentClassScopeResolver.php‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)