Browse Source

SingleLineCommentStyleFixer - do not fix possible attributes (PHP8)

SpacePossum 4 years ago
parent
commit
89d95e8e78

+ 4 - 0
src/Fixer/Comment/SingleLineCommentStyleFixer.php

@@ -120,6 +120,10 @@ $c = 3;
             $commentContent = substr($content, 2, -2) ?: '';
 
             if ($this->hashEnabled && '#' === $content[0]) {
+                if (isset($content[1]) && '[' === $content[1]) {
+                    continue; // This might be attribute on PHP8, do not change
+                }
+
                 $tokens[$index] = new Token([$token->getId(), '//'.substr($content, 1)]);
 
                 continue;

+ 9 - 1
tests/Fixer/Comment/SingleLineCommentStyleFixerTest.php

@@ -258,7 +258,10 @@ second line*/',
                     # test 4
                 ',
             ],
-
+            [
+                '<?php //',
+                '<?php #',
+            ],
             // Untouched cases
             [
                 '<?php
@@ -279,6 +282,11 @@ second line*/',
             [
                 '<?php /* start-end */',
             ],
+            [
+                '<?php function foo(
+    #[MyAttr([1, 2])] Type $myParam,
+) {} // foo',
+            ],
         ];
     }