From ab49273fb881540f42307fdc9a7c9569073c9ebe Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 29 May 2025 08:56:37 +0700 Subject: [PATCH 1/2] [Renaming] Allow rename method in trait itself on RenameMethodRector --- .../Fixture/rename_trait_method.php.inc | 25 +++++++++++++++++++ .../config/configured_rule.php | 2 ++ .../Rector/MethodCall/RenameMethodRector.php | 21 ++++++++-------- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/Fixture/rename_trait_method.php.inc diff --git a/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/Fixture/rename_trait_method.php.inc b/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/Fixture/rename_trait_method.php.inc new file mode 100644 index 00000000000..8e6d17046ec --- /dev/null +++ b/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/Fixture/rename_trait_method.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config/configured_rule.php b/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config/configured_rule.php index 35463168a2a..ce6738cb748 100644 --- a/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config/configured_rule.php +++ b/rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config/configured_rule.php @@ -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; @@ -37,5 +38,6 @@ new MethodCallRename(SomeEnumWithMethod::class, 'oldEnumMethod', 'newEnumMethod'), new MethodCallRename(SomeTrait::class, '_test', 'test'), + new MethodCallRename(RenameTraitMethod::class, 'run', 'execute'), ]); }; diff --git a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php index 5f29d5219d9..59d5ce1ac21 100644 --- a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php +++ b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php @@ -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; @@ -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); } @@ -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()); @@ -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; @@ -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 === null && $classOrInterface instanceof Trait_) { + return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename); + } + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( $classMethod, $methodCallRename->getObjectType() From 071bd39b607a3750901724894494cb44616ef6d2 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 29 May 2025 01:58:38 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- rules/Renaming/Rector/MethodCall/RenameMethodRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php index 59d5ce1ac21..621554b89f8 100644 --- a/rules/Renaming/Rector/MethodCall/RenameMethodRector.php +++ b/rules/Renaming/Rector/MethodCall/RenameMethodRector.php @@ -198,7 +198,7 @@ private function shouldSkipRename( return true; } - if ($classReflection === null && $classOrInterface instanceof Trait_) { + if (!$classReflection instanceof ClassReflection && $classOrInterface instanceof Trait_) { return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename); }