Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply AddOverrideAttributeToOverriddenMethodsRector to traits #6367

Merged
merged 1 commit into from
Oct 9, 2024
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,42 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleFromTrait;

class ApplyAttributeToOverrideMethodFromTrait
{
use ExampleFromTrait;

public function foo()
{
}

public function bar()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\ExampleFromTrait;

class ApplyAttributeToOverrideMethodFromTrait
{
use ExampleFromTrait;

#[\Override]
public function foo()
{
}

public function bar()
{
}
}

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

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;

trait ExampleFromTrait
{
public abstract function foo();

public function bar() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public function refactor(Node $node): ?Node
}

$classReflection = $this->reflectionProvider->getClass($className);
$parentClassReflections = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
$parentClassReflections = [
...$classReflection->getParents(),
...$classReflection->getInterfaces(),
...$classReflection->getTraits(),
];

$this->processAddOverrideAttribute($node, $parentClassReflections);

Expand Down Expand Up @@ -152,6 +156,9 @@ private function processAddOverrideAttribute(Class_ $class, array $parentClassRe
if ($parentMethod->isPrivate()) {
continue;
}
if ($parentClassReflection->isTrait() && !$parentMethod->isAbstract()) {
continue;
}

$classMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified('Override'))]);
$this->hasChanged = true;
Expand Down