Browse Source

bug #6064 SquareBraceTransformer - fix detect array destructing in foreach (SpacePossum)

This PR was merged into the master branch.

Discussion
----------

SquareBraceTransformer - fix detect array destructing in foreach

closes https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/6062

Commits
-------

b2bee6b03 SquareBraceTransformer - fix detect array destructing in foreach
SpacePossum 3 years ago
parent
commit
3968d73950

+ 4 - 0
src/Tokenizer/Transformer/SquareBraceTransformer.php

@@ -172,6 +172,10 @@ final class SquareBraceTransformer extends AbstractTransformer
             return false;
         }
 
+        if ($prevToken->isGivenKind(T_AS)) {
+            return true;
+        }
+
         $type = Tokens::detectBlockType($tokens[$index]);
         $end = $tokens->findBlockEnd($type['type'], $index);
 

+ 23 - 17
tests/Fixer/ListNotation/ListSyntaxFixerTest.php

@@ -42,27 +42,22 @@ final class ListSyntaxFixerTest extends AbstractFixerTestCase
         $this->doTest($expected, $input);
     }
 
-    /**
-     * @dataProvider provideFixToShortSyntaxCases
-     */
-    public function testFixToShortSyntax(string $expected, ?string $input = null): void
-    {
-        $this->fixer->configure(['syntax' => 'short']);
-        $this->doTest($expected, $input);
-    }
-
-    public function provideFixToLongSyntaxCases(): array
+    public function provideFixToLongSyntaxCases(): \Generator
     {
         // reverse testing
         $shortCases = $this->provideFixToShortSyntaxCases();
-        $cases = [];
+
         foreach ($shortCases as $label => $shortCase) {
-            $cases[$label] = [$shortCase[1], $shortCase[0]];
+            if ('messy comments case' === $label) {
+                continue;
+            }
+
+            yield $label => [$shortCase[1], $shortCase[0]];
         }
 
         // the reverse of this is different because of all the comments and white space,
         // therefore we override with a similar case here
-        $cases['comment case'] = [
+        yield 'comment case' => [
             '<?php
 #
 list(#
@@ -81,7 +76,7 @@ $a#
 ;#',
         ];
 
-        $cases[] = ['<?php
+        yield ['<?php
 
 class Test
 {
@@ -92,9 +87,16 @@ class Test
 }',
         ];
 
-        $cases[] = ['<?php [$b[$a]] = $foo();'];
+        yield ['<?php [$b[$a]] = $foo();'];
+    }
 
-        return $cases;
+    /**
+     * @dataProvider provideFixToShortSyntaxCases
+     */
+    public function testFixToShortSyntax(string $expected, ?string $input = null): void
+    {
+        $this->fixer->configure(['syntax' => 'short']);
+        $this->doTest($expected, $input);
     }
 
     public function provideFixToShortSyntaxCases(): array
@@ -122,7 +124,7 @@ class Test
 list(//
     $x) =/**/$a?>',
             ],
-            'comment case' => [
+            'messy comments case' => [
                 '<?php
 #a
 #g
@@ -168,6 +170,10 @@ $a;#
                 '<?php [[$a]] = $foo();',
                 '<?php list(list($a)) = $foo();',
             ],
+            [
+                '<?php foreach ($z as [$a, $b]) {}',
+                '<?php foreach ($z as list($a, $b)) {}',
+            ],
         ];
     }
 

+ 7 - 0
tests/Tokenizer/Transformer/SquareBraceTransformerTest.php

@@ -318,6 +318,13 @@ class Test
                     25 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
                 ],
             ],
+            [
+                '<?php foreach ($z as [$a, $b]) {}',
+                [
+                    8 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
+                    13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
+                ],
+            ],
         ];
     }