Browse Source

NewWithBracesFixer - Fix object operator and curly brace open cases

SpacePossum 5 years ago
parent
commit
b703cc4749

+ 3 - 3
src/Fixer/Operator/NewWithBracesFixer.php

@@ -120,8 +120,8 @@ final class NewWithBracesFixer extends AbstractFixer
             }
 
             // entrance into array index syntax - need to look for exit
-            while ($nextToken->equals('[')) {
-                $nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
+            while ($nextToken->equals('[') || $nextToken->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
+                $nextIndex = $tokens->findBlockEnd($tokens->detectBlockType($nextToken)['type'], $nextIndex) + 1;
                 $nextToken = $tokens[$nextIndex];
             }
 
@@ -132,7 +132,7 @@ final class NewWithBracesFixer extends AbstractFixer
             }
 
             // new statement with () - nothing to do
-            if ($nextToken->equals('(')) {
+            if ($nextToken->equals('(') || $nextToken->isGivenKind(T_OBJECT_OPERATOR)) {
                 continue;
             }
 

+ 22 - 0
tests/Fixer/Operator/NewWithBracesFixerTest.php

@@ -74,6 +74,12 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
                 '<?php $foo = new $foo();',
                 '<?php $foo = new $foo;',
             ],
+            [
+                '<?php
+                    $bar1 = new $foo[0]->bar();
+                    $bar2 = new $foo[0][1]->bar();
+                ',
+            ],
             [
                 '<?php $xyz = new X(new Y(new Z()));',
                 '<?php $xyz = new X(new Y(new Z));',
@@ -106,6 +112,22 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
                 '<?php $a = new $b[$c]();',
                 '<?php $a = new $b[$c];',
             ],
+            [
+                '<?php $a = new $b[$c][0]();',
+                '<?php $a = new $b[$c][0];',
+            ],
+            [
+                '<?php $a = new $b{$c}();',
+                '<?php $a = new $b{$c};',
+            ],
+            [
+                '<?php $a = new $b{$c}{0}{1}() ?>',
+                '<?php $a = new $b{$c}{0}{1} ?>',
+            ],
+            [
+                '<?php $a = new $b{$c}[1]{0}[2]();',
+                '<?php $a = new $b{$c}[1]{0}[2];',
+            ],
             [
                 '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
                 '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',