Browse Source

Fixers - better comments handling

SpacePossum 8 years ago
parent
commit
4b5d15fb8c

+ 1 - 1
src/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixer.php

@@ -74,7 +74,7 @@ final class NoWhitespaceBeforeCommaInArrayFixer extends AbstractFixer
             $i = $this->skipNonArrayElements($i, $tokens);
             $currentToken = $tokens[$i];
             $prevIndex = $tokens->getPrevNonWhitespace($i - 1);
-            if ($currentToken->equals(',') && !$tokens[$prevIndex]->equals(array(T_END_HEREDOC))) {
+            if ($currentToken->equals(',') && !$tokens[$prevIndex]->equals(array(T_END_HEREDOC)) && !$tokens[$prevIndex]->isComment()) {
                 $tokens->removeLeadingWhitespace($i);
             }
         }

+ 22 - 12
src/Fixer/ClassNotation/ClassDefinitionFixer.php

@@ -294,7 +294,9 @@ interface Bar extends
         }
 
         if ($tokens[$openIndex - 1]->isWhitespace()) {
-            $tokens[$openIndex - 1]->setContent($spacing);
+            if (' ' !== $spacing || !$tokens[$tokens->getPrevNonWhitespace($openIndex - 1)]->isComment()) {
+                $tokens[$openIndex - 1]->setContent($spacing);
+            }
 
             return $openIndex;
         }
@@ -379,20 +381,28 @@ interface Bar extends
     {
         for ($i = $endIndex; $i >= $startIndex; --$i) {
             if ($tokens[$i]->isWhitespace()) {
-                if (
-                    $tokens[$i + 1]->equalsAny(array(',', '(', ')'))
-                    || $tokens[$i - 1]->equals('(')
-                ) {
+                $prevNonWhite = $tokens->getPrevNonWhitespace($i);
+                $nextNonWhite = $tokens->getNextNonWhitespace($i);
+
+                if ($tokens[$prevNonWhite]->isComment() || $tokens[$nextNonWhite]->isComment()) {
+                    $content = $tokens[$prevNonWhite]->getContent();
+                    if (!('#' === $content || '//' === substr($content, 0, 2))) {
+                        $content = $tokens[$nextNonWhite]->getContent();
+                        if (!('#' === $content || '//' === substr($content, 0, 2))) {
+                            $tokens[$i]->setContent(' ');
+                        }
+                    }
+
+                    continue;
+                }
+
+                if ($tokens[$i + 1]->equalsAny(array(',', '(', ')')) || $tokens[$i - 1]->equals('(')) {
                     $tokens[$i]->clear();
-                } elseif (
-                    !$tokens[$i + 1]->isComment()
-                    && !($tokens[$i - 1]->isGivenKind(T_COMMENT)
-                        && ('#' === substr($tokens[$i - 1]->getContent(), 0, 1) || '//' === substr($tokens[$i - 1]->getContent(), 0, 2)))
-                ) {
-                    $tokens[$i]->setContent(' ');
+
+                    continue;
                 }
 
-                --$i;
+                $tokens[$i]->setContent(' ');
 
                 continue;
             }

+ 30 - 8
src/Fixer/ControlStructure/IncludeFixer.php

@@ -64,7 +64,10 @@ include_once("sample4.php");
     {
         foreach (array_reverse($includies) as $includy) {
             if ($includy['end'] && !$tokens[$includy['end']]->isGivenKind(T_CLOSE_TAG)) {
-                $tokens->removeLeadingWhitespace($includy['end']);
+                $afterEndIndex = $tokens->getNextNonWhitespace($includy['end']);
+                if (null === $afterEndIndex || !$tokens[$afterEndIndex]->isComment()) {
+                    $tokens->removeLeadingWhitespace($includy['end']);
+                }
             }
 
             $braces = $includy['braces'];
@@ -73,13 +76,15 @@ include_once("sample4.php");
                 $nextToken = $tokens[$tokens->getNextMeaningfulToken($braces['close'])];
 
                 if ($nextToken->equalsAny(array(';', array(T_CLOSE_TAG)))) {
-                    $tokens->removeLeadingWhitespace($braces['open']);
-                    $tokens->removeTrailingWhitespace($braces['open']);
-                    $tokens->removeLeadingWhitespace($braces['close']);
-                    $tokens->removeTrailingWhitespace($braces['close']);
-
-                    $tokens[$braces['open']] = new Token(array(T_WHITESPACE, ' '));
-                    $tokens[$braces['close']]->clear();
+                    $this->removeWhitespaceAroundIfPossible($tokens, $braces['open']);
+                    $this->removeWhitespaceAroundIfPossible($tokens, $braces['close']);
+                    $tokens->clearTokenAndMergeSurroundingWhitespace($braces['open']);
+                    $tokens->clearTokenAndMergeSurroundingWhitespace($braces['close']);
+
+                    $nextSiblingIndex = $tokens->getNonEmptySibling($includy['begin'], 1);
+                    if (!$tokens[$nextSiblingIndex]->isWhitespace()) {
+                        $tokens->insertAt($nextSiblingIndex, new Token(array(T_WHITESPACE, ' ')));
+                    }
                 }
             }
 
@@ -134,4 +139,21 @@ include_once("sample4.php");
 
         return $includies;
     }
+
+    /**
+     * @param Tokens $tokens
+     * @param int    $index
+     */
+    private function removeWhitespaceAroundIfPossible(Tokens $tokens, $index)
+    {
+        $nextIndex = $tokens->getNextNonWhitespace($index);
+        if (null === $nextIndex || !$tokens[$nextIndex]->isComment()) {
+            $tokens->removeLeadingWhitespace($index);
+        }
+
+        $prevIndex = $tokens->getPrevNonWhitespace($index);
+        if (null === $prevIndex || !$tokens[$prevIndex]->isComment()) {
+            $tokens->removeTrailingWhitespace($index);
+        }
+    }
 }

+ 5 - 2
src/Fixer/ControlStructure/SwitchCaseSpaceFixer.php

@@ -81,9 +81,12 @@ final class SwitchCaseSpaceFixer extends AbstractFixer
             }
 
             $valueIndex = $tokens->getPrevNonWhitespace($colonIndex);
-            if (2 + $valueIndex === $colonIndex) {
-                $tokens[$valueIndex + 1]->clear();
+            // skip if there is no space between the colon and previous token or is space after comment
+            if ($valueIndex === $colonIndex - 1 || $tokens[$valueIndex]->isComment()) {
+                continue;
             }
+
+            $tokens[$valueIndex + 1]->clear();
         }
     }
 }

+ 1 - 1
src/Fixer/FunctionNotation/FunctionDeclarationFixer.php

@@ -128,7 +128,7 @@ class Foo
 
             // remove whitespace before (
             // eg: `function foo () {}` => `function foo() {}`
-            if (!$isLambda && $tokens[$startParenthesisIndex - 1]->isWhitespace()) {
+            if (!$isLambda && $tokens[$startParenthesisIndex - 1]->isWhitespace() && !$tokens[$tokens->getPrevNonWhitespace($startParenthesisIndex - 1)]->isComment()) {
                 $tokens[$startParenthesisIndex - 1]->clear();
             }
 

+ 3 - 1
src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php

@@ -86,11 +86,13 @@ final class MethodArgumentSpaceFixer extends AbstractFixer
 
             if ($token->equals(')')) {
                 $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index, false);
+
                 continue;
             }
 
             if ($token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_CLOSE)) {
                 $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index, false);
+
                 continue;
             }
 
@@ -112,7 +114,7 @@ final class MethodArgumentSpaceFixer extends AbstractFixer
         if ($tokens[$index - 1]->isWhitespace()) {
             $prevIndex = $tokens->getPrevNonWhitespace($index - 1);
 
-            if (!$tokens[$prevIndex]->equalsAny(array(',', array(T_END_HEREDOC)))) {
+            if (!$tokens[$prevIndex]->equalsAny(array(',', array(T_END_HEREDOC))) && !$tokens[$prevIndex]->isComment()) {
                 $tokens[$index - 1]->clear();
             }
         }

+ 9 - 4
src/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixer.php

@@ -124,6 +124,7 @@ function example($foo = "two words", $bar) {}
 
             if ($token->equals('=')) {
                 $i = $tokens->getPrevMeaningfulToken($i);
+
                 continue;
             }
 
@@ -156,7 +157,7 @@ function example($foo = "two words", $bar) {}
     private function removeDefaultArgument(Tokens $tokens, $startIndex, $endIndex)
     {
         for ($i = $startIndex; $i <= $endIndex;) {
-            $tokens[$i]->clear();
+            $tokens->clearTokenAndMergeSurroundingWhitespace($i);
             $this->clearWhitespacesBeforeIndex($tokens, $i);
             $i = $tokens->getNextMeaningfulToken($i);
         }
@@ -197,10 +198,14 @@ function example($foo = "two words", $bar) {}
      */
     private function clearWhitespacesBeforeIndex(Tokens $tokens, $index)
     {
-        $token = $tokens[$index - 1];
+        $prevIndex = $tokens->getNonEmptySibling($index, -1);
+        if (!$tokens[$prevIndex]->isWhitespace()) {
+            return;
+        }
 
-        if ($token->isGivenKind(T_WHITESPACE)) {
-            $token->clear();
+        $prevNonWhiteIndex = $tokens->getPrevNonWhitespace($prevIndex);
+        if (null === $prevNonWhiteIndex || !$tokens[$prevNonWhiteIndex]->isComment()) {
+            $tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
         }
     }
 }

+ 9 - 9
src/Fixer/FunctionNotation/ReturnTypeDeclarationFixer.php

@@ -111,27 +111,27 @@ final class ReturnTypeDeclarationFixer extends AbstractFixer implements Configur
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
-            $token = $tokens[$index];
-
-            if (!$token->isGivenKind(CT::T_TYPE_COLON)) {
+            if (!$tokens[$index]->isGivenKind(CT::T_TYPE_COLON)) {
                 continue;
             }
 
             $previousToken = $tokens[$index - 1];
 
             if ($previousToken->isWhitespace()) {
-                if ('none' === $this->configuration) {
-                    $previousToken->clear();
-                } else {
-                    $previousToken->setContent(' ');
+                if (!$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
+                    if ('none' === $this->configuration) {
+                        $previousToken->clear();
+                    } else {
+                        $previousToken->setContent(' ');
+                    }
                 }
             } elseif ('one' === $this->configuration) {
-                $tokens->ensureWhitespaceAtIndex($index, 0, ' ');
+                $limit += $tokens->ensureWhitespaceAtIndex($index, 0, ' ') ? 1 : 0;
                 ++$index;
             }
 
             ++$index;
-            $tokens->ensureWhitespaceAtIndex($index, 0, ' ');
+            $limit += $tokens->ensureWhitespaceAtIndex($index, 0, ' ') ? 1 : 0;
         }
     }
 }

+ 7 - 2
src/Fixer/LanguageConstruct/DeclareEqualNormalizeFixer.php

@@ -106,7 +106,9 @@ final class DeclareEqualNormalizeFixer extends AbstractFixer implements Configur
         }
 
         if ($tokens[$index - 1]->isWhitespace()) {
-            $tokens[$index - 1]->setContent(' ');
+            if (!$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
+                $tokens[$index - 1]->setContent(' ');
+            }
         } else {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
         }
@@ -118,7 +120,10 @@ final class DeclareEqualNormalizeFixer extends AbstractFixer implements Configur
      */
     private function removeWhitespaceAroundToken(Tokens $tokens, $index)
     {
-        $tokens->removeLeadingWhitespace($index);
+        if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isComment()) {
+            $tokens->removeLeadingWhitespace($index);
+        }
+
         $tokens->removeTrailingWhitespace($index);
     }
 }

+ 8 - 2
src/Fixer/LanguageConstruct/DirConstantFixer.php

@@ -81,11 +81,17 @@ final class DirConstantFixer extends AbstractFunctionReferenceFixer
             }
 
             // closing parenthesis removed with leading spaces
-            $tokens->removeLeadingWhitespace($closeParenthesis);
+            if (!$tokens[$tokens->getNextNonWhitespace($closeParenthesis)]->isComment()) {
+                $tokens->removeLeadingWhitespace($closeParenthesis);
+            }
+
             $tokens[$closeParenthesis]->clear();
 
             // opening parenthesis removed with trailing and leading spaces
-            $tokens->removeLeadingWhitespace($openParenthesis);
+            if (!$tokens[$tokens->getNextNonWhitespace($openParenthesis)]->isComment()) {
+                $tokens->removeLeadingWhitespace($openParenthesis);
+            }
+
             $tokens->removeTrailingWhitespace($openParenthesis);
             $tokens[$openParenthesis]->clear();
 

Some files were not shown because too many files changed in this diff