From 585b50af5fb01bbf0c6dfc10cf9e3695574f6dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Mon, 20 Dec 2021 15:48:41 +0000 Subject: [PATCH] SquareBraceTransformer - fix for destructing square brace after double arrow --- .../Transformer/SquareBraceTransformer.php | 15 ++++++++- .../ArrayNotation/ArraySyntaxFixerTest.php | 2 ++ .../ListNotation/ListSyntaxFixerTest.php | 5 +++ .../SquareBraceTransformerTest.php | 32 +++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Tokenizer/Transformer/SquareBraceTransformer.php b/src/Tokenizer/Transformer/SquareBraceTransformer.php index 46874b1c853..c4ace5d9c2e 100644 --- a/src/Tokenizer/Transformer/SquareBraceTransformer.php +++ b/src/Tokenizer/Transformer/SquareBraceTransformer.php @@ -167,7 +167,8 @@ private function isArrayDestructing(Tokens $tokens, int $index): bool [CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE], ]; - $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)]; + $prevIndex = $tokens->getPrevMeaningfulToken($index); + $prevToken = $tokens[$prevIndex]; if ($prevToken->equalsAny($disallowedPrevTokens)) { return false; } @@ -176,6 +177,18 @@ private function isArrayDestructing(Tokens $tokens, int $index): bool return true; } + if ($prevToken->isGivenKind(T_DOUBLE_ARROW)) { + $variableIndex = $tokens->getPrevMeaningfulToken($prevIndex); + if (!$tokens[$variableIndex]->isGivenKind(T_VARIABLE)) { + return false; + } + + $prevVariableIndex = $tokens->getPrevMeaningfulToken($variableIndex); + if ($tokens[$prevVariableIndex]->isGivenKind(T_AS)) { + return true; + } + } + $type = Tokens::detectBlockType($tokens[$index]); $end = $tokens->findBlockEnd($type['type'], $index); diff --git a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php index 26f0b4750e0..4c220fb4e9d 100644 --- a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php +++ b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php @@ -77,6 +77,8 @@ public function provideLongSyntaxCases(): array [' [$x, $y]) {}'], ]; } diff --git a/tests/Fixer/ListNotation/ListSyntaxFixerTest.php b/tests/Fixer/ListNotation/ListSyntaxFixerTest.php index 41d97ffc200..8be10162abe 100644 --- a/tests/Fixer/ListNotation/ListSyntaxFixerTest.php +++ b/tests/Fixer/ListNotation/ListSyntaxFixerTest.php @@ -88,6 +88,11 @@ public function updateAttributeKey($key, $value) ]; yield [' list($x, $y) = getList()];', + ' [$x, $y] = getList()];', + ]; } /** diff --git a/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php b/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php index de2d34725fb..9c568b263c3 100644 --- a/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php +++ b/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php @@ -325,6 +325,38 @@ public function updateAttributeKey($key, $value) 13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, ], ], + [ + ' [$x, $y]) {}', + [ + 12 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, + 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, + ], + ], + [ + ' [$x, $y]];', + [ + 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN, + 6 => CT::T_ARRAY_SQUARE_BRACE_OPEN, + 11 => CT::T_ARRAY_SQUARE_BRACE_CLOSE, + 12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE, + ], + ], + [ + ' [$x, $y]);', + [ + 7 => CT::T_ARRAY_SQUARE_BRACE_OPEN, + 12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE, + ], + ], + [ + ' [$x, $y] = foo()];', + [ + 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN, + 6 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, + 11 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, + 18 => CT::T_ARRAY_SQUARE_BRACE_CLOSE, + ], + ], ]; }