mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-24 09:19:47 +01:00
Merge pull request #1830 from rectorphp/leave-node-bug
fix non-same parent method name for RemoveParentCallWithoutParentRector
This commit is contained in:
commit
b25913e35d
@ -82,7 +82,8 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($methodNode)) {
|
||||
$calledMethodName = $this->getName($node->name);
|
||||
if ($this->classMethodManipulator->hasParentMethodOrInterfaceMethod($methodNode, $calledMethodName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ final class RemoveParentCallWithoutParentRectorTest extends AbstractRectorTestCa
|
||||
__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',
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -90,29 +90,30 @@ final class ClassMethodManipulator
|
||||
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)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! class_exists($class)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isMethodInParent($class, $method)) {
|
||||
if (! is_string($methodName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -186,9 +187,7 @@ final class ClassMethodManipulator
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user