Browse Source

Fix: Do not adjust whitespace before multiple multi-line extends

Andreas Möller 4 years ago
parent
commit
1c55a74ab8

+ 28 - 0
src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php

@@ -218,6 +218,10 @@ yield  from  baz();
                 continue;
             }
 
+            if ($token->isGivenKind(T_EXTENDS) && $this->isMultilineExtendsWithMoreThanOneAncestor($tokens, $index)) {
+                continue;
+            }
+
             if ($token->isGivenKind(T_RETURN) && $this->isMultiLineReturn($tokens, $index)) {
                 continue;
             }
@@ -288,4 +292,28 @@ yield  from  baz();
 
         return false;
     }
+
+    /**
+     * @param int $index
+     *
+     * @return bool
+     */
+    private function isMultilineExtendsWithMoreThanOneAncestor(Tokens $tokens, $index)
+    {
+        $hasMoreThanOneAncestor = false;
+
+        for ($i = $index + 1; $i < $tokens->getNextTokenOfKind($index, ['{']); ++$i) {
+            $token = $tokens[$i];
+
+            if (false !== strpos($token->getContent(), "\n") && $hasMoreThanOneAncestor) {
+                return true;
+            }
+
+            if (',' === $token->getContent()) {
+                $hasMoreThanOneAncestor = true;
+            }
+        }
+
+        return false;
+    }
 }

+ 43 - 0
tests/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixerTest.php

@@ -920,6 +920,49 @@ $foo;',
                 '<?php class Foo extends /* foo */\InvalidArgumentException {}',
                 '<?php class Foo extends  /* foo */\InvalidArgumentException {}',
             ],
+            [
+                '<?php interface Foo extends Bar {}',
+                '<?php interface Foo extends  Bar {}',
+            ],
+            [
+                '<?php interface Foo extends Bar {}',
+                '<?php interface Foo extends
+
+Bar {}',
+            ],
+            [
+                '<?php interface Foo extends /* foo */Bar {}',
+                '<?php interface Foo extends/* foo */Bar {}',
+            ],
+            [
+                '<?php interface Foo extends /* foo */Bar {}',
+                '<?php interface Foo extends  /* foo */Bar {}',
+            ],
+            [
+                '<?php interface Foo extends Bar, Baz, Qux {}',
+                '<?php interface Foo extends  Bar, Baz, Qux {}',
+            ],
+            [
+                '<?php interface Foo extends Bar, Baz, Qux {}',
+                '<?php interface Foo extends
+
+Bar, Baz, Qux {}',
+            ],
+            [
+                '<?php interface Foo extends /* foo */Bar, Baz, Qux {}',
+                '<?php interface Foo extends/* foo */Bar, Baz, Qux {}',
+            ],
+            [
+                '<?php interface Foo extends /* foo */Bar, Baz, Qux {}',
+                '<?php interface Foo extends  /* foo */Bar, Baz, Qux {}',
+            ],
+            [
+                '<?php interface Foo extends
+    Bar,
+    Baz,
+    Qux
+{}',
+            ],
         ];
     }