@@ -583,6 +583,8 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
583
583
public static function getListAssignments (File $ phpcsFile , $ listOpenerIndex ) {
584
584
$ tokens = $ phpcsFile ->getTokens ();
585
585
self ::debug ('getListAssignments ' , $ listOpenerIndex , $ tokens [$ listOpenerIndex ]);
586
+
587
+ // First find the end of the list
586
588
$ closePtr = null ;
587
589
if (isset ($ tokens [$ listOpenerIndex ]['parenthesis_closer ' ])) {
588
590
$ closePtr = $ tokens [$ listOpenerIndex ]['parenthesis_closer ' ];
@@ -594,20 +596,31 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
594
596
return null ;
595
597
}
596
598
599
+ // Find the assignment (equals sign) which, if this is a list assignment, should be the next non-space token
597
600
$ 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
598
603
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)
600
605
$ parents = isset ($ tokens [$ listOpenerIndex ]['nested_parenthesis ' ]) ? $ tokens [$ listOpenerIndex ]['nested_parenthesis ' ] : [];
601
606
// 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
+ }
605
613
}
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 ;
609
617
}
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 ) {
611
624
return null ;
612
625
}
613
626
}
0 commit comments