Skip to content

Commit

Permalink
[Renaming] Rename class method implements interface with method exists (
Browse files Browse the repository at this point in the history
#1155)

Co-authored-by: Wojciech Kania <wojciech.kania@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Nov 5, 2021
1 parent 2906122 commit 9d626ea
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UnionType;
use Rector\NameImporting\NodeAnalyzer\UseAnalyzer;
use Rector\NameImporting\ValueObject\NameAndParent;
use Rector\Testing\PHPUnit\AbstractTestCase;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function provideData(): Iterator
'String_',
0,
new FullyQualified(String_::class),
Node\UnionType::class,
UnionType::class,
];

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

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

final class CustomType
{
public function notify(): int
{
return 5;
}
}

final class Caller
{
public static function execute(): void
{
$demo = new CustomType();
$demo->notify();
}
}
?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

final class CustomType
{
public function __invoke(): int
{
return 5;
}
}

final class Caller
{
public static function execute(): void
{
$demo = new CustomType();
$demo->__invoke();
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo;

class RenameMethodCall
{
private function call()
{
$foo = new Foo();
$foo->old();
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo;

class RenameMethodCall
{
private function call()
{
$foo = new Foo();
$foo->new();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

class SkipRenameMethodCall
{
public static function execute(): void
{
$demo = new SomeSubscriber();
$demo->old();
}
}

class SomeSubscriber implements SubscriberInterface
{
public function old(): int
{
return 5;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

final class CustomType
{
public function notify(): int
{
return 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

final class Foo implements FooInterface
{
public function old(): void
{
}

public function new(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

interface FooInterface
{
public function old(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

class SomeSubscriber implements SubscriberInterface
{
public function old(): int
{
return 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source;

interface SubscriberInterface
{
public function old(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\AbstractType;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\CustomType;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\Foo;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\SomeSubscriber;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -16,16 +19,9 @@
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
new MethodCallRename(AbstractType::class, 'setDefaultOptions', 'configureOptions'),
new MethodCallRename('Nette\Utils\Html', 'add', 'addHtml'),
new MethodCallRename(
'Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture\DemoFile',
'notify',
'__invoke'
),
new MethodCallRename(
'Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture\SomeSubscriber',
'old',
'new'
),
new MethodCallRename(CustomType::class, 'notify', '__invoke'),
new MethodCallRename(SomeSubscriber::class, 'old', 'new'),
new MethodCallRename(Foo::class, 'old', 'new'),
// with array key
new MethodCallRenameWithArrayKey('Nette\Utils\Html', 'addToArray', 'addToHtmlArray', 'hey'),
]),
Expand Down
6 changes: 3 additions & 3 deletions rules/CodingStyle/Naming/NameRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function __construct(
*/
public function renameNameNode(array $namesAndParent, string $lastName): void
{
foreach ($namesAndParent as $usedNameNode) {
$parentNode = $usedNameNode->getParentNode();
$usedName = $usedNameNode->getNameNode();
foreach ($namesAndParent as $nameAndParent) {
$parentNode = $nameAndParent->getParentNode();
$usedName = $nameAndParent->getNameNode();

if ($parentNode instanceof TraitUse) {
$this->renameTraitUse($lastName, $parentNode, $usedName);
Expand Down
3 changes: 2 additions & 1 deletion rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function refactor(Node $node): ?Node
foreach ($this->methodCallRenames as $methodCallRename) {
$implementsInterface = $this->classManipulator->hasParentMethodOrInterface(
$methodCallRename->getObjectType(),
$methodCallRename->getOldMethod()
$methodCallRename->getOldMethod(),
$methodCallRename->getNewMethod()
);
if ($implementsInterface) {
continue;
Expand Down
16 changes: 12 additions & 4 deletions src/NodeManipulator/ClassManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PostRector\Collector\NodesToRemoveCollector;

Expand All @@ -18,11 +19,12 @@ final class ClassManipulator
public function __construct(
private NodeNameResolver $nodeNameResolver,
private NodesToRemoveCollector $nodesToRemoveCollector,
private ReflectionProvider $reflectionProvider
private ReflectionProvider $reflectionProvider,
private ClassChildAnalyzer $classChildAnalyzer
) {
}

public function hasParentMethodOrInterface(ObjectType $objectType, string $methodName): bool
public function hasParentMethodOrInterface(ObjectType $objectType, string $oldMethod, string $newMethod): bool
{
if (! $this->reflectionProvider->hasClass($objectType->getClassName())) {
return false;
Expand All @@ -34,9 +36,15 @@ public function hasParentMethodOrInterface(ObjectType $objectType, string $metho
continue;
}

if ($ancestorClassReflection->hasMethod($methodName)) {
return true;
if (! $ancestorClassReflection->hasMethod($oldMethod)) {
continue;
}

if ($this->classChildAnalyzer->hasChildClassMethod($ancestorClassReflection, $newMethod)) {
continue;
}

return true;
}

return false;
Expand Down

0 comments on commit 9d626ea

Please sign in to comment.