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

Allow array unpacking in ArrayDeclaration multiline Sniff #3843

Merged
merged 10 commits into from
Jul 11, 2023
19 changes: 16 additions & 3 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,22 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}

if ($keyUsed === true && $tokens[$lastToken]['code'] === T_COMMA) {
$error = 'No key specified for array entry; first entry specifies key';
$phpcsFile->addError($error, $nextToken, 'NoKeySpecified');
return;
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($lastToken + 1), null, true);

/*
Allow array unpacking

$x = [
'foo' => 'bar',
...$baz,
];
*/

edorian marked this conversation as resolved.
Show resolved Hide resolved
if ($tokens[$nextToken]['code'] !== T_ELLIPSIS) {
$error = 'No key specified for array entry; first entry specifies key';
$phpcsFile->addError($error, $nextToken, 'NoKeySpecified');
return;
}
}

if ($keyUsed === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,9 @@ yield [
$a = [
'a' =>
];

// Ensure absence of NoKeySpecified error for array unpacking
edorian marked this conversation as resolved.
Show resolved Hide resolved
$x = [
'foo' => 'bar',
...$a,
];
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,9 @@ yield [
$a = [
'a' =>
];

// Ensure absence of NoKeySpecified error for array unpacking
$x = [
'foo' => 'bar',
...$a,
];