Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion config/sets/annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\AnnotationW

use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\UsesClass(Foo::Bar)]
final class UsesInTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\UsesClass(Foo::Bar)]
public function run()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'disabled' => false,
]),
new AnnotationWithValueToAttribute('dataProvider', 'PHPUnit\Framework\Attributes\DataProvider'),
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass'),
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass', [], true),
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -101,6 +103,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node instanceof Class_) {
Comment thread
samsonasik marked this conversation as resolved.
$this->currentClass = $node;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/ValueObject/AnnotationWithValueToAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public function __construct(
private string $annotationName,
private string $attributeClass,
private array $valueMap = []
private array $valueMap = [],
private bool $isOnClassLevel = false,
) {
}

Expand All @@ -33,4 +34,9 @@ public function getValueMap(): array
{
return $this->valueMap;
}

public function getIsOnClassLevel(): bool
{
return $this->isOnClassLevel;
}
}
Loading