Browse Source

bug #4804 TokensAnalyzer::isUnarySuccessorOperator fix for array curly braces (SpacePossum)

This PR was merged into the 2.15 branch.

Discussion
----------

TokensAnalyzer::isUnarySuccessorOperator fix for array curly braces

Commits
-------

d53ad323 TokensAnalyzer::isUnarySuccessorOperator fix for array curly braces
SpacePossum 5 years ago
parent
commit
a4df7187af

+ 1 - 0
src/Fixer/Operator/IncrementStyleFixer.php

@@ -145,6 +145,7 @@ final class IncrementStyleFixer extends AbstractFixer implements ConfigurationDe
             '[',
             [CT::T_DYNAMIC_PROP_BRACE_OPEN],
             [CT::T_DYNAMIC_VAR_BRACE_OPEN],
+            [CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN],
             [T_NS_SEPARATOR],
             [T_STATIC],
             [T_STRING],

+ 1 - 0
src/Tokenizer/TokensAnalyzer.php

@@ -388,6 +388,7 @@ final class TokensAnalyzer
             ']',
             [T_STRING],
             [T_VARIABLE],
+            [CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE],
             [CT::T_DYNAMIC_PROP_BRACE_CLOSE],
             [CT::T_DYNAMIC_VAR_BRACE_CLOSE],
         ];

+ 44 - 0
tests/Fixer/ControlStructure/NoSuperfluousElseifFixerTest.php

@@ -235,4 +235,48 @@ if ($some) { return 1; } elseif ($a == 6){ $test = false; } //',
             ],
         ];
     }
+
+    /**
+     * @param string $expected
+     * @param string $input
+     *
+     * @dataProvider provideFix70Cases
+     * @requires PHP 7.0
+     */
+    public function testFix70($expected, $input)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    public function provideFix70Cases()
+    {
+        return [
+            [
+                '<?php
+
+                if ($foo) {
+                    return 1;
+                }
+                if ($bar) {
+                    return 2;
+                }
+                if ($baz) {
+                    throw new class extends Exception{};
+                } else {
+                    return 4;
+                }',
+                '<?php
+
+                if ($foo) {
+                    return 1;
+                } elseif ($bar) {
+                    return 2;
+                } else if ($baz) {
+                    throw new class extends Exception{};
+                } else {
+                    return 4;
+                }',
+            ],
+        ];
+    }
 }

+ 5 - 0
tests/Fixer/ControlStructure/NoUselessElseFixerTest.php

@@ -708,6 +708,11 @@ else?><?php echo 5;',
             'foreach($a as $b){throw new Exception($i);}',
         ];
 
+        if (\PHP_VERSION_ID >= 70000) {
+            $statements[] = 'throw new class extends Exception{};';
+            $statements[] = 'throw new class ($a, 9) extends Exception{ public function z($a, $b){ echo 7;} };';
+        }
+
         $ifTemplate = '<?php
             if ($a === false)
             {

+ 5 - 0
tests/Fixer/Operator/IncrementStyleFixerTest.php

@@ -114,6 +114,10 @@ final class IncrementStyleFixerTest extends AbstractFixerTestCase
                 '<?php ++$a[0];',
                 '<?php $a[0]++;',
             ],
+            [
+                '<?php ++$a{0};',
+                '<?php $a{0}++;',
+            ],
             [
                 '<?php ++$a[$b];',
                 '<?php $a[$b]++;',
@@ -138,6 +142,7 @@ final class IncrementStyleFixerTest extends AbstractFixerTestCase
             ['<?php foo($a, ++$b);'],
             ['<?php $a[++$b];'],
             ['<?php echo ++$a;'],
+            ['<?= ++$a;'],
 
             [
                 '<?php class Test {

+ 5 - 1
tests/Tokenizer/TokensAnalyzerTest.php

@@ -920,10 +920,14 @@ preg_replace_callback(
                 '<?php $foo->{"bar"}++;',
                 [6 => true],
             ],
-            [
+            'array access' => [
                 '<?php $a["foo"]++;',
                 [5 => true],
             ],
+            'array curly access' => [
+                '<?php $a{"foo"}++;',
+                [5 => true],
+            ],
         ];
     }