Skip to content

Commit

Permalink
bug #6202 SquareBraceTransformer - fix for destructing square brace a…
Browse files Browse the repository at this point in the history
…fter double arrow (kubawerlos)

This PR was squashed before being merged into the master branch (closes #6202).

Discussion
----------

SquareBraceTransformer - fix for destructing square brace after double arrow

Commits
-------

585b50a SquareBraceTransformer - fix for destructing square brace after double arrow
  • Loading branch information
SpacePossum committed Dec 20, 2021
2 parents 80e7ca1 + 585b50a commit fe85ada
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
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
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
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
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 fe85ada

Please sign in to comment.