Browse Source

fix: Do not mangle non-whitespace token in `PhpdocIndentFixer` (#8147)

Jan Verbeek 7 months ago
parent
commit
efafb74c5f

+ 10 - 4
src/Fixer/Phpdoc/PhpdocIndentFixer.php

@@ -63,7 +63,9 @@ class DocBlocks
 
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
-        foreach ($tokens as $index => $token) {
+        for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
+            $token = $tokens[$index];
+
             if (!$token->isGivenKind(T_DOC_COMMENT)) {
                 continue;
             }
@@ -95,7 +97,13 @@ class DocBlocks
 
             $newPrevContent = $this->fixWhitespaceBeforeDocblock($prevToken->getContent(), $indent);
 
-            if ('' !== $newPrevContent) {
+            $tokens[$index] = new Token([T_DOC_COMMENT, $this->fixDocBlock($token->getContent(), $indent)]);
+
+            if (!$prevToken->isWhitespace()) {
+                if ('' !== $indent) {
+                    $tokens->insertAt($index, new Token([T_WHITESPACE, $indent]));
+                }
+            } elseif ('' !== $newPrevContent) {
                 if ($prevToken->isArray()) {
                     $tokens[$prevIndex] = new Token([$prevToken->getId(), $newPrevContent]);
                 } else {
@@ -104,8 +112,6 @@ class DocBlocks
             } else {
                 $tokens->clearAt($prevIndex);
             }
-
-            $tokens[$index] = new Token([T_DOC_COMMENT, $this->fixDocBlock($token->getContent(), $indent)]);
         }
     }
 

+ 11 - 0
tests/Fixer/Phpdoc/PhpdocIndentFixerTest.php

@@ -410,6 +410,17 @@ class Application
 class Dispatcher
 {
 }
+',
+        ];
+
+        yield [
+            '<?php
+$foo->bar()    /** comment */
+    ->baz();
+',
+            '<?php
+$foo->bar()/** comment */
+    ->baz();
 ',
         ];
     }

+ 13 - 0
tests/Fixtures/Integration/misc/phpdoc_indent,no_spaces_after_function_name.test

@@ -0,0 +1,13 @@
+--TEST--
+Integration of fixers: phpdoc_indent,no_spaces_after_function_name
+--RULESET--
+{"phpdoc_indent": true, "no_spaces_after_function_name": true}
+--EXPECT--
+<?php
+$foo->bar()    /** comment */
+    ->baz();
+
+--INPUT--
+<?php
+$foo->bar()/** comment */
+    ->baz();