Browse Source

Merge branch 'master' into 3.0

* master:
  merge changes between 2.15 and 2.16
  TernaryOperatorSpacesFixer - fix for discovering \":\" correctly
  StandardizeIncrementFixer - fix handling static properties
  PhpUnitNoExpectationAnnotationFixer - annotation in single line doc comment
  Priority test for NoBreakComment and NoUselessElse
  NoAlternativeSyntaxFixer - add support for switch and declare
  DX: add missing priority test for indentation_type and phpdoc_indent
  PhpdocTypesFixer - fix for multidimensional array
  DX: cleanup FunctionTypehintSpaceFixer
  Move all tests from AutoReview/FixerTest to Test/AbstractFixerTestCase
  DX: moving integration tests for braces, indentation_type and no_break_comment into right place
  BacktickToShellExecFixer - add priority relation to NativeFunctionInvocationFixer and SingleQuoteFixer

# Conflicts:
#	src/Fixer/Operator/IncrementStyleFixer.php
#	tests/AutoReview/FixerTest.php
SpacePossum 4 years ago
parent
commit
885e8b97c4

+ 1 - 1
src/AbstractPhpdocTypesFixer.php

@@ -127,7 +127,7 @@ abstract class AbstractPhpdocTypesFixer extends AbstractFixer
     private function normalizeType($type)
     {
         if ('[]' === substr($type, -2)) {
-            return $this->normalize(substr($type, 0, -2)).'[]';
+            return $this->normalizeType(substr($type, 0, -2)).'[]';
         }
 
         return $this->normalize($type);

+ 0 - 12
src/Config.php

@@ -39,18 +39,6 @@ class Config implements ConfigInterface
         $this->name = $name;
     }
 
-    /**
-     * @return static
-     *
-     * @deprecated since 2.17
-     */
-    public static function create()
-    {
-        @trigger_error(__METHOD__.' is deprecated since 2.17 and will be removed in 3.0.', E_USER_DEPRECATED);
-
-        return new static();
-    }
-
     /**
      * {@inheritdoc}
      */

+ 61 - 0
src/Fixer/AbstractIncrementOperatorFixer.php

@@ -0,0 +1,61 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Fixer;
+
+use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Tokenizer\Tokens;
+
+abstract class AbstractIncrementOperatorFixer extends AbstractFixer
+{
+    /**
+     * @param int $index
+     *
+     * @return int
+     */
+    final protected function findStart(Tokens $tokens, $index)
+    {
+        do {
+            $index = $tokens->getPrevMeaningfulToken($index);
+            $token = $tokens[$index];
+
+            $blockType = Tokens::detectBlockType($token);
+            if (null !== $blockType && !$blockType['isStart']) {
+                $index = $tokens->findBlockStart($blockType['type'], $index);
+                $token = $tokens[$index];
+            }
+        } while (!$token->equalsAny(['$', [T_VARIABLE]]));
+
+        $prevIndex = $tokens->getPrevMeaningfulToken($index);
+        $prevToken = $tokens[$prevIndex];
+
+        if ($prevToken->equals('$')) {
+            return $this->findStart($tokens, $index);
+        }
+
+        if ($prevToken->isGivenKind(T_OBJECT_OPERATOR)) {
+            return $this->findStart($tokens, $prevIndex);
+        }
+
+        if ($prevToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
+            $prevPrevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
+            if (!$tokens[$prevPrevIndex]->isGivenKind([T_STATIC, T_STRING])) {
+                return $this->findStart($tokens, $prevIndex);
+            }
+
+            $index = $tokens->getTokenNotOfKindSibling($prevIndex, -1, [[T_NS_SEPARATOR], [T_STATIC], [T_STRING]]);
+            $index = $tokens->getNextMeaningfulToken($index);
+        }
+
+        return $index;
+    }
+}

+ 1 - 1
src/Fixer/Alias/BacktickToShellExecFixer.php

@@ -56,7 +56,7 @@ EOT
     /**
      * {@inheritdoc}
      *
-     * Must run before EscapeImplicitBackslashesFixer, ExplicitStringVariableFixer.
+     * Must run before EscapeImplicitBackslashesFixer, ExplicitStringVariableFixer, NativeFunctionInvocationFixer, SingleQuoteFixer.
      */
     public function getPriority()
     {

+ 4 - 1
src/Fixer/Casing/ConstantCaseFixer.php

@@ -63,7 +63,10 @@ final class ConstantCaseFixer extends AbstractFixer implements ConfigurableFixer
     {
         return new FixerDefinition(
             'The PHP constants `true`, `false`, and `null` MUST be written using the correct casing.',
-            [new CodeSample("<?php\n\$a = FALSE;\n\$b = True;\n\$c = nuLL;\n")]
+            [
+                new CodeSample("<?php\n\$a = FALSE;\n\$b = True;\n\$c = nuLL;\n"),
+                new CodeSample("<?php\n\$a = FALSE;\n\$b = True;\n\$c = nuLL;\n", ['case' => 'upper']),
+            ]
         );
     }
 

+ 4 - 1
src/Fixer/Comment/CommentToPhpdocFixer.php

@@ -69,7 +69,10 @@ final class CommentToPhpdocFixer extends AbstractFixer implements ConfigurableFi
     {
         return new FixerDefinition(
             'Comments with annotation should be docblock when used on structural elements.',
-            [new CodeSample("<?php /* header */ \$x = true; /* @var bool \$isFoo */ \$isFoo = true;\n")],
+            [
+                new CodeSample("<?php /* header */ \$x = true; /* @var bool \$isFoo */ \$isFoo = true;\n"),
+                new CodeSample("<?php\n// @todo do something later\n\$foo = 1;\n\n// @var int \$a\n\$a = foo();\n", ['ignored_tags' => ['todo']]),
+            ],
             null,
             'Risky as new docblocks might mean more, e.g. a Doctrine entity might have a new column in database.'
         );

+ 17 - 13
src/Fixer/ControlStructure/NoAlternativeSyntaxFixer.php

@@ -52,14 +52,7 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
      */
     public function isCandidate(Tokens $tokens)
     {
-        return $tokens->isAnyTokenKindsFound(
-            [
-                T_ENDIF,
-                T_ENDWHILE,
-                T_ENDFOREACH,
-                T_ENDFOR,
-            ]
-        );
+        return $tokens->hasAlternativeSyntax();
     }
 
     /**
@@ -108,42 +101,47 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
      */
     private function fixOpenCloseControls($index, Token $token, Tokens $tokens)
     {
-        if ($token->isGivenKind([T_IF, T_FOREACH, T_WHILE, T_FOR])) {
+        if ($token->isGivenKind([T_IF, T_FOREACH, T_WHILE, T_FOR, T_SWITCH, T_DECLARE])) {
             $openIndex = $tokens->getNextTokenOfKind($index, ['(']);
             $closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
-            $afterParenthesisIndex = $tokens->getNextNonWhitespace($closeIndex);
+            $afterParenthesisIndex = $tokens->getNextMeaningfulToken($closeIndex);
             $afterParenthesis = $tokens[$afterParenthesisIndex];
 
             if (!$afterParenthesis->equals(':')) {
                 return;
             }
+
             $items = [];
+
             if (!$tokens[$afterParenthesisIndex - 1]->isWhitespace()) {
                 $items[] = new Token([T_WHITESPACE, ' ']);
             }
+
             $items[] = new Token('{');
 
             if (!$tokens[$afterParenthesisIndex + 1]->isWhitespace()) {
                 $items[] = new Token([T_WHITESPACE, ' ']);
             }
+
             $tokens->clearAt($afterParenthesisIndex);
             $tokens->insertAt($afterParenthesisIndex, $items);
         }
 
-        if (!$token->isGivenKind([T_ENDIF, T_ENDFOREACH, T_ENDWHILE, T_ENDFOR])) {
+        if (!$token->isGivenKind([T_ENDIF, T_ENDFOREACH, T_ENDWHILE, T_ENDFOR, T_ENDSWITCH, T_ENDDECLARE])) {
             return;
         }
 
         $nextTokenIndex = $tokens->getNextMeaningfulToken($index);
         $nextToken = $tokens[$nextTokenIndex];
         $tokens[$index] = new Token('}');
+
         if ($nextToken->equals(';')) {
             $tokens->clearAt($nextTokenIndex);
         }
     }
 
     /**
-     * Handle the else:.
+     * Handle the else: cases.
      *
      * @param int    $index  the index of the token being processed
      * @param Token  $token  the token being processed
@@ -157,6 +155,7 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
 
         $tokenAfterElseIndex = $tokens->getNextMeaningfulToken($index);
         $tokenAfterElse = $tokens[$tokenAfterElseIndex];
+
         if (!$tokenAfterElse->equals(':')) {
             return;
         }
@@ -176,9 +175,11 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
         if (!$token->isGivenKind(T_ELSEIF)) {
             return;
         }
+
         $parenthesisEndIndex = $this->findParenthesisEnd($tokens, $index);
         $tokenAfterParenthesisIndex = $tokens->getNextMeaningfulToken($parenthesisEndIndex);
         $tokenAfterParenthesis = $tokens[$tokenAfterParenthesisIndex];
+
         if (!$tokenAfterParenthesis->equals(':')) {
             return;
         }
@@ -187,7 +188,7 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
     }
 
     /**
-     * Add opening and closing braces to the else: and elseif: .
+     * Add opening and closing braces to the else: and elseif: cases.
      *
      * @param Tokens $tokens     the tokens collection
      * @param Token  $token      the current token
@@ -201,9 +202,11 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
             new Token([T_WHITESPACE, ' ']),
             $token,
         ];
+
         if (!$tokens[$index + 1]->isWhitespace()) {
             $items[] = new Token([T_WHITESPACE, ' ']);
         }
+
         $tokens->clearAt($index);
         $tokens->insertAt(
             $index,
@@ -214,6 +217,7 @@ final class NoAlternativeSyntaxFixer extends AbstractFixer
         $colonIndex += \count($items);
 
         $items = [new Token('{')];
+
         if (!$tokens[$colonIndex + 1]->isWhitespace()) {
             $items[] = new Token([T_WHITESPACE, ' ']);
         }

+ 10 - 0
src/Fixer/ControlStructure/NoBreakCommentFixer.php

@@ -76,6 +76,16 @@ switch ($foo) {
         return $tokens->isAnyTokenKindsFound([T_CASE, T_DEFAULT]);
     }
 
+    /**
+     * {@inheritdoc}
+     *
+     * Must run after NoUselessElseFixer.
+     */
+    public function getPriority()
+    {
+        return 0;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 1 - 1
src/Fixer/ControlStructure/NoUselessElseFixer.php

@@ -46,7 +46,7 @@ final class NoUselessElseFixer extends AbstractNoUselessElseFixer
     /**
      * {@inheritdoc}
      *
-     * Must run before BracesFixer, CombineConsecutiveUnsetsFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUselessReturnFixer, NoWhitespaceInBlankLineFixer, SimplifiedIfReturnFixer.
+     * Must run before BracesFixer, CombineConsecutiveUnsetsFixer, NoBreakCommentFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUselessReturnFixer, NoWhitespaceInBlankLineFixer, SimplifiedIfReturnFixer.
      * Must run after NoAlternativeSyntaxFixer, NoEmptyStatementFixer, NoUnneededCurlyBracesFixer.
      */
     public function getPriority()

+ 1 - 12
src/Fixer/FunctionNotation/FunctionTypehintSpaceFixer.php

@@ -17,7 +17,6 @@ use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
 use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
-use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
 /**
@@ -77,17 +76,7 @@ final class FunctionTypehintSpaceFixer extends AbstractFixer
                     continue;
                 }
 
-                $whitespaceTokenIndex = $type->getEndIndex() + 1;
-
-                if ($tokens[$whitespaceTokenIndex]->equals([T_WHITESPACE])) {
-                    if (' ' === $tokens[$whitespaceTokenIndex]->getContent()) {
-                        continue;
-                    }
-
-                    $tokens->clearAt($whitespaceTokenIndex);
-                }
-
-                $tokens->insertAt($whitespaceTokenIndex, new Token([T_WHITESPACE, ' ']));
+                $tokens->ensureWhitespaceAtIndex($type->getEndIndex() + 1, 0, ' ');
             }
         }
     }

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