Skip to content

Commit

Permalink
[Php83] Skip add #[\Override] on non-empty method from trait on AddOv…
Browse files Browse the repository at this point in the history
…errideAttributeToOverriddenMethodsRector (#6471)

* [Php83] Skip add #[\Override] on non-empty method from trait on AddOverrideAttributeToOverriddenMethodsRector

* fix

* fix in loop multi methods on multi parents check
  • Loading branch information
samsonasik authored Nov 22, 2024
1 parent 1c0260f commit 9d022cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
}
Expand All @@ -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()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9d022cf

Please sign in to comment.