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
@@ -0,0 +1,25 @@
<?php

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

trait RenameTraitMethod
{
private function run()
{
}
}

?>
-----
<?php

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

trait RenameTraitMethod
{
private function execute()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture\RenameTraitMethod;
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\DifferentInterface;
Expand Down Expand Up @@ -37,5 +38,6 @@

new MethodCallRename(SomeEnumWithMethod::class, 'oldEnumMethod', 'newEnumMethod'),
new MethodCallRename(SomeTrait::class, '_test', 'test'),
new MethodCallRename(RenameTraitMethod::class, 'run', 'execute'),
]);
};
21 changes: 11 additions & 10 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -70,16 +71,16 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class];
return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Trait_::class, Interface_::class];
}

/**
* @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node
* @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_|Trait_ $node
*/
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
if ($node instanceof Class_ || $node instanceof Interface_) {
if ($node instanceof Class_ || $node instanceof Trait_ || $node instanceof Interface_) {
return $this->refactorClass($node, $scope);
}

Expand Down Expand Up @@ -124,7 +125,7 @@ private function shouldSkipClassMethod(
}

private function hasClassNewClassMethod(
Class_|Interface_ $classOrInterface,
Class_|Trait_|Interface_ $classOrInterface,
MethodCallRenameInterface $methodCallRename
): bool {
return (bool) $classOrInterface->getMethod($methodCallRename->getNewMethod());
Expand All @@ -149,12 +150,8 @@ private function shouldKeepForParentInterface(
);
}

private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope): Class_|Interface_|null
private function refactorClass(Class_|Trait_|Interface_ $classOrInterface, Scope $scope): Class_|Trait_|Interface_|null
{
if (! $scope->isInClass()) {
return null;
}

$classReflection = $scope->getClassReflection();

$hasChanged = false;
Expand Down Expand Up @@ -194,13 +191,17 @@ private function shouldSkipRename(
string $methodName,
ClassMethod $classMethod,
MethodCallRenameInterface $methodCallRename,
Class_|Interface_ $classOrInterface,
Class_|Trait_|Interface_ $classOrInterface,
?ClassReflection $classReflection
): bool {
if (! $this->nodeNameResolver->isStringName($methodName, $methodCallRename->getOldMethod())) {
return true;
}

if (!$classReflection instanceof ClassReflection && $classOrInterface instanceof Trait_) {
return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename);
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$classMethod,
$methodCallRename->getObjectType()
Expand Down