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
Expand Up @@ -82,7 +82,8 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($methodNode)) {
$calledMethodName = $this->getName($node->name);
if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($methodNode, $calledMethodName)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

class EdgeCase extends AbstractFilterFactory
{
public function getOrderByIdDesc()
{
return parent::getOrderByColumnDesc('id');
}
}

abstract class AbstractFilterFactory
{
public function getOrderByColumnDesc(string $column)
{
return new OrderByDesc($column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function test(): void
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/parent_but_no_method.php.inc',
__DIR__ . '/Fixture/skip_trait.php.inc',
__DIR__ . '/Fixture/edge_case.php.inc',
]);
}

Expand Down
17 changes: 8 additions & 9 deletions src/PhpParser/Node/Manipulator/ClassMethodManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,30 @@ public function isParameterUsedMethod(Param $param, ClassMethod $classMethod): b
return $this->nameResolver->isNames($param, $arguments);
}

public function hasParentMethodOrInterfaceMethod(ClassMethod $classMethod): bool
public function hasParentMethodOrInterfaceMethod(ClassMethod $classMethod, ?string $methodName = null): bool
{
$methodName = $methodName ?? $this->nameResolver->getName($classMethod->name);

$class = $classMethod->getAttribute(AttributeKey::CLASS_NAME);
if (! is_string($class)) {
return false;
}

$method = $classMethod->getAttribute(AttributeKey::METHOD_NAME);
if (! is_string($method)) {
if (! class_exists($class)) {
return false;
}

if (! class_exists($class)) {
if (! is_string($methodName)) {
return false;
}

if ($this->isMethodInParent($class, $method)) {
if ($this->isMethodInParent($class, $methodName)) {
return true;
}

$implementedInterfaces = class_implements($class);
foreach ($implementedInterfaces as $implementedInterface) {
if (method_exists($implementedInterface, $method)) {
if (method_exists($implementedInterface, $methodName)) {
return true;
}
}
Expand Down Expand Up @@ -186,9 +187,7 @@ public function addMethodParameterIfMissing(Node $node, string $type, array $pos

private function isMethodInParent(string $class, string $method): bool
{
$parentClass = $class;

while ($parentClass = get_parent_class($parentClass)) {
foreach (class_parents($class) as $parentClass) {
if (method_exists($parentClass, $method)) {
return true;
}
Expand Down