Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SquareBraceTransformer - fix for destructing square brace after double arrow #6202

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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