Skip to content
Closed
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,10 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;

final class SkipUnresolvableInterface implements \App\OutOfScope\PostingInterface
{
public function submitPosting(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;

final class SkipUnresolvableParent extends \App\OutOfScope\AbstractPosting
{
protected function submitPosting(): void
{
}
}
27 changes: 27 additions & 0 deletions rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
return true;
}

// parent class or interface is out of analyzed scope, keep method as it may implement an abstract requirement
if ($this->hasUnresolvableParentOrInterface($class, $classMethod, $scope->getClassReflection())) {
return true;
}

if ($this->paramAnalyzer->hasPropertyPromotion($classMethod->params)) {
return true;
}
Expand Down Expand Up @@ -184,6 +189,28 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
return $this->isAttributeMarkerConstructor($classMethod, $classReflection);
}

private function hasUnresolvableParentOrInterface(
Class_ $class,
ClassMethod $classMethod,
?ClassReflection $classReflection
): bool {
if (! $classReflection instanceof ClassReflection) {
return false;
}

// a protected empty method only exists to override/implement a parent's method;
// if the parent is out of scope, keep it to avoid breaking an abstract requirement
if ($classMethod->isProtected() && $class->extends instanceof Name && ! $classReflection->getParentClass() instanceof ClassReflection) {
return true;
}

$interfaceNames = array_map(
static fn (ClassReflection $classReflection): string => $classReflection->getName(),
$classReflection->getInterfaces()
);
return array_any($class->implements, fn (Name $name): bool => ! in_array($this->getName($name), $interfaceNames, true));
}

private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
Expand Down
Loading