Skip to content

Commit

Permalink
SquareBraceTransformer - fix for destructing square brace after doubl…
Browse files Browse the repository at this point in the history
…e arrow
  • Loading branch information
kubawerlos authored and SpacePossum committed Dec 20, 2021
1 parent 80e7ca1 commit 585b50a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Tokenizer/Transformer/SquareBraceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function provideLongSyntaxCases(): array
['<?php $x = func()[$x];'],
['<?php $x = "foo"[$x];'],
['<?php $text = "foo ${aaa[123]} bar $bbb[0] baz";'],
['<?php foreach ($array as [$x, $y]) {}'],
['<?php foreach ($array as $key => [$x, $y]) {}'],
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Fixer/ListNotation/ListSyntaxFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function updateAttributeKey($key, $value)
];

yield ['<?php [$b[$a]] = $foo();'];

yield [
'<?php [$iHaveList => list($x, $y) = getList()];',
'<?php [$iHaveList => [$x, $y] = getList()];',
];
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Tokenizer/Transformer/SquareBraceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,38 @@ public function updateAttributeKey($key, $value)
13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
],
[
'<?php foreach ($a as $key => [$x, $y]) {}',
[
12 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
],
[
'<?php [$key => [$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,
],
],
[
'<?php array($key => [$x, $y]);',
[
7 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
],
[
'<?php [$key => [$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,
],
],
];
}

Expand Down

0 comments on commit 585b50a

Please sign in to comment.