Browse Source

SingleTraitInsertPerStatement - fix formatting for multiline \"use\"

Kuba Werłos 5 years ago
parent
commit
a982efcfdd

+ 21 - 9
src/Fixer/ClassNotation/SingleTraitInsertPerStatementFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\ClassNotation;
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Preg;
 use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -58,24 +59,35 @@ final class Example
             if ($tokens[$index]->isGivenKind(CT::T_USE_TRAIT)) {
                 $candidates = $this->getCandidates($tokens, $index);
                 if (\count($candidates) > 0) {
-                    $this->fixTraitUse($tokens, array_reverse($candidates));
+                    $this->fixTraitUse($tokens, $index, $candidates);
                 }
             }
         }
     }
 
     /**
-     * @param int[] $candidates ',' indexes to fix
+     * @param int   $useTraitIndex
+     * @param int[] $candidates    ',' indexes to fix
      */
-    private function fixTraitUse(Tokens $tokens, array $candidates)
+    private function fixTraitUse(Tokens $tokens, $useTraitIndex, array $candidates)
     {
-        foreach ($candidates as $nextInsertIndex) {
-            $tokens[$nextInsertIndex] = new Token(';');
-            $tokens->insertAt($nextInsertIndex + 1, new Token([CT::T_USE_TRAIT, 'use']));
+        foreach ($candidates as $commaIndex) {
+            $inserts = [
+                new Token([CT::T_USE_TRAIT, 'use']),
+                new Token([T_WHITESPACE, ' ']),
+            ];
 
-            if (!$tokens[$nextInsertIndex + 2]->isWhitespace()) {
-                $tokens->insertAt($nextInsertIndex + 2, new Token([T_WHITESPACE, ' ']));
+            $nextImportStartIndex = $tokens->getNextMeaningfulToken($commaIndex);
+
+            if ($tokens[$nextImportStartIndex - 1]->isWhitespace()) {
+                if (1 === Preg::match('/\R/', $tokens[$nextImportStartIndex - 1]->getContent())) {
+                    array_unshift($inserts, clone $tokens[$useTraitIndex - 1]);
+                }
+                $tokens->clearAt($nextImportStartIndex - 1);
             }
+
+            $tokens[$commaIndex] = new Token(';');
+            $tokens->insertAt($nextImportStartIndex, $inserts);
         }
     }
 
@@ -98,6 +110,6 @@ final class Example
             $index = $tokens->getNextTokenOfKind($index, [',', ';', '{']);
         }
 
-        return $indexes;
+        return array_reverse($indexes);
     }
 }

+ 58 - 2
tests/Fixer/ClassNotation/SingleTraitInsertPerStatementFixerTest.php

@@ -81,6 +81,24 @@ final class Example
 {
     use Foo, Bar ;
 }
+',
+            ],
+            'simple III' => [
+                '<?php
+class Example
+{
+    use Foo;use Bar;
+
+    public function baz() {}
+}
+',
+                '<?php
+class Example
+{
+    use Foo, Bar;
+
+    public function baz() {}
+}
 ',
             ],
             'multiple' => [
@@ -101,6 +119,44 @@ final class Example
     use Foo10, Bar11, Bar110;
     use Foo20, Bar20, Bar200, Bar201;
 }
+',
+            ],
+            'multiple_multiline' => [
+                '<?php
+final class Example
+{
+    use Foo;
+    use Bar;
+    use Baz;
+}
+',
+                '<?php
+final class Example
+{
+    use Foo,
+        Bar,
+        Baz;
+}
+',
+            ],
+            'multiple_multiline_with_comment' => [
+                '<?php
+final class Example
+{
+    use Foo;
+    use Bar;
+//        Bazz,
+    use Baz;
+}
+',
+                '<?php
+final class Example
+{
+    use Foo,
+        Bar,
+//        Bazz,
+        Baz;
+}
 ',
             ],
             'namespaces' => [
@@ -126,9 +182,9 @@ class ZZ
 use#2
 Z/* 2 */ #3
 #4
-;use #5
+;#5
 #6
-T#7
+use T#7
 #8
 ;#9
 #10