Browse Source

NoBlankLines*: fix removing lines consisting only of spaces

Kuba Werłos 6 years ago
parent
commit
7df251c575

+ 2 - 3
src/AbstractLinesBeforeNamespaceFixer.php

@@ -99,13 +99,12 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implement
 
             return;
         }
-        $newWhitespaceToken = new Token([T_WHITESPACE, str_repeat($lineEnding, $newlinesForWhitespaceToken)]);
         if ($previous->isWhitespace()) {
             // Fix the previous whitespace token
-            $tokens[$previousIndex] = $newWhitespaceToken;
+            $tokens[$previousIndex] = new Token([T_WHITESPACE, str_repeat($lineEnding, $newlinesForWhitespaceToken).substr($previous->getContent(), strrpos($previous->getContent(), "\n") + 1)]);
         } else {
             // Add a new whitespace token
-            $tokens->insertAt($index, $newWhitespaceToken);
+            $tokens->insertAt($index, new Token([T_WHITESPACE, str_repeat($lineEnding, $newlinesForWhitespaceToken)]));
         }
     }
 }

+ 1 - 3
src/Fixer/ClassNotation/NoBlankLinesAfterClassOpeningFixer.php

@@ -18,7 +18,6 @@ use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
-use PhpCsFixer\Utils;
 
 /**
  * @author Ceeram <ceeram@cakephp.org>
@@ -87,8 +86,7 @@ final class Sample
         // if there is more than one new line in the whitespace, then we need to fix it
         if (substr_count($content, "\n") > 1) {
             // the final bit of the whitespace must be the next statement's indentation
-            $lines = Utils::splitLines($content);
-            $tokens[$index] = new Token([T_WHITESPACE, $this->whitespacesConfig->getLineEnding().end($lines)]);
+            $tokens[$index] = new Token([T_WHITESPACE, $this->whitespacesConfig->getLineEnding().substr($content, strrpos($content, "\n") + 1)]);
         }
     }
 }

+ 1 - 3
src/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixer.php

@@ -17,7 +17,6 @@ use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
-use PhpCsFixer\Utils;
 
 /**
  * @author Graham Campbell <graham@alt-three.com>
@@ -107,8 +106,7 @@ class Bar {}
         // if there is more than one new line in the whitespace, then we need to fix it
         if (substr_count($content, "\n") > 1) {
             // the final bit of the whitespace must be the next statement's indentation
-            $lines = Utils::splitLines($content);
-            $tokens[$index] = new Token([T_WHITESPACE, "\n".end($lines)]);
+            $tokens[$index] = new Token([T_WHITESPACE, substr($content, strrpos($content, "\n"))]);
         }
     }
 }

+ 0 - 1
tests/AutoReview/FixerFactoryTest.php

@@ -149,7 +149,6 @@ final class FixerFactoryTest extends TestCase
             [$fixers['no_useless_return'], $fixers['blank_line_before_statement']],
             [$fixers['no_useless_return'], $fixers['no_extra_blank_lines']],
             [$fixers['no_useless_return'], $fixers['no_whitespace_in_blank_line']],
-            [$fixers['no_whitespace_in_blank_line'], $fixers['no_blank_lines_after_phpdoc']],
             [$fixers['ordered_class_elements'], $fixers['class_attributes_separation']],
             [$fixers['ordered_class_elements'], $fixers['method_separation']],
             [$fixers['ordered_class_elements'], $fixers['no_blank_lines_after_class_opening']],

+ 34 - 0
tests/Fixer/ClassNotation/NoBlankLinesAfterClassOpeningFixerTest.php

@@ -130,6 +130,40 @@ class Good
 }',
         ];
 
+        // check if line with spaces is removed when next token is indented
+        $cases[] = [
+            '<?php
+class Foo
+{
+    function bar() {}
+}
+',
+            '<?php
+class Foo
+{
+    '.'
+    function bar() {}
+}
+',
+        ];
+
+        // check if line with spaces is removed when next token is not indented
+        $cases[] = [
+            '<?php
+class Foo
+{
+function bar() {}
+}
+',
+            '<?php
+class Foo
+{
+    '.'
+function bar() {}
+}
+',
+        ];
+
         return $cases;
     }
 

+ 22 - 0
tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php

@@ -53,6 +53,28 @@ final class NoBlankLinesBeforeNamespaceFixerTest extends AbstractFixerTestCase
             ["<?php\nnamespace X;", "<?php\r\n\r\n\r\n\r\nnamespace X;"],
             ["<?php\r\nnamespace X;", "<?php\r\n\r\n\r\n\r\nnamespace X;", new WhitespacesFixerConfig('    ', "\r\n")],
             ["<?php\n\nnamespace\\Sub\\Foo::bar();"],
+            [
+                '<?php
+    // Foo
+    namespace Foo;
+',
+                '<?php
+    // Foo
+    '.'
+    namespace Foo;
+',
+            ],
+            [
+                '<?php
+// Foo
+namespace Foo;
+',
+                '<?php
+// Foo
+    '.'
+namespace Foo;
+',
+            ],
         ];
     }
 

+ 34 - 0
tests/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixerTest.php

@@ -144,6 +144,40 @@ EOF;
         $this->doTest($expected);
     }
 
+    public function testLineWithSpacesIsRemovedWhenNextTokenIsIndented()
+    {
+        $this->doTest(
+            '<?php
+                /**
+                 * PHPDoc with a line with space
+                 */
+                class Foo {}',
+            '<?php
+                /**
+                 * PHPDoc with a line with space
+                 */
+                '.'
+                class Foo {}'
+            );
+    }
+
+    public function testLineWithSpacesIsRemovedWhenNextTokenIsNotIndented()
+    {
+        $this->doTest(
+            '<?php
+    /**
+     * PHPDoc with a line with space
+     */
+class Foo {}',
+            '<?php
+    /**
+     * PHPDoc with a line with space
+     */
+    '.'
+class Foo {}'
+        );
+    }
+
     public function testFixesSimpleClass()
     {
         $expected = <<<'EOF'

+ 0 - 0
tests/Fixtures/Integration/priority/no_whitespace_in_blank_line,no_blank_lines_after_phpdoc.test → tests/Fixtures/Integration/misc/no_whitespace_in_blank_line,no_blank_lines_after_phpdoc.test