[Renaming] Skip RenameMethodRector when class implements interface method (#5557)

Co-authored-by: Ruud Kamphuis <ruudk@users.noreply.github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-02-15 23:21:29 +07:00 committed by GitHub
parent 19c876f01e
commit cbebb9a4de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\ClassManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Renaming\Contract\MethodCallRenameInterface;
@ -37,6 +38,16 @@ final class RenameMethodRector extends AbstractRector implements ConfigurableRec
*/
private $methodCallRenames = [];
/**
* @var ClassManipulator
*/
private $classManipulator;
public function __construct(ClassManipulator $classManipulator)
{
$this->classManipulator = $classManipulator;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns method names to new ones.', [
@ -74,6 +85,14 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallRenames as $methodCallRename) {
$implementsInterface = $this->classManipulator->hasParentMethodOrInterface(
$methodCallRename->getOldClass(),
$methodCallRename->getOldMethod()
);
if ($implementsInterface) {
continue;
}
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
$methodCallRename->getOldClass()

View File

@ -0,0 +1,26 @@
<?php
namespace Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture;
interface SubscriberInterface
{
public function old();
}
final class SomeSubscriber implements SubscriberInterface
{
public function old()
{
return 5;
}
}
final class SomeCaller
{
public static function execute()
{
$demo = new SomeSubscriber();
$demo->old();
}
}
?>

View File

@ -20,6 +20,11 @@ return static function (ContainerConfigurator $containerConfigurator): void {
'notify',
'__invoke'
),
new MethodCallRename(
'Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture\SomeSubscriber',
'old',
'new'
),
new MethodCallRename('*Presenter', 'run', '__invoke'),
new MethodCallRename(
\Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture\SkipSelfMethodRename::class,