Skip to content

Commit 6d2b1ba

Browse files
authored
[AnnotationsToAttributes] Handle both annotation and attributes exists on DataProviderAnnotationToAttributeRector (#603)
* [AnnotationsToAttributes] Hanlde both annotation and attributes exists on DataProviderAnnotationToAttributeRector * fix typo
1 parent 5d1c0bb commit 6d2b1ba

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class BothAnnotationAndAttributeExists extends TestCase
9+
{
10+
/** @dataProvider provideData */
11+
#[DataProvider('provideData')]
12+
public function testExample(string $value): void
13+
{
14+
self::assertNotEmpty($value);
15+
}
16+
17+
public static function provideData(): iterable
18+
{
19+
yield 'case 1' => ['foo'];
20+
yield 'case 2' => ['bar'];
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
declare(strict_types=1);
29+
30+
use PHPUnit\Framework\Attributes\DataProvider;
31+
use PHPUnit\Framework\TestCase;
32+
33+
final class BothAnnotationAndAttributeExists extends TestCase
34+
{
35+
#[DataProvider('provideData')]
36+
public function testExample(string $value): void
37+
{
38+
self::assertNotEmpty($value);
39+
}
40+
41+
public static function provideData(): iterable
42+
{
43+
yield 'case 1' => ['foo'];
44+
yield 'case 2' => ['bar'];
45+
}
46+
}
47+
48+
?>

rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ public function refactor(Node $node): ?Node
148148
continue;
149149
}
150150

151-
$node->attrGroups[] = $this->createAttributeGroup($originalAttributeValueToken);
151+
$attributeGroup = $this->createAttributeGroup($node, $originalAttributeValueToken);
152+
153+
if ($attributeGroup instanceof AttributeGroup) {
154+
$node->attrGroups[] = $attributeGroup;
155+
}
152156

153157
// cleanup
154158
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
@@ -159,7 +163,7 @@ public function refactor(Node $node): ?Node
159163
return $node;
160164
}
161165

162-
private function createAttributeGroup(string $originalAttributeValue): AttributeGroup
166+
private function createAttributeGroup(ClassMethod $classMethod, string $originalAttributeValue): ?AttributeGroup
163167
{
164168
$methodName = trim($originalAttributeValue, '()');
165169

@@ -173,12 +177,23 @@ private function createAttributeGroup(string $originalAttributeValue): Attribute
173177
$className = '\\' . $className;
174178
}
175179

176-
return $this->phpAttributeGroupFactory->createFromClassWithItems(
180+
$attributeGroup = $this->phpAttributeGroupFactory->createFromClassWithItems(
177181
'PHPUnit\Framework\Attributes\DataProviderExternal',
178182
[$className . '::class', $methodName]
179183
);
184+
} else {
185+
$attributeGroup = $this->phpAttributeGroupFactory->createFromClassWithItems(
186+
self::DATA_PROVIDER_CLASS,
187+
[$methodName]
188+
);
189+
}
190+
191+
foreach ($classMethod->attrGroups as $existingAttributeGroup) {
192+
if ($this->nodeComparator->areNodesEqual($existingAttributeGroup, $attributeGroup)) {
193+
return null;
194+
}
180195
}
181196

182-
return $this->phpAttributeGroupFactory->createFromClassWithItems(self::DATA_PROVIDER_CLASS, [$methodName]);
197+
return $attributeGroup;
183198
}
184199
}

0 commit comments

Comments
 (0)