Skip to content

Commit 02dfcad

Browse files
authored
Fix RemoveParentCallWithoutParentRector removing valid call when ancestor hierarchy is unresolvable (#8018)
1 parent 6fe25a4 commit 02dfcad

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Fixture;
4+
5+
use Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Source\SkipBrokenHierarchyParent;
6+
7+
class SkipBrokenHierarchy extends SkipBrokenHierarchyParent
8+
{
9+
public function run()
10+
{
11+
parent::run();
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Source;
4+
5+
// the grandparent class is not autoloadable, so the ancestor chain cannot be
6+
// fully resolved and the called method cannot be safely declared as missing
7+
abstract class SkipBrokenHierarchyParent extends NonAutoloadableGrandParent
8+
{
9+
}

‎rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\Enum\ObjectReference;
1717
use Rector\NodeAnalyzer\ClassAnalyzer;
1818
use Rector\NodeManipulator\ClassMethodManipulator;
19+
use Rector\PhpParser\AstResolver;
1920
use Rector\Rector\AbstractRector;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -28,7 +29,8 @@ final class RemoveParentCallWithoutParentRector extends AbstractRector
2829
public function __construct(
2930
private readonly ClassMethodManipulator $classMethodManipulator,
3031
private readonly ClassAnalyzer $classAnalyzer,
31-
private readonly ReflectionProvider $reflectionProvider
32+
private readonly ReflectionProvider $reflectionProvider,
33+
private readonly AstResolver $astResolver
3234
) {
3335
}
3436

@@ -160,6 +162,36 @@ private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $c
160162
return false;
161163
}
162164

163-
return $this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $calledMethodName);
165+
if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $calledMethodName)) {
166+
return true;
167+
}
168+
169+
// the called method may be defined in an ancestor that cannot be resolved
170+
// (e.g. a grandparent class is not autoloadable); in that case we cannot
171+
// safely tell the method does not exist, so the call must not be removed
172+
return $this->hasUnresolvableAncestor($class->extends);
173+
}
174+
175+
private function hasUnresolvableAncestor(Name $parentName): bool
176+
{
177+
$parentClassName = $this->getName($parentName);
178+
if (! is_string($parentClassName)) {
179+
return false;
180+
}
181+
182+
if (! $this->reflectionProvider->hasClass($parentClassName)) {
183+
return true;
184+
}
185+
186+
$parentClass = $this->astResolver->resolveClassFromName($parentClassName);
187+
if (! $parentClass instanceof Class_) {
188+
return false;
189+
}
190+
191+
if (! $parentClass->extends instanceof Name) {
192+
return false;
193+
}
194+
195+
return $this->hasUnresolvableAncestor($parentClass->extends);
164196
}
165197
}

0 commit comments

Comments
 (0)