Browse Source

Merge branch '1.12'

Conflicts:
	src/Fixer/Operator/NewWithBracesFixer.php
Dariusz Ruminski 8 years ago
parent
commit
7e24383b4b

+ 56 - 4
src/Fixer/Operator/NewWithBracesFixer.php

@@ -34,6 +34,61 @@ final class NewWithBracesFixer extends AbstractFixer
      */
     public function fix(\SplFileInfo $file, Tokens $tokens)
     {
+        static $nextTokenKinds = null;
+
+        if (null === $nextTokenKinds) {
+            $nextTokenKinds = array(
+                '?',
+                ';',
+                ',',
+                '(',
+                ')',
+                '[',
+                ']',
+                ':',
+                '<',
+                '>',
+                '+',
+                '-',
+                '*',
+                '/',
+                '%',
+                '&',
+                '^',
+                '|',
+                array(T_IS_SMALLER_OR_EQUAL),
+                array(T_IS_GREATER_OR_EQUAL),
+                array(T_IS_EQUAL),
+                array(T_IS_NOT_EQUAL),
+                array(T_IS_IDENTICAL),
+                array(T_IS_NOT_IDENTICAL),
+                array(T_CLOSE_TAG),
+                array(T_LOGICAL_AND),
+                array(T_LOGICAL_OR),
+                array(T_LOGICAL_XOR),
+                array(T_BOOLEAN_AND),
+                array(T_BOOLEAN_OR),
+                array(T_INC),
+                array(T_DEC),
+                array(T_SL),
+                array(T_SR),
+                array(T_INSTANCEOF),
+                array(T_AS),
+                array(T_DOUBLE_ARROW),
+                array(CT_ARRAY_SQUARE_BRACE_OPEN),
+                array(CT_ARRAY_SQUARE_BRACE_CLOSE),
+                array(CT_BRACE_CLASS_INSTANTIATION_OPEN),
+                array(CT_BRACE_CLASS_INSTANTIATION_CLOSE),
+            );
+            if (defined('T_POW')) {
+                $nextTokenKinds[] = array(T_POW);
+            }
+
+            if (defined('T_SPACESHIP')) {
+                $nextTokenKinds[] = array(T_SPACESHIP);
+            }
+        }
+
         for ($index = $tokens->count() - 3; $index > 0; --$index) {
             $token = $tokens[$index];
 
@@ -41,10 +96,7 @@ final class NewWithBracesFixer extends AbstractFixer
                 continue;
             }
 
-            $nextIndex = $tokens->getNextTokenOfKind(
-                $index,
-                array(':', ';', ',', '(', ')', '[', ']', array(T_CLOSE_TAG), array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE))
-            );
+            $nextIndex = $tokens->getNextTokenOfKind($index, $nextTokenKinds);
             $nextToken = $tokens[$nextIndex];
 
             // entrance into array index syntax - need to look for exit

+ 148 - 4
tests/Fixer/Operator/NewWithBracesFixerTest.php

@@ -22,9 +22,9 @@ use PhpCsFixer\Test\AbstractFixerTestCase;
 final class NewWithBracesFixerTest extends AbstractFixerTestCase
 {
     /**
-     * @dataProvider provideStandardCases
+     * @dataProvider provideCases
      */
-    public function testStandard($expected, $input = null)
+    public function testFix($expected, $input = null)
     {
         $this->doTest($expected, $input);
     }
@@ -33,12 +33,30 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
      * @dataProvider provide54Cases
      * @requires PHP 5.4
      */
-    public function test54($expected, $input = null)
+    public function testFix54($expected, $input = null)
     {
         $this->doTest($expected, $input);
     }
 
-    public function provideStandardCases()
+    /**
+     * @dataProvider provide56Cases
+     * @requires PHP 5.6
+     */
+    public function testFix56($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @dataProvider provide70Cases
+     * @requires PHP 7.0
+     */
+    public function testFix70($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideCases()
     {
         return array(
             array(
@@ -126,6 +144,104 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
                 '<?php $b = new \StdClass() /**/?>',
                 '<?php $b = new \StdClass /**/?>',
             ),
+            array(
+                '<?php $a = new Foo() instanceof Foo;',
+                '<?php $a = new Foo instanceof Foo;',
+            ),
+            array(
+                '<?php
+                    $a = new Foo() + 1;
+                    $a = new Foo() - 1;
+                    $a = new Foo() * 1;
+                    $a = new Foo() / 1;
+                    $a = new Foo() % 1;
+                ',
+                '<?php
+                    $a = new Foo + 1;
+                    $a = new Foo - 1;
+                    $a = new Foo * 1;
+                    $a = new Foo / 1;
+                    $a = new Foo % 1;
+                ',
+            ),
+            array(
+                '<?php
+                    $a = new Foo()++;
+                    $a = new Foo()--;
+                ',
+                '<?php
+                    $a = new Foo++;
+                    $a = new Foo--;
+                ',
+            ),
+            array(
+                '<?php
+                    $a = new Foo() & 1;
+                    $a = new Foo() | 1;
+                    $a = new Foo() ^ 1;
+                    $a = new Foo() << 1;
+                    $a = new Foo() >> 1;
+                ',
+                '<?php
+                    $a = new Foo & 1;
+                    $a = new Foo | 1;
+                    $a = new Foo ^ 1;
+                    $a = new Foo << 1;
+                    $a = new Foo >> 1;
+                ',
+            ),
+            array(
+                '<?php
+                    $a = new Foo() and 1;
+                    $a = new Foo() or 1;
+                    $a = new Foo() xor 1;
+                    $a = new Foo() && 1;
+                    $a = new Foo() || 1;
+                ',
+                '<?php
+                    $a = new Foo and 1;
+                    $a = new Foo or 1;
+                    $a = new Foo xor 1;
+                    $a = new Foo && 1;
+                    $a = new Foo || 1;
+                ',
+            ),
+            array(
+                '<?php
+                    if (new DateTime() > $this->startDate) {}
+                    if (new DateTime() >= $this->startDate) {}
+                    if (new DateTime() < $this->startDate) {}
+                    if (new DateTime() <= $this->startDate) {}
+                    if (new DateTime() == $this->startDate) {}
+                    if (new DateTime() != $this->startDate) {}
+                    if (new DateTime() <> $this->startDate) {}
+                    if (new DateTime() === $this->startDate) {}
+                    if (new DateTime() !== $this->startDate) {}
+                ',
+                '<?php
+                    if (new DateTime > $this->startDate) {}
+                    if (new DateTime >= $this->startDate) {}
+                    if (new DateTime < $this->startDate) {}
+                    if (new DateTime <= $this->startDate) {}
+                    if (new DateTime == $this->startDate) {}
+                    if (new DateTime != $this->startDate) {}
+                    if (new DateTime <> $this->startDate) {}
+                    if (new DateTime === $this->startDate) {}
+                    if (new DateTime !== $this->startDate) {}
+                ',
+            ),
+            array(
+                '<?php $a = new \stdClass() ? $b : $c;',
+                '<?php $a = new \stdClass ? $b : $c;',
+            ),
+            array(
+                '<?php foreach (new Collection() as $x) {}',
+                '<?php foreach (new Collection as $x) {}',
+            ),
+            array(
+                '<?php $a = [(string) new Foo() => 1];',
+                '<?php $a = [(string) new Foo => 1];',
+            ),
         );
     }
 
@@ -142,4 +258,32 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
             ),
         );
     }
+
+    public function provide56Cases()
+    {
+        return array(
+            array(
+                '<?php
+                    $a = new Foo() ** 1;
+                ',
+                '<?php
+                    $a = new Foo ** 1;
+                ',
+            ),
+        );
+    }
+
+    public function provide70Cases()
+    {
+        return array(
+            array(
+                '<?php
+                    $a = new Foo() <=> 1;
+                ',
+                '<?php
+                    $a = new Foo <=> 1;
+                ',
+            ),
+        );
+    }
 }