Skip to content

Commit c6716a9

Browse files
authored
Only check one parent for recursive list assignment (sirbrillig#218)
1 parent 5048655 commit c6716a9

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
583583
public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
584584
$tokens = $phpcsFile->getTokens();
585585
self::debug('getListAssignments', $listOpenerIndex, $tokens[$listOpenerIndex]);
586+
587+
// First find the end of the list
586588
$closePtr = null;
587589
if (isset($tokens[$listOpenerIndex]['parenthesis_closer'])) {
588590
$closePtr = $tokens[$listOpenerIndex]['parenthesis_closer'];
@@ -594,20 +596,31 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
594596
return null;
595597
}
596598

599+
// Find the assignment (equals sign) which, if this is a list assignment, should be the next non-space token
597600
$assignPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $closePtr + 1, null, true);
601+
602+
// If the next token isn't an assignment, check for nested brackets because we might be a nested assignment
598603
if (! is_int($assignPtr) || $tokens[$assignPtr]['code'] !== T_EQUAL) {
599-
// If we are nested inside a destructured assignment, we are also an assignment
604+
// Collect the enclosing list open/close tokens ($parents is an assoc array keyed by opener index and the value is the closer index)
600605
$parents = isset($tokens[$listOpenerIndex]['nested_parenthesis']) ? $tokens[$listOpenerIndex]['nested_parenthesis'] : [];
601606
// There's no record of nested brackets for short lists; we'll have to find the parent ourselves
602-
$parentSquareBracket = Helpers::findContainingOpeningSquareBracket($phpcsFile, $listOpenerIndex);
603-
if (is_int($parentSquareBracket)) {
604-
$parents[$parentSquareBracket] = 0; // We don't actually need the closing paren
607+
if (empty($parents)) {
608+
$parentSquareBracket = Helpers::findContainingOpeningSquareBracket($phpcsFile, $listOpenerIndex);
609+
if (is_int($parentSquareBracket)) {
610+
// Collect the opening index, but we don't actually need the closing paren index so just make that 0
611+
$parents[$parentSquareBracket] = 0;
612+
}
605613
}
606-
$nestedAssignments = null;
607-
foreach (array_reverse($parents, true) as $openParen => $closeParen) {
608-
$nestedAssignments = self::getListAssignments($phpcsFile, $openParen);
614+
// If we have no parents, this is not a nested assignment and therefore is not an assignment
615+
if (empty($parents)) {
616+
return null;
609617
}
610-
if ($nestedAssignments === null) {
618+
619+
// Recursively check to see if the parent is a list assignment (we only need to check one level due to the recursion)
620+
$isNestedAssignment = null;
621+
$parentListOpener = array_keys(array_reverse($parents, true))[0];
622+
$isNestedAssignment = self::getListAssignments($phpcsFile, $parentListOpener);
623+
if ($isNestedAssignment === null) {
611624
return null;
612625
}
613626
}

0 commit comments

Comments
 (0)