Browse Source

bug: `PhpdocToCommentFixer` support for enum cases (#7040)

Greg Korba 1 year ago
parent
commit
7b7f77f97d

+ 6 - 0
src/Tokenizer/Analyzer/CommentsAnalyzer.php

@@ -194,6 +194,12 @@ final class CommentsAnalyzer
             return true;
         }
 
+        if ($token->isGivenKind(T_CASE) && \defined('T_ENUM')) {
+            $caseParent = $tokens->getPrevTokenOfKind($index, [[T_ENUM], [T_SWITCH]]);
+
+            return $tokens[$caseParent]->isGivenKind([T_ENUM]);
+        }
+
         if ($token->isGivenKind(T_STATIC)) {
             return !$tokens[$tokens->getNextMeaningfulToken($index)]->isGivenKind(T_DOUBLE_COLON);
         }

+ 12 - 0
tests/Fixer/Phpdoc/PhpdocToCommentFixerTest.php

@@ -898,5 +898,17 @@ enum Foo {
     //
 }',
         ];
+
+        yield 'phpDoc over enum case' => [
+            '<?php
+enum Foo: int
+{
+    /**
+     * @deprecated do not convert this
+     */
+    case BAR = 1;
+}
+',
+        ];
     }
 }

+ 14 - 4
tests/Tokenizer/Analyzer/CommentsAnalyzerTest.php

@@ -310,15 +310,15 @@ $bar;',
         self::assertTrue($analyzer->isBeforeStructuralElement($tokens, $index));
     }
 
-    public static function providePhpdocCandidatePhp80Cases(): array
+    public static function providePhpdocCandidatePhp80Cases(): iterable
     {
-        return [
-            ['<?php
+        yield 'attribute between class and phpDoc' => [
+            '<?php
 /**
  * @Annotation
  */
 #[CustomAnnotationA]
-Class MyAnnotation3 {}'],
+Class MyAnnotation3 {}',
         ];
     }
 
@@ -369,6 +369,16 @@ Class MyAnnotation3 {}'],
         yield 'enum' => [
             '<?php /* Before enum */ enum Foo {}',
         ];
+
+        yield 'enum with deprecated case' => [
+            '<?php
+enum Foo: int {
+    /**
+     * @deprecated Lorem ipsum
+     */
+    case BAR = 1;
+}',
+        ];
     }
 
     /**