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

feat: Support for array destructuring in array_indentation #7405

Merged
Merged
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
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