Skip to content

Commit

Permalink
Support single-line multi-value arrays in PHP array-destructing (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
windaishi authored Jan 22, 2024
1 parent 5957553 commit e41c5c8
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,15 @@ public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arra
$nestedParenthesis = array_pop($nested);
}

if ($nestedParenthesis === false
|| $tokens[$nestedParenthesis]['line'] !== $tokens[$stackPtr]['line']
// Decide whether this array is used for array destructing ( [$a, $b] = foo() )
$nextTokenThatIsNotEqual = $phpcsFile->findNext([T_EQUAL, T_WHITESPACE], $arrayEnd + 1, null, true);
$isArrayDestructing = $phpcsFile->findNext(T_EQUAL, $arrayEnd + 1, $nextTokenThatIsNotEqual) !== false;

if (!$isArrayDestructing
&& (
$nestedParenthesis === false
|| $tokens[$nestedParenthesis]['line'] !== $tokens[$stackPtr]['line']
)
) {
$error = 'Array with multiple values cannot be declared on a single line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SingleLineNotAllowed');
Expand All @@ -243,7 +250,7 @@ public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arra
return;
}

// We have a multiple value array that is inside a condition or
// We have a multiple value array that is an array destructing or inside a condition or
// function. Check its spacing is correct.
foreach ($commas as $comma) {
if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
Expand Down

0 comments on commit e41c5c8

Please sign in to comment.