Skip to content

Commit 6cad71e

Browse files
authored
[AnnotationsToAttributes] Apply to class level on @uses on AnnotationWithValueToAttributeRector (#492)
* [AnnotationsToAttributes] Apply to class level on @uses on AnnotationWithValueToAttributeRector * [AnnotationsToAttributes] Apply to class level on @uses on AnnotationWithValueToAttributeRector * [AnnotationsToAttributes] Apply to class level on @uses on AnnotationWithValueToAttributeRector * Fix phpstan * skip in anonymous fixture
1 parent fbf0a2f commit 6cad71e

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

config/sets/annotations-to-attributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
new AnnotationWithValueToAttribute('depends', 'PHPUnit\Framework\Attributes\Depends'),
5353
new AnnotationWithValueToAttribute('group', 'PHPUnit\Framework\Attributes\Group'),
54-
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass'),
54+
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass', [], true),
5555
new AnnotationWithValueToAttribute('testDox', 'PHPUnit\Framework\Attributes\TestDox'),
5656
new AnnotationWithValueToAttribute('testdox', 'PHPUnit\Framework\Attributes\TestDox'),
5757
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipUsedInAnonymousClassTest extends TestCase
8+
{
9+
public function someTest()
10+
{
11+
new class {
12+
/**
13+
* @uses Foo::Bar
14+
*/
15+
public function run()
16+
{
17+
}
18+
};
19+
}
20+
}

rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/uses_in_test.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\AnnotationW
2222

2323
use PHPUnit\Framework\TestCase;
2424

25+
#[\PHPUnit\Framework\Attributes\UsesClass(Foo::Bar)]
2526
final class UsesInTest extends TestCase
2627
{
27-
#[\PHPUnit\Framework\Attributes\UsesClass(Foo::Bar)]
2828
public function run()
2929
{
3030
}

rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
'disabled' => false,
1414
]),
1515
new AnnotationWithValueToAttribute('dataProvider', 'PHPUnit\Framework\Attributes\DataProvider'),
16-
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass'),
16+
new AnnotationWithValueToAttribute('uses', 'PHPUnit\Framework\Attributes\UsesClass', [], true),
1717
]);
1818
};

rules/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ final class AnnotationWithValueToAttributeRector extends AbstractRector implemen
3434
*/
3535
private array $annotationWithValueToAttributes = [];
3636

37+
private ?Class_ $currentClass = null;
38+
3739
public function __construct(
3840
private readonly PhpDocTagRemover $phpDocTagRemover,
3941
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
@@ -101,6 +103,10 @@ public function refactor(Node $node): ?Node
101103
return null;
102104
}
103105

106+
if ($node instanceof Class_) {
107+
$this->currentClass = $node;
108+
}
109+
104110
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
105111
if (! $phpDocInfo instanceof PhpDocInfo) {
106112
return null;
@@ -127,7 +133,12 @@ public function refactor(Node $node): ?Node
127133
[$attributeValue]
128134
);
129135

130-
$node->attrGroups[] = $attributeGroup;
136+
if ($node instanceof ClassMethod && $annotationWithValueToAttribute->getIsOnClassLevel() && $this->currentClass instanceof Class_) {
137+
Assert::isInstanceOf($this->currentClass, Class_::class);
138+
$this->currentClass->attrGroups = array_merge($this->currentClass->attrGroups, [$attributeGroup]);
139+
} else {
140+
$node->attrGroups = array_merge($node->attrGroups, [$attributeGroup]);
141+
}
131142

132143
// cleanup
133144
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);

src/ValueObject/AnnotationWithValueToAttribute.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
public function __construct(
1313
private string $annotationName,
1414
private string $attributeClass,
15-
private array $valueMap = []
15+
private array $valueMap = [],
16+
private bool $isOnClassLevel = false,
1617
) {
1718
}
1819

@@ -33,4 +34,9 @@ public function getValueMap(): array
3334
{
3435
return $this->valueMap;
3536
}
37+
38+
public function getIsOnClassLevel(): bool
39+
{
40+
return $this->isOnClassLevel;
41+
}
3642
}

0 commit comments

Comments
 (0)