src/ETSGlobalCodingStandard/Helpers/ArrayHelper.php
/**
     * Finds the pointer to the given key string.
     *
     * @param int       $arrayPointer The pointer to the array opening bracket.
     * @param string    $key          The key to search for.
     */
Finds the pointer to the given key string.
@param int       $arrayPointer The pointer to the array opening bracket.
@param string    $key          The key to search for.
Finds the pointer to the given key string . @param int $arrayPointer The pointer to the array opening bracket . @param string $key The key to search for .
private static function findKey(File $phpcsFile, int $arrayPointer, string $key): ?int
    {
        $tokens = $phpcsFile->getTokens();
        $array = $tokens[$arrayPointer];
        $closeBracketPointer = $array['bracket_closer'];
        $currentPointer = $arrayPointer + 1;
        while ($currentPointer < $closeBracketPointer) {
            $nextDelimiterPointer = $phpcsFile->findEndOfStatement($currentPointer, [T_DOUBLE_ARROW]);
            if (!$nextDelimiterPointer) {
                return null;
            }
            $entryPointer = $phpcsFile->findNext([T_CONSTANT_ENCAPSED_STRING, T_VARIABLE], $currentPointer, $nextDelimiterPointer);
            if ($entryPointer === false) {
                $currentPointer = $nextDelimiterPointer + 1;
                continue;
            }
            $doubleArrowPointer = $phpcsFile->findNext([T_DOUBLE_ARROW], $entryPointer, $nextDelimiterPointer);
            if ($doubleArrowPointer === false) {
                $currentPointer = $nextDelimiterPointer + 1;
                continue;
            }
            if ($tokens[$entryPointer]['type'] === 'T_CONSTANT_ENCAPSED_STRING' && $tokens[$entryPointer]['content'] === sprintf('\'%s\'', $key)) {
                return $entryPointer;
            }
            if ($nextDelimiterPointer === $closeBracketPointer) {
                return null;
            }
            $currentPointer = $nextDelimiterPointer + 1;
        }
        return null;
    }
private static function findKey ( File $ phpcsFile , int $ arrayPointer , string $ key ) : ? int { $ tokens = $ phpcsFile -> getTokens ( ) ; $ array = $ tokens [ $ arrayPointer ] ; $ closeBracketPointer = $ array [ 'bracket_closer' ] ; $ currentPointer = $ arrayPointer + 1 ; while ( $ currentPointer < $ closeBracketPointer ) { $ nextDelimiterPointer = $ phpcsFile -> findEndOfStatement ( $ currentPointer , [ T_DOUBLE_ARROW ] ) ; if ( ! $ nextDelimiterPointer ) { return null ; } $ entryPointer = $ phpcsFile -> findNext ( [ T_CONSTANT_ENCAPSED_STRING , T_VARIABLE ] , $ currentPointer , $ nextDelimiterPointer ) ; if ( $ entryPointer === false ) { $ currentPointer = $ nextDelimiterPointer + 1 ; continue ; } $ doubleArrowPointer = $ phpcsFile -> findNext ( [ T_DOUBLE_ARROW ] , $ entryPointer , $ nextDelimiterPointer ) ; if ( $ doubleArrowPointer === false ) { $ currentPointer = $ nextDelimiterPointer + 1 ; continue ; } if ( $ tokens [ $ entryPointer ] [ 'type' ] === 'T_CONSTANT_ENCAPSED_STRING' && $ tokens [ $ entryPointer ] [ 'content' ] === sprintf ( '\'%s\'' , $ key ) ) { return $ entryPointer ; } if ( $ nextDelimiterPointer === $ closeBracketPointer ) { return null ; } $ currentPointer = $ nextDelimiterPointer + 1 ; } return null ; }
Finds the pointer to the given key string.
Finds the pointer to the given key string .
// Error // No key token candidate found. // Array entry doesn't not have the key => value form // Reached the end of the array