@@ -37,15 +37,36 @@ public static function getIntOrNull($value)
3737 }
3838
3939 /**
40+ * Find the position of the square bracket containing the token at $stackPtr,
41+ * if any.
42+ *
4043 * @param File $phpcsFile
4144 * @param int $stackPtr
4245 *
4346 * @return ?int
4447 */
4548 public static function findContainingOpeningSquareBracket (File $ phpcsFile , $ stackPtr )
4649 {
50+ // Find the previous bracket within this same statement.
4751 $ previousStatementPtr = self ::getPreviousStatementPtr ($ phpcsFile , $ stackPtr );
48- return self ::getIntOrNull ($ phpcsFile ->findPrevious ([T_OPEN_SHORT_ARRAY , T_OPEN_SQUARE_BRACKET ], $ stackPtr - 1 , $ previousStatementPtr ));
52+ $ openBracketPosition = self ::getIntOrNull ($ phpcsFile ->findPrevious ([T_OPEN_SHORT_ARRAY , T_OPEN_SQUARE_BRACKET ], $ stackPtr - 1 , $ previousStatementPtr ));
53+ if (empty ($ openBracketPosition )) {
54+ return null ;
55+ }
56+ // Make sure we are inside the pair of brackets we found.
57+ $ tokens = $ phpcsFile ->getTokens ();
58+ $ openBracketToken = $ tokens [$ openBracketPosition ];
59+ if (empty ($ openBracketToken ) || empty ($ tokens [$ openBracketToken ['bracket_closer ' ]])) {
60+ return null ;
61+ }
62+ $ closeBracketPosition = $ openBracketToken ['bracket_closer ' ];
63+ if (empty ($ closeBracketPosition )) {
64+ return null ;
65+ }
66+ if ($ stackPtr > $ closeBracketPosition ) {
67+ return null ;
68+ }
69+ return $ openBracketPosition ;
4970 }
5071
5172 /**
@@ -835,10 +856,17 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex)
835856 $ parents = isset ($ tokens [$ listOpenerIndex ]['nested_parenthesis ' ]) ? $ tokens [$ listOpenerIndex ]['nested_parenthesis ' ] : [];
836857 // There's no record of nested brackets for short lists; we'll have to find the parent ourselves
837858 if (empty ($ parents )) {
838- $ parentSquareBracket = self ::findContainingOpeningSquareBracket ($ phpcsFile , $ listOpenerIndex );
839- if (is_int ($ parentSquareBracket )) {
840- // Collect the opening index, but we don't actually need the closing paren index so just make that 0
841- $ parents = [$ parentSquareBracket => 0 ];
859+ $ parentSquareBracketPtr = self ::findContainingOpeningSquareBracket ($ phpcsFile , $ listOpenerIndex );
860+ if (is_int ($ parentSquareBracketPtr )) {
861+ // Make sure that the parent is really a parent by checking that its
862+ // closing index is outside of the current bracket's closing index.
863+ $ parentSquareBracketToken = $ tokens [$ parentSquareBracketPtr ];
864+ $ parentSquareBracketClosePtr = $ parentSquareBracketToken ['bracket_closer ' ];
865+ if ($ parentSquareBracketClosePtr && $ parentSquareBracketClosePtr > $ closePtr ) {
866+ self ::debug ("found enclosing bracket for {$ listOpenerIndex }: {$ parentSquareBracketPtr }" );
867+ // Collect the opening index, but we don't actually need the closing paren index so just make that 0
868+ $ parents = [$ parentSquareBracketPtr => 0 ];
869+ }
842870 }
843871 }
844872 // If we have no parents, this is not a nested assignment and therefore is not an assignment
0 commit comments