diff --git a/config/sets/annotations-to-attributes.php b/config/sets/annotations-to-attributes.php index df44415e..a5b81221 100644 --- a/config/sets/annotations-to-attributes.php +++ b/config/sets/annotations-to-attributes.php @@ -51,7 +51,7 @@ new AnnotationWithValueToAttribute('depends', 'PHPUnit\Framework\Attributes\Depends'), new AnnotationWithValueToAttribute('group', 'PHPUnit\Framework\Attributes\Group'), - new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass'), + new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass', [], true), new AnnotationWithValueToAttribute('testDox', 'PHPUnit\Framework\Attributes\TestDox'), new AnnotationWithValueToAttribute('testdox', 'PHPUnit\Framework\Attributes\TestDox'), ]); diff --git a/rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/skip_uses_in_anonymous_class.php.inc b/rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/skip_uses_in_anonymous_class.php.inc new file mode 100644 index 00000000..913f1cf9 --- /dev/null +++ b/rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/skip_uses_in_anonymous_class.php.inc @@ -0,0 +1,20 @@ + false, ]), new AnnotationWithValueToAttribute('dataProvider', 'PHPUnit\Framework\Attributes\DataProvider'), - new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass'), + new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass', [], true), ]); }; diff --git a/rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php index c3d070dc..422dd761 100644 --- a/rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php @@ -34,6 +34,8 @@ final class AnnotationWithValueToAttributeRector extends AbstractRector implemen */ private array $annotationWithValueToAttributes = []; + private ?Class_ $currentClass = null; + public function __construct( private readonly PhpDocTagRemover $phpDocTagRemover, private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory, @@ -101,6 +103,10 @@ public function refactor(Node $node): ?Node return null; } + if ($node instanceof Class_) { + $this->currentClass = $node; + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); if (! $phpDocInfo instanceof PhpDocInfo) { return null; @@ -127,7 +133,12 @@ public function refactor(Node $node): ?Node [$attributeValue] ); - $node->attrGroups[] = $attributeGroup; + if ($node instanceof ClassMethod && $annotationWithValueToAttribute->getIsOnClassLevel() && $this->currentClass instanceof Class_) { + Assert::isInstanceOf($this->currentClass, Class_::class); + $this->currentClass->attrGroups = array_merge($this->currentClass->attrGroups, [$attributeGroup]); + } else { + $node->attrGroups = array_merge($node->attrGroups, [$attributeGroup]); + } // cleanup $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); diff --git a/src/ValueObject/AnnotationWithValueToAttribute.php b/src/ValueObject/AnnotationWithValueToAttribute.php index 577434f1..0027de6e 100644 --- a/src/ValueObject/AnnotationWithValueToAttribute.php +++ b/src/ValueObject/AnnotationWithValueToAttribute.php @@ -12,7 +12,8 @@ public function __construct( private string $annotationName, private string $attributeClass, - private array $valueMap = [] + private array $valueMap = [], + private bool $isOnClassLevel = false, ) { } @@ -33,4 +34,9 @@ public function getValueMap(): array { return $this->valueMap; } + + public function getIsOnClassLevel(): bool + { + return $this->isOnClassLevel; + } }