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
23 changes: 18 additions & 5 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,13 @@ 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 for PHP 7.4+ array unpacking within an array declaration.
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 Expand Up @@ -470,8 +474,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
true
);

$indices[] = ['value' => $valueContent];
$singleUsed = true;
$indices[] = ['value' => $valueContent];
$usesArrayUnpacking = $phpcsFile->findPrevious(
Tokens::$emptyTokens,
($nextToken - 2),
null,
true
);
if ($tokens[$usesArrayUnpacking]['code'] !== T_ELLIPSIS) {
// Don't decide if an array is key => value indexed or not when PHP 7.4+ array unpacking is used.
$singleUsed = true;
}
}//end if

$lastToken = $nextToken;
Expand Down
17 changes: 17 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,20 @@ $foo = [
$a = array(
'a' =>
);

// Ensure absence of NoKeySpecified error for PHP 7.4+ array unpacking.
edorian marked this conversation as resolved.
Show resolved Hide resolved
$x = array(
...$a,
'foo' => 'bar',
);

$x = array(
'foo' => 'bar',
...$a,
);

$x = array(
'foo' => 'bar',
...$a,
'baz' => 'bar',
);
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,20 @@ $foo = [
$a = array(
'a' =>
);

// Ensure absence of NoKeySpecified error for PHP 7.4+ array unpacking.
$x = array(
...$a,
'foo' => 'bar',
);

$x = array(
'foo' => 'bar',
...$a,
);

$x = array(
'foo' => 'bar',
...$a,
'baz' => 'bar',
);
17 changes: 17 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,20 @@ yield [
$a = [
'a' =>
];

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

$x = [
'foo' => 'bar',
...$a,
];

$x = [
'foo' => 'bar',
...$a,
'baz' => 'bar',
];
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,20 @@ yield [
$a = [
'a' =>
];

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

$x = [
'foo' => 'bar',
...$a,
];

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