@@ -1814,38 +1814,80 @@ public function add_unslash_error( $stackPtr ) {
18141814 );
18151815 }
18161816
1817+ /**
1818+ * Get the index keys of an array variable.
1819+ *
1820+ * E.g., "bar" and "baz" in $foo['bar']['baz'].
1821+ *
1822+ * @since 2.1.0
1823+ *
1824+ * @param int $stackPtr The index of the variable token in the stack.
1825+ * @param bool $all Whether to get all keys or only the first.
1826+ * Defaults to `true`(= all).
1827+ *
1828+ * @return array An array of index keys whose value is being accessed.
1829+ * or an empty array if this is not array access.
1830+ */
1831+ protected function get_array_access_keys ( $ stackPtr , $ all = true ) {
1832+
1833+ $ keys = array ();
1834+
1835+ if ( \T_VARIABLE !== $ this ->tokens [ $ stackPtr ]['code ' ] ) {
1836+ return $ keys ;
1837+ }
1838+
1839+ $ current = $ stackPtr ;
1840+
1841+ do {
1842+ // Find the next non-empty token.
1843+ $ open_bracket = $ this ->phpcsFile ->findNext (
1844+ Tokens::$ emptyTokens ,
1845+ ( $ current + 1 ),
1846+ null ,
1847+ true
1848+ );
1849+
1850+ // If it isn't a bracket, this isn't an array-access.
1851+ if ( false === $ open_bracket
1852+ || \T_OPEN_SQUARE_BRACKET !== $ this ->tokens [ $ open_bracket ]['code ' ]
1853+ || ! isset ( $ this ->tokens [ $ open_bracket ]['bracket_closer ' ] )
1854+ ) {
1855+ break ;
1856+ }
1857+
1858+ $ key = $ this ->phpcsFile ->getTokensAsString (
1859+ ( $ open_bracket + 1 ),
1860+ ( $ this ->tokens [ $ open_bracket ]['bracket_closer ' ] - $ open_bracket - 1 )
1861+ );
1862+
1863+ $ keys [] = trim ( $ key );
1864+ $ current = $ this ->tokens [ $ open_bracket ]['bracket_closer ' ];
1865+ } while ( isset ( $ this ->tokens [ $ current ] ) && true === $ all );
1866+
1867+ return $ keys ;
1868+ }
1869+
18171870 /**
18181871 * Get the index key of an array variable.
18191872 *
18201873 * E.g., "bar" in $foo['bar'].
18211874 *
18221875 * @since 0.5.0
1876+ * @since 2.1.0 Now uses get_array_access_keys() under the hood.
18231877 *
18241878 * @param int $stackPtr The index of the token in the stack.
18251879 *
18261880 * @return string|false The array index key whose value is being accessed.
18271881 */
18281882 protected function get_array_access_key ( $ stackPtr ) {
18291883
1830- // Find the next non-empty token.
1831- $ open_bracket = $ this ->phpcsFile ->findNext (
1832- Tokens::$ emptyTokens ,
1833- ( $ stackPtr + 1 ),
1834- null ,
1835- true
1836- );
1884+ $ keys = $ this ->get_array_access_keys ( $ stackPtr , false );
18371885
1838- // If it isn't a bracket, this isn't an array-access.
1839- if ( false === $ open_bracket || \T_OPEN_SQUARE_BRACKET !== $ this ->tokens [ $ open_bracket ]['code ' ] ) {
1840- return false ;
1886+ if ( isset ( $ keys [0 ] ) ) {
1887+ return $ keys [0 ];
18411888 }
18421889
1843- $ key = $ this ->phpcsFile ->getTokensAsString (
1844- ( $ open_bracket + 1 ),
1845- ( $ this ->tokens [ $ open_bracket ]['bracket_closer ' ] - $ open_bracket - 1 )
1846- );
1847-
1848- return trim ( $ key );
1890+ return false ;
18491891 }
18501892
18511893 /**
0 commit comments