Browse Source

bug #2044 SingleClassElementPerStatementFixer - fix array handling (keradus)

This PR was merged into the 2.0-dev branch.

Discussion
----------

SingleClassElementPerStatementFixer - fix array handling

Fixes #2046

Commits
-------

4d0f685 SingleClassElementPerStatementFixer - fix array handling
Dariusz Ruminski 8 years ago
parent
commit
cb718cc3cd

+ 40 - 4
src/Fixer/ClassNotation/SingleClassElementPerStatementFixer.php

@@ -23,6 +23,7 @@ use PhpCsFixer\Tokenizer\TokensAnalyzer;
  *
  * @author Javier Spagnoletti <phansys@gmail.com>
  * @author SpacePossum
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
 final class SingleClassElementPerStatementFixer extends AbstractFixer
 {
@@ -100,9 +101,31 @@ final class SingleClassElementPerStatementFixer extends AbstractFixer
      */
     private function fixElement(Tokens $tokens, $index)
     {
-        $repeatIndex = $tokens->getNextTokenOfKind($index, array(',', ';'));
-        if (!$tokens[$repeatIndex]->equals(',')) {
-            return; // no repeating found, no fixing needed
+        $tokensAnalyzer = new TokensAnalyzer($tokens);
+        $repeatIndex = $index;
+
+        while (true) {
+            $repeatIndex = $tokens->getNextMeaningfulToken($repeatIndex);
+            $repeatToken = $tokens[$repeatIndex];
+
+            if ($tokensAnalyzer->isArray($repeatIndex)) {
+                if ($repeatToken->isGivenKind(T_ARRAY)) {
+                    $repeatIndex = $tokens->getNextTokenOfKind($repeatIndex, array('('));
+                    $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $repeatIndex);
+                } else {
+                    $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $repeatIndex);
+                }
+
+                continue;
+            }
+
+            if ($repeatToken->equals(';')) {
+                return; // no repeating found, no fixing needed
+            }
+
+            if ($repeatToken->equals(',')) {
+                break;
+            }
         }
 
         $start = $tokens->getPrevTokenOfKind($index, array(';', '{', '}'));
@@ -130,11 +153,23 @@ final class SingleClassElementPerStatementFixer extends AbstractFixer
 
         // iterate variables to split up
         for ($i = $endIndex - 1; $i > $startIndex; --$i) {
+            $token = $tokens[$i];
+
+            if ($token->equals(')')) {
+                $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $i, false);
+                continue;
+            }
+
+            if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_CLOSE)) {
+                $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $i, false);
+                continue;
+            }
+
             if (!$tokens[$i]->equals(',')) {
                 continue;
             }
 
-            $tokens[$i]->setContent(';');
+            $token->setContent(';');
             if ($tokens[$i + 1]->isWhitespace()) {
                 $tokens[$i + 1]->clear();
             }
@@ -142,6 +177,7 @@ final class SingleClassElementPerStatementFixer extends AbstractFixer
             if ($divisionContent) {
                 $tokens->insertAt($i + 1, new Token(array(T_WHITESPACE, $divisionContent)));
             }
+
             // collect modifiers
             $sequence = $this->getModifiersSequences($tokens, $startIndex, $endIndex);
             $tokens->insertAt($i + 2, $sequence);

+ 25 - 5
tests/Fixer/ClassNotation/SingleClassElementPerStatementFixerTest.php

@@ -32,8 +32,28 @@ final class SingleClassElementPerStatementFixerTest extends AbstractFixerTestCas
     public function provideCases()
     {
         return array(
-                array(
-'<?php
+            array(
+                '<?php
+class Foo
+{
+    private static $bar1 = array(1,2,3);
+    private static $bar2 = [1,2,3];
+    private static $baz1 = array(array(1,2), array(3, 4));
+    private static $baz2 = array(1,2,3);
+    private static $aaa1 = 1;
+    private static $aaa2 = array(2, 2);
+    private static $aaa3 = 3;
+}',
+                '<?php
+class Foo
+{
+    private static $bar1 = array(1,2,3), $bar2 = [1,2,3];
+    private static $baz1 = array(array(1,2), array(3, 4)), $baz2 = array(1,2,3);
+    private static $aaa1 = 1, $aaa2 = array(2, 2), $aaa3 = 3;
+}',
+            ),
+            array(
+                '<?php
 class Foo
 {
     const A = 1;
@@ -42,7 +62,7 @@ class Foo
 
 echo Foo::A, Foo::B;
 ',
-'<?php
+                '<?php
 class Foo
 {
     const A = 1, B = 2;
@@ -50,8 +70,8 @@ class Foo
 
 echo Foo::A, Foo::B;
 ',
-                ),
-                array(
+            ),
+            array(
                 <<<'EOT'
 <?php
 

+ 2 - 2
tests/Fixtures/Integration/misc/PHP7.test

@@ -44,7 +44,7 @@ class Foo implements FooInterface
     const FOR = 1;
 
     // array const
-    // const ARR = array(1, 2);
+    const ARR = array(1, 2);
 
     // semi-reserved keyword as function name
     public function list($a, $b)
@@ -143,7 +143,7 @@ class Foo implements FooInterface
     const FOR = 1;
 
     // array const
-    // const ARR = array(1, 2);
+    const ARR = array(1, 2);
 
     // semi-reserved keyword as function name
     public function list($a,$b)