From bbcb1fc87a1d8c8e8d097d67c2a50edbaf47e9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Mon, 20 Dec 2021 16:48:41 +0100 Subject: [PATCH 1/3] Add tests --- .github/workflows/ci.yml | 2 +- .../ArrayNotation/ArraySyntaxFixerTest.php | 2 ++ .../ListNotation/ListSyntaxFixerTest.php | 5 +++ .../SquareBraceTransformerTest.php | 32 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac4642ad666..22a6fcb1a18 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -124,7 +124,7 @@ jobs: env: PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }} FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }} - run: vendor/bin/phpunit ${{ matrix.phpunit-flags }} + run: vendor/bin/phpunit --filter=iWillFail ${{ matrix.phpunit-flags }} - name: Upload coverage results to Coveralls if: matrix.calculate-code-coverage == 'yes' diff --git a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php index 26f0b4750e0..b4940c801cc 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..c1bc60ec091 100644 --- a/tests/Fixer/ListNotation/ListSyntaxFixerTest.php +++ b/tests/Fixer/ListNotation/ListSyntaxFixerTest.php @@ -88,6 +88,11 @@ public function updateAttributeKey($key, $value) ]; yield [' list(1, 2) = getList()];', + ' [1, 2] = getList()];', + ]; } /** diff --git a/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php b/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php index de2d34725fb..d2ce2fbe296 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, ], ], + 'iWillFail' => [ + ' [$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, + ], + ], ]; } From a53b68f1a49faf2470c6d1d2e605ab42e3b119a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Mon, 20 Dec 2021 16:52:53 +0100 Subject: [PATCH 2/3] Add fix --- .github/workflows/ci.yml | 2 +- .../Transformer/SquareBraceTransformer.php | 15 ++++++++++++++- .../Fixer/ArrayNotation/ArraySyntaxFixerTest.php | 2 +- .../Transformer/SquareBraceTransformerTest.php | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 22a6fcb1a18..ac4642ad666 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -124,7 +124,7 @@ jobs: env: PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }} FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }} - run: vendor/bin/phpunit --filter=iWillFail ${{ matrix.phpunit-flags }} + run: vendor/bin/phpunit ${{ matrix.phpunit-flags }} - name: Upload coverage results to Coveralls if: matrix.calculate-code-coverage == 'yes' 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 b4940c801cc..4c220fb4e9d 100644 --- a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php +++ b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php @@ -78,7 +78,7 @@ public function provideLongSyntaxCases(): array [' [' [$x, $y]) {}'], + [' [$x, $y]) {}'], ]; } diff --git a/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php b/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php index d2ce2fbe296..9c568b263c3 100644 --- a/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php +++ b/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php @@ -325,7 +325,7 @@ public function updateAttributeKey($key, $value) 13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, ], ], - 'iWillFail' => [ + [ ' [$x, $y]) {}', [ 12 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, From b7c315f61ad8599ee0b00bf62de3a131dd5a4b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Mon, 20 Dec 2021 17:12:51 +0100 Subject: [PATCH 3/3] Fix test --- tests/Fixer/ListNotation/ListSyntaxFixerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Fixer/ListNotation/ListSyntaxFixerTest.php b/tests/Fixer/ListNotation/ListSyntaxFixerTest.php index c1bc60ec091..8be10162abe 100644 --- a/tests/Fixer/ListNotation/ListSyntaxFixerTest.php +++ b/tests/Fixer/ListNotation/ListSyntaxFixerTest.php @@ -90,8 +90,8 @@ public function updateAttributeKey($key, $value) yield [' list(1, 2) = getList()];', - ' [1, 2] = getList()];', + ' list($x, $y) = getList()];', + ' [$x, $y] = getList()];', ]; }