Browse Source

StandardizeIncrementFixer - fix handling static properties

Kuba Werłos 4 years ago
parent
commit
07548b5bb3

+ 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;
+    }
+}

+ 3 - 46
src/Fixer/Operator/IncrementStyleFixer.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\Fixer\Operator;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractIncrementOperatorFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
@@ -26,7 +26,7 @@ use PhpCsFixer\Tokenizer\TokensAnalyzer;
  * @author Gregor Harlan <gharlan@web.de>
  * @author Kuba Werłos <werlos@gmail.com>
  */
-final class IncrementStyleFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class IncrementStyleFixer extends AbstractIncrementOperatorFixer implements ConfigurationDefinitionFixerInterface
 {
     /**
      * @internal
@@ -142,6 +142,7 @@ final class IncrementStyleFixer extends AbstractFixer implements ConfigurationDe
 
         while ($nextToken->equalsAny([
             '$',
+            '(',
             '[',
             [CT::T_DYNAMIC_PROP_BRACE_OPEN],
             [CT::T_DYNAMIC_VAR_BRACE_OPEN],
@@ -170,48 +171,4 @@ final class IncrementStyleFixer extends AbstractFixer implements ConfigurationDe
 
         return $index;
     }
-
-    /**
-     * @param int $index
-     *
-     * @return int
-     */
-    private 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('$')) {
-            $index = $prevIndex;
-            $prevIndex = $tokens->getPrevMeaningfulToken($index);
-            $prevToken = $tokens[$prevIndex];
-        }
-
-        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;
-    }
 }

+ 3 - 37
src/Fixer/Operator/StandardizeIncrementFixer.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\Fixer\Operator;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractIncrementOperatorFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\Tokenizer\CT;
@@ -22,7 +22,7 @@ use PhpCsFixer\Tokenizer\Tokens;
 /**
  * @author ntzm
  */
-final class StandardizeIncrementFixer extends AbstractFixer
+final class StandardizeIncrementFixer extends AbstractIncrementOperatorFixer
 {
     /**
      * @internal
@@ -93,7 +93,7 @@ final class StandardizeIncrementFixer extends AbstractFixer
                 continue;
             }
 
-            $startIndex = $this->findStart($tokens, $tokens->getPrevMeaningfulToken($operatorIndex));
+            $startIndex = $this->findStart($tokens, $operatorIndex);
 
             $this->clearRangeLeaveComments(
                 $tokens,
@@ -108,40 +108,6 @@ final class StandardizeIncrementFixer extends AbstractFixer
         }
     }
 
-    /**
-     * Find the start of a reference.
-     *
-     * @param int $index
-     *
-     * @return int
-     */
-    private function findStart(Tokens $tokens, $index)
-    {
-        while (!$tokens[$index]->equalsAny(['$', [T_VARIABLE]])) {
-            if ($tokens[$index]->equals(']')) {
-                $index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
-            } elseif ($tokens[$index]->isGivenKind(CT::T_DYNAMIC_PROP_BRACE_CLOSE)) {
-                $index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_DYNAMIC_PROP_BRACE, $index);
-            } elseif ($tokens[$index]->isGivenKind(CT::T_DYNAMIC_VAR_BRACE_CLOSE)) {
-                $index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_DYNAMIC_VAR_BRACE, $index);
-            } elseif ($tokens[$index]->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE)) {
-                $index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE, $index);
-            } else {
-                $index = $tokens->getPrevMeaningfulToken($index);
-            }
-        }
-
-        while ($tokens[$tokens->getPrevMeaningfulToken($index)]->equals('$')) {
-            $index = $tokens->getPrevMeaningfulToken($index);
-        }
-
-        if ($tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_OBJECT_OPERATOR)) {
-            return $this->findStart($tokens, $tokens->getPrevMeaningfulToken($index));
-        }
-
-        return $index;
-    }
-
     /**
      * Clear tokens in the given range unless they are comments.
      *

+ 1 - 1
src/FixerFactory.php

@@ -96,7 +96,7 @@ final class FixerFactory
             $builtInFixers = [];
 
             /** @var SplFileInfo $file */
-            foreach (SymfonyFinder::create()->files()->in(__DIR__.'/Fixer') as $file) {
+            foreach (SymfonyFinder::create()->files()->in(__DIR__.'/Fixer')->depth(1) as $file) {
                 $relativeNamespace = $file->getRelativePath();
                 $fixerClass = 'PhpCsFixer\\Fixer\\'.($relativeNamespace ? $relativeNamespace.'\\' : '').$file->getBasename('.php');
                 if ('Fixer' === substr($fixerClass, -5)) {

+ 11 - 1
tests/Fixer/Operator/IncrementStyleFixerTest.php

@@ -20,6 +20,7 @@ use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  *
  * @internal
  *
+ * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer
  * @covers \PhpCsFixer\Fixer\Operator\IncrementStyleFixer
  */
 final class IncrementStyleFixerTest extends AbstractFixerTestCase
@@ -57,7 +58,7 @@ final class IncrementStyleFixerTest extends AbstractFixerTestCase
 
     public function provideFixPreIncrementCases()
     {
-        return [
+        $cases = [
             [
                 '<?php ++$a;',
                 '<?php $a++;',
@@ -178,5 +179,14 @@ final class IncrementStyleFixerTest extends AbstractFixerTestCase
                 '<?php if ($foo) $a++;',
             ],
         ];
+
+        if (\PHP_VERSION_ID >= 70000) {
+            $cases[] = [
+                '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
+                '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h++;',
+            ];
+        }
+
+        return $cases;
     }
 }

+ 51 - 0
tests/Fixer/Operator/StandardizeIncrementFixerTest.php

@@ -19,6 +19,7 @@ use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  *
  * @internal
  *
+ * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer
  * @covers \PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer
  */
 final class StandardizeIncrementFixerTest extends AbstractFixerTestCase
@@ -526,6 +527,40 @@ $i#3
                 '<?php $i *= 1; ++$i;',
                 '<?php $i *= 1; $i += 1;',
             ],
+            [
+                '<?php ++A::$b;',
+                '<?php A::$b += 1;',
+            ],
+            [
+                '<?php ++\A::$b;',
+                '<?php \A::$b += 1;',
+            ],
+            [
+                '<?php ++\A\B\C::$d;',
+                '<?php \A\B\C::$d += 1;',
+            ],
+            [
+                '<?php ++$a::$b;',
+                '<?php $a::$b += 1;',
+            ],
+            [
+                '<?php ++$a::$b->$c;',
+                '<?php $a::$b->$c += 1;',
+            ],
+            [
+                '<?php class Foo {
+                    public static function bar() {
+                        ++self::$v1;
+                        ++static::$v2;
+                    }
+                }',
+                '<?php class Foo {
+                    public static function bar() {
+                        self::$v1 += 1;
+                        static::$v2 += 1;
+                    }
+                }',
+            ],
         ];
     }
 
@@ -556,6 +591,22 @@ $i#3
             [
                 '<?php $i += 1 <=> 2;',
             ],
+            [
+                '<?php ++$a::$b::$c;',
+                '<?php $a::$b::$c += 1;',
+            ],
+            [
+                '<?php ++$a->$b::$c;',
+                '<?php $a->$b::$c += 1;',
+            ],
+            [
+                '<?php ++$a::${$b}::$c;',
+                '<?php $a::${$b}::$c += 1;',
+            ],
+            [
+                '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
+                '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h += 1;',
+            ],
         ];
     }