diff --git a/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector/Fixture/both_annotation_and_attribute_exists.php.inc b/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector/Fixture/both_annotation_and_attribute_exists.php.inc new file mode 100644 index 00000000..106c6ba5 --- /dev/null +++ b/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector/Fixture/both_annotation_and_attribute_exists.php.inc @@ -0,0 +1,48 @@ + ['foo']; + yield 'case 2' => ['bar']; + } +} + +?> +----- + ['foo']; + yield 'case 2' => ['bar']; + } +} + +?> \ No newline at end of file diff --git a/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php index e9bbc351..55470c4f 100644 --- a/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php @@ -148,7 +148,11 @@ public function refactor(Node $node): ?Node continue; } - $node->attrGroups[] = $this->createAttributeGroup($originalAttributeValueToken); + $attributeGroup = $this->createAttributeGroup($node, $originalAttributeValueToken); + + if ($attributeGroup instanceof AttributeGroup) { + $node->attrGroups[] = $attributeGroup; + } // cleanup $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); @@ -159,7 +163,7 @@ public function refactor(Node $node): ?Node return $node; } - private function createAttributeGroup(string $originalAttributeValue): AttributeGroup + private function createAttributeGroup(ClassMethod $classMethod, string $originalAttributeValue): ?AttributeGroup { $methodName = trim($originalAttributeValue, '()'); @@ -173,12 +177,23 @@ private function createAttributeGroup(string $originalAttributeValue): Attribute $className = '\\' . $className; } - return $this->phpAttributeGroupFactory->createFromClassWithItems( + $attributeGroup = $this->phpAttributeGroupFactory->createFromClassWithItems( 'PHPUnit\Framework\Attributes\DataProviderExternal', [$className . '::class', $methodName] ); + } else { + $attributeGroup = $this->phpAttributeGroupFactory->createFromClassWithItems( + self::DATA_PROVIDER_CLASS, + [$methodName] + ); + } + + foreach ($classMethod->attrGroups as $existingAttributeGroup) { + if ($this->nodeComparator->areNodesEqual($existingAttributeGroup, $attributeGroup)) { + return null; + } } - return $this->phpAttributeGroupFactory->createFromClassWithItems(self::DATA_PROVIDER_CLASS, [$methodName]); + return $attributeGroup; } }