Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Fixture;

use Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Source\SkipBrokenHierarchyParent;

class SkipBrokenHierarchy extends SkipBrokenHierarchyParent
{
public function run()
{
parent::run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector\Source;

// the grandparent class is not autoloadable, so the ancestor chain cannot be
// fully resolved and the called method cannot be safely declared as missing
abstract class SkipBrokenHierarchyParent extends NonAutoloadableGrandParent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Enum\ObjectReference;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeManipulator\ClassMethodManipulator;
use Rector\PhpParser\AstResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -28,7 +29,8 @@ final class RemoveParentCallWithoutParentRector extends AbstractRector
public function __construct(
private readonly ClassMethodManipulator $classMethodManipulator,
private readonly ClassAnalyzer $classAnalyzer,
private readonly ReflectionProvider $reflectionProvider
private readonly ReflectionProvider $reflectionProvider,
private readonly AstResolver $astResolver
) {
}

Expand Down Expand Up @@ -160,6 +162,36 @@ private function doesCalledMethodExistInParent(StaticCall $staticCall, Class_ $c
return false;
}

return $this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $calledMethodName);
if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($class, $calledMethodName)) {
return true;
}

// the called method may be defined in an ancestor that cannot be resolved
// (e.g. a grandparent class is not autoloadable); in that case we cannot
// safely tell the method does not exist, so the call must not be removed
return $this->hasUnresolvableAncestor($class->extends);
}

private function hasUnresolvableAncestor(Name $parentName): bool
{
$parentClassName = $this->getName($parentName);
if (! is_string($parentClassName)) {
return false;
}

if (! $this->reflectionProvider->hasClass($parentClassName)) {
return true;
}

$parentClass = $this->astResolver->resolveClassFromName($parentClassName);
if (! $parentClass instanceof Class_) {
return false;
}

if (! $parentClass->extends instanceof Name) {
return false;
}

return $this->hasUnresolvableAncestor($parentClass->extends);
}
}
Loading