Skip to content

Commit

Permalink
feat: Support for array destructuring in array_indentation (#7405)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Nov 4, 2023
1 parent 223d28a commit 204e6f4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Fixer/Whitespace/ArrayIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getDefinition(): FixerDefinitionInterface

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
return $tokens->isAnyTokenKindsFound([T_ARRAY, T_LIST, CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN]);
}

/**
Expand Down Expand Up @@ -70,13 +70,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

if (
$token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)
|| ($token->equals('(') && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
$token->isGivenKind([CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN])
|| ($token->equals('(') && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind([T_ARRAY, T_LIST]))
) {
$endIndex = $tokens->findBlockEnd(
$token->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE,
$index
);
$blockType = Tokens::detectBlockType($token);
$endIndex = $tokens->findBlockEnd($blockType['type'], $index);

$scopes[] = [
'type' => 'array',
Expand Down Expand Up @@ -175,7 +173,9 @@ private function findExpressionEndIndex(Tokens $tokens, int $index, int $parentS
for ($searchEndIndex = $index + 1; $searchEndIndex < $parentScopeEndIndex; ++$searchEndIndex) {
$searchEndToken = $tokens[$searchEndIndex];

if ($searchEndToken->equalsAny(['(', '{']) || $searchEndToken->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
if ($searchEndToken->equalsAny(['(', '{'])
|| $searchEndToken->isGivenKind([CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN])
) {
$type = Tokens::detectBlockType($searchEndToken);
$searchEndIndex = $tokens->findBlockEnd(
$type['type'],
Expand Down
88 changes: 86 additions & 2 deletions tests/Fixer/Whitespace/ArrayIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testFix(string $expected, ?string $input = null): void

public static function provideFixCases(): iterable
{
return self::withLongArraySyntaxCases([
yield from self::withLongArraySyntaxCases([
[
<<<'EXPECTED'
<?php
Expand Down Expand Up @@ -852,6 +852,48 @@ class="link"
,
],
]);

yield 'array destructuring' => [
<<<'EXPECTED'
<?php
[
$foo,
$bar,
$baz
] = $arr;
EXPECTED
,
<<<'INPUT'
<?php
[
$foo,
$bar,
$baz
] = $arr;
INPUT
,
];

yield 'array destructuring using list' => [
<<<'EXPECTED'
<?php
list(
$foo,
$bar,
$baz
) = $arr;
EXPECTED
,
<<<'INPUT'
<?php
list(
$foo,
$bar,
$baz
) = $arr;
INPUT
,
];
}

/**
Expand All @@ -865,7 +907,7 @@ public function testFixWithTabs(string $expected, ?string $input = null): void

public static function provideFixWithTabsCases(): iterable
{
return self::withLongArraySyntaxCases([
yield from self::withLongArraySyntaxCases([
[
<<<EXPECTED
<?php
Expand Down Expand Up @@ -903,6 +945,48 @@ public static function provideFixWithTabsCases(): iterable
,
],
]);

yield 'array destructuring' => [
<<<EXPECTED
<?php
[
\t\$foo,
\t\$bar,
\t\$baz
] = \$arr;
EXPECTED
,
<<<'INPUT'
<?php
[
$foo,
$bar,
$baz
] = $arr;
INPUT
,
];

yield 'array destructuring using list' => [
<<<EXPECTED
<?php
list(
\t\$foo,
\t\$bar,
\t\$baz
) = \$arr;
EXPECTED
,
<<<'INPUT'
<?php
list(
$foo,
$bar,
$baz
) = $arr;
INPUT
,
];
}

/**
Expand Down

0 comments on commit 204e6f4

Please sign in to comment.