mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 08:02:23 +00:00
[Renaming] Skip RenameMethodRector when class implements interface method (#5557)
Co-authored-by: Ruud Kamphuis <ruudk@users.noreply.github.com>
This commit is contained in:
parent
19c876f01e
commit
cbebb9a4de
|
@ -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()
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user