diff --git a/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_abstract_trait_method.php.inc b/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_override_non_empty_method_from_trait.php.inc similarity index 79% rename from rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_abstract_trait_method.php.inc rename to rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_override_non_empty_method_from_trait.php.inc index af974d94595..1869f969c08 100644 --- a/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_abstract_trait_method.php.inc +++ b/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/skip_override_non_empty_method_from_trait.php.inc @@ -4,11 +4,11 @@ namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverridden use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleFromTrait; -final class SkipAbstractTraitMethod +class SkipOverrideNonEmptyMethodFromTrait { use ExampleFromTrait; - public function foo() + public function bar() { } } diff --git a/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/apply_attribute_to_override_method_from_trait.php.inc b/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/with_abstract_trait_method.php.inc similarity index 79% rename from rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/apply_attribute_to_override_method_from_trait.php.inc rename to rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/with_abstract_trait_method.php.inc index 0444a87ae8d..4f866e64e04 100644 --- a/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/apply_attribute_to_override_method_from_trait.php.inc +++ b/rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Fixture/with_abstract_trait_method.php.inc @@ -4,11 +4,11 @@ namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverridden use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleFromTrait; -class ApplyAttributeToOverrideMethodFromTrait +final class WithAbstractTraitMethod { use ExampleFromTrait; - public function bar() + public function foo() { } } @@ -21,12 +21,12 @@ namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverridden use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleFromTrait; -class ApplyAttributeToOverrideMethodFromTrait +final class WithAbstractTraitMethod { use ExampleFromTrait; #[\Override] - public function bar() + public function foo() { } } diff --git a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php index 3028e7a8b3d..577d5989206 100644 --- a/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php +++ b/rules/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector.php @@ -155,6 +155,7 @@ private function processAddOverrideAttribute(ClassMethod $classMethod, array $pa $classMethodName = $this->getName($classMethod->name); // Private methods should be ignored + $shouldAddOverride = false; foreach ($parentClassReflections as $parentClassReflection) { if (! $parentClassReflection->hasNativeMethod($classMethod->name->toString())) { continue; @@ -167,16 +168,23 @@ private function processAddOverrideAttribute(ClassMethod $classMethod, array $pa $parentMethod = $parentClassReflection->getNativeMethod($classMethodName); if ($parentMethod->isPrivate()) { - continue; + // early stop as already private + $shouldAddOverride = false; + return; } if ($this->shouldSkipParentClassMethod($parentClassReflection, $classMethod)) { - continue; + // early stop as already skipped + $shouldAddOverride = false; + return; } + $shouldAddOverride = true; + } + + if ($shouldAddOverride) { $classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified(self::OVERRIDE_CLASS))]); $this->hasChanged = true; - return; } } @@ -207,10 +215,14 @@ private function shouldSkipParentClassMethod(ClassReflection $parentClassReflect return true; } - if ($parentClassMethod->isAbstract()) { + if ($parentClassReflection->isTrait() && ! $parentClassMethod->isAbstract()) { return true; } + if ($parentClassMethod->isAbstract()) { + return ! $parentClassReflection->isTrait(); + } + // has any stmts? if ($parentClassMethod->stmts === null || $parentClassMethod->stmts === []) { return true;