supportsBelow('5.6') === false) { return; } $dereferencing = $this->isArrayStringDereferencing($phpcsFile, $stackPtr); if (empty($dereferencing)) { return; } $tokens = $phpcsFile->getTokens(); $supports54 = $this->supportsBelow('5.4'); foreach ($dereferencing['braces'] as $openBrace => $closeBrace) { if ($supports54 === true && ($tokens[$openBrace]['type'] === 'T_OPEN_SQUARE_BRACKET' || $tokens[$openBrace]['type'] === 'T_OPEN_SHORT_ARRAY') // Work around bug #1381 in PHPCS 2.8.1 and lower. ) { $phpcsFile->addError( 'Direct array dereferencing of %s is not present in PHP version 5.4 or earlier', $openBrace, 'Found', array($dereferencing['type']) ); continue; } // PHP 7.0 Array/string dereferencing using curly braces. if ($tokens[$openBrace]['type'] === 'T_OPEN_CURLY_BRACKET') { $phpcsFile->addError( 'Direct array dereferencing of %s using curly braces is not present in PHP version 5.6 or earlier', $openBrace, 'FoundUsingCurlies', array($dereferencing['type']) ); } } } /** * Check if this string/array is being dereferenced. * * @since 9.3.0 Logic split off from the process method. * * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return array Array containing the type of access and stack pointers to the * open/close braces involved in the array/string dereferencing; * or an empty array if no array/string dereferencing was detected. */ public function isArrayStringDereferencing(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); switch ($tokens[$stackPtr]['code']) { case \T_CONSTANT_ENCAPSED_STRING: $type = 'string literals'; $end = $stackPtr; break; case \T_ARRAY: if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { // Live coding. return array(); } else { $type = 'arrays'; $end = $tokens[$stackPtr]['parenthesis_closer']; } break; case \T_OPEN_SHORT_ARRAY: if (isset($tokens[$stackPtr]['bracket_closer']) === false) { // Live coding. return array(); } else { $type = 'arrays'; $end = $tokens[$stackPtr]['bracket_closer']; } break; } if (isset($type, $end) === false) { // Shouldn't happen, but for some reason did. return array(); } $braces = array(); do { $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true, null, true); if ($nextNonEmpty === false) { break; } if ($tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET' || $tokens[$nextNonEmpty]['type'] === 'T_OPEN_CURLY_BRACKET' // PHP 7.0+. || $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SHORT_ARRAY' // Work around bug #1381 in PHPCS 2.8.1 and lower. ) { if (isset($tokens[$nextNonEmpty]['bracket_closer']) === false) { // Live coding or parse error. break; } $braces[$nextNonEmpty] = $tokens[$nextNonEmpty]['bracket_closer']; // Continue, just in case there is nested array access, i.e. `array(1, 2, 3)[$i][$j];`. $end = $tokens[$nextNonEmpty]['bracket_closer']; continue; } // If we're still here, we've reached the end of the variable. break; } while (true); if (empty($braces)) { return array(); } return array( 'type' => $type, 'braces' => $braces, ); } }