Просмотр исходного кода

fix: `PhpUnitAttributesFixer` - handle multiple annotations of the same name (#8075)

Kuba Werłos 9 месяцев назад
Родитель
Сommit
fa3b1ed1aa

+ 1 - 1
phpstan.dist.neon

@@ -57,6 +57,6 @@ parameters:
         -
         -
             identifier: offsetAccess.notFound
             identifier: offsetAccess.notFound
             path: .
             path: .
-            count: 559
+            count: 560
     tipsOfTheDay: false
     tipsOfTheDay: false
     tmpDir: dev-tools/phpstan/cache
     tmpDir: dev-tools/phpstan/cache

+ 6 - 1
src/Fixer/PhpUnit/PhpUnitAttributesFixer.php

@@ -109,6 +109,7 @@ final class PhpUnitAttributesFixer extends AbstractPhpUnitFixer
 
 
             $docBlock = new DocBlock($tokens[$index]->getContent());
             $docBlock = new DocBlock($tokens[$index]->getContent());
 
 
+            $presentAttributes = [];
             foreach (array_reverse($docBlock->getAnnotations()) as $annotation) {
             foreach (array_reverse($docBlock->getAnnotations()) as $annotation) {
                 $annotationName = $annotation->getTag()->getName();
                 $annotationName = $annotation->getTag()->getName();
 
 
@@ -122,7 +123,11 @@ final class PhpUnitAttributesFixer extends AbstractPhpUnitFixer
                 /** @phpstan-ignore-next-line */
                 /** @phpstan-ignore-next-line */
                 $tokensToInsert = self::{$this->fixingMap[$annotationName]}($tokens, $index, $annotation);
                 $tokensToInsert = self::{$this->fixingMap[$annotationName]}($tokens, $index, $annotation);
 
 
-                if (self::isAttributeAlreadyPresent($tokens, $index, $tokensToInsert)) {
+                if (!isset($presentAttributes[$annotationName])) {
+                    $presentAttributes[$annotationName] = self::isAttributeAlreadyPresent($tokens, $index, $tokensToInsert);
+                }
+
+                if ($presentAttributes[$annotationName]) {
                     continue;
                     continue;
                 }
                 }
 
 

+ 19 - 0
tests/Fixer/PhpUnit/PhpUnitAttributesFixerTest.php

@@ -598,6 +598,25 @@ final class PhpUnitAttributesFixerTest extends AbstractFixerTestCase
                 }
                 }
                 PHP,
                 PHP,
         ];
         ];
+
+        yield 'handle multiple annotations of the same name' => [
+            <<<'PHP'
+                <?php
+                /**
+                 */
+                #[\PHPUnit\Framework\Attributes\Group('foo')]
+                #[\PHPUnit\Framework\Attributes\Group('bar')]
+                class TheTest extends \PHPUnit\Framework\TestCase {}
+                PHP,
+            <<<'PHP'
+                <?php
+                /**
+                 * @group foo
+                 * @group bar
+                 */
+                class TheTest extends \PHPUnit\Framework\TestCase {}
+                PHP,
+        ];
     }
     }
 
 
     /**
     /**