Find the namespace in the tokens starting at a given key.

@param int   $key
@param array $tokens

@return null|string
17918
private static function getNamespace(int $key, array $tokens): ?string
    {
        $namespace  = null;
        $tokenCount = \count($tokens);

        for ($i = $key; $i < $tokenCount; $i++) {
            if (self::isPartOfNamespace($tokens[$i])) {
                $namespace .= $tokens[$i][1];
            } elseif ($tokens[$i] === ';') {
                return $namespace;
            }
        }

        return null;
    }
712975
private function parseNamespace($tokens)
    {
        $namespace = null;
        $tokenCount = count($tokens);
        for ($offset=0; $offset < $tokenCount; $offset++) {
            if (!is_array($tokens[$offset])) {
                continue;
            }

            if (T_NAMESPACE === $tokens[$offset][0]) {
                $offset++; // the next token is a whitespace

                return $this->parseNamespaceString($tokens, $offset);
            }
        }

        return $namespace;
    }
105719
public function parseToken($key = 'access_token')
    {
        if ( $token = $this->getToken() )
        {
            $token_array = json_decode($token, true);

            return $token_array[$key];
        }
    }
207823
private function getClassNamesFromTokens(array $tokens) : array
    {
        for ($i = 0;$i < count($tokens);$i++) {
            // Skip literals
            if (is_string($tokens[$i])) {
                continue;
            }

            $className = '';

            switch ($tokens[$i][0]) {
                case T_NAMESPACE:
                    $namespace = '';

                    // Collect all the namespace parts and separators
                    while (isset($tokens[++$i][1])) {
                        if (in_array($tokens[$i][0], [T_NS_SEPARATOR, T_STRING])) {
                            $namespace .= $tokens[$i][1];
                        }
                    }

                    break;
                case T_CLASS:
                    $isClassConstant = false;

                    // Scan previous tokens to see if they're double colons, which would mean this is a class constant
                    for ($j = $i - 1;$j >= 0;$j--) {
                        if (!isset($tokens[$j][1])) {
                            break;
                        }

                        if ($tokens[$j][0] === T_DOUBLE_COLON) {
                            $isClassConstant = true;
                            break 2;
                        } elseif ($tokens[$j][0] === T_WHITESPACE) {
                            // Since we found whitespace, then we know this isn't a class constant
                            break;
                        }
                    }

                    // Get the class name
                    while (isset($tokens[++$i][1])) {
                        if ($tokens[$i][0] === T_STRING) {
                            $className .= $tokens[$i][1];
                            break;
                        }
                    }

                    $classNames[] = ltrim($namespace . '\\' . $className, '\\');
                    break 2;
            }
        }

        return $classNames;
    }
1164306
protected function isShortArraySyntax(array $tokens, Node\Expr\Array_ $array)
    {
        $i = $array->getAttribute('startTokenPos');
        return is_string($tokens[$i]);
    }
118276
function findKey( $key )
    {
        $msg = null;
        if ( isset( $this->Messages[$key] ) )
        {
            $msg = $this->Messages[$key];
        }
        return $msg;
    }
794867
public static function create(array $tokens) {
        HashMap::assert($tokens, 'tokens');

        if (empty($tokens['ORDER'])) return [];

        $headers = [];
        $orderQueryArr = array_map(function($order){
            $query = end($order['no_quotes']['parts']);
            if (empty($query)) return null;
            return !isset($order['direction']) ? $query : $query . ' ' . $order['direction'];
        },$tokens['ORDER']);
        array_push($headers, 'Order: '.implode(',',$orderQueryArr));
        return $headers;
    }
348802
private function parseRaw($key)
    {
        $key = str_replace('[]', '', $key);
        parse_str($key, $levels);
        if (is_null($levels)) {
            return null;
        }
        $inputs = $this->data;
        return $this->recurseGet($levels, $inputs);
    }
51847
public function until($tokens)
    {
        $tokens = (array)$tokens;

        if(empty($tokens)) {
            throw new \InvalidArgumentException('$tokens is not specified.');
        }

        $startAt = $this->pos;

        $isEscaped = false;
        $inQuote   = false;
        while($this->pos < $this->len) {
            $isReached = false;

            if(!$isEscaped && $this->isNextToken(Tokens::T_ESCAPE, false)) {
                // if escape symbol is given then flag escape, and go next
                $isEscaped = true;
                $this->pos++;
                continue;
            }

            if(!$inQuote) {
                foreach($tokens as $token) {
                    if($this->isNextToken($token, false)) {
                        $isReached = true;
                        break;
                    }
                }

                if($isReached && !$isEscaped) {
                    break;
                }
            }

            if(!$isEscaped && $this->isNextToken(Tokens::T_QUOTE, false)) {
                $inQuote = !$inQuote;
            }

            $isEscaped = false;
            $this->pos++;
        }

        if($this->pos > $this->len) {
            throw new \InvalidArgumentException('EOL reached.');
        }


        return substr($this->value, $startAt, $this->pos - $startAt);
    }
608547
protected function filterTokens(array $tokens)
    {
        $filteredTokens = array_filter($tokens, function (Token $t) {
            return $t->getType() !== TokenType::Whitespace;
        });

        // Return the array values only, because array_filter preserves the keys
        return array_values($filteredTokens);
    }
285381
private function parseUseStatement(\ArrayIterator $tokens)
    {
        $uses = [];
        $continue = true;

        while ($continue) {
            $this->skipToNextStringOrNamespaceSeparator($tokens);

            list($alias, $fqnn) = $this->extractUseStatement($tokens);
            $uses[$alias] = $fqnn;
            if (self::T_LITERAL_END_OF_USE === $tokens->current()[0]) {
                $continue = false;
            }
        }

        return $uses;
    }
525298
private static function getNthTokenWithoutWhitespace($tokens, $i) {
		$j = 0;
		foreach ($tokens as $token) {
			if ($token['token'] == 'T_WHITESPACE') {
				continue;
			}
			if ($i == $j) {
				return $token;
			}
			$j++;
		}
		return null;
	}
123532
private function extractNamespaces(array $tokens)
    {
        $closureNamespace = null;
        $closureUses = [];
        $state = null;
        $namespace = null;
        $alias = null;
        foreach ($tokens as $token) {
            if (is_array($token)) {
                if ($state === null && ($token[0] === T_NAMESPACE || $token[0] === T_USE)) {
                    $state = $token[0];
                    $namespace = '';
                } elseif ($state === T_USE && $alias === null && $token[0] === T_AS) {
                    $alias = '';
                } elseif ($state !== null && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
                    if ($alias !== null) {
                        $alias .= $token[1];
                    } else {
                        $namespace .= $token[1];
                    }
                }
            } else {
                if ($state !== null && ($token === ';' || $token === '{')) {
                    if ($alias === null) {
                        if ($state === T_NAMESPACE) {
                            $closureNamespace = $namespace;
                            $closureUses = [];
                        } else {
                            $closureUses[] = $namespace;
                        }
                    } else {
                        $closureUses[$alias] = $namespace;
                        $alias = null;
                    }
                    $state = null;
                    $namespace = null;
                } elseif ($state === T_USE && $token === ',') {
                    if ($alias === null) {
                        $closureUses[] = $namespace;
                    } else {
                        $closureUses[$alias] = $namespace;
                        $alias = null;
                    }
                    $namespace = null;
                }
            }
        }
        return [
            'namespace' => $closureNamespace,
            'uses' => $closureUses,
        ];
    }
997951
private function normalizeTokens(array $tokens)
    {
        $nTokens = array();
        for ($i=0,$c=count($tokens); $i<$c; $i++) {
            $token = $tokens[$i];

            if (is_string($token)) {
                $nTokens[] = new LiteralToken($token, end($nTokens)->getEndLine());
                continue;
            }

            switch ($token[0]) {
                case T_WHITESPACE:
                    $lines = explode("\n", $token[1]);
                    for ($j=0,$k=count($lines); $j<$k; $j++) {
                        $line = $lines[$j].($j+1 === $k ? '' : "\n");

                        if ($j+1 === $k && '' === $line) {
                            break;
                        }

                        $nTokens[] = new PhpToken(array(T_WHITESPACE, $line, $token[2] + $j));
                    }
                    break;

                default:
                    // remove any trailing whitespace of the token
                    if (preg_match('/^(.*?)(\s+)$/', $token[1], $match)) {
                        $nTokens[] = new PhpToken(array($token[0], $match[1], $token[2]));

                        // if the next token is whitespace, change it
                        if (isset($tokens[$i+1]) && !is_string($tokens[$i+1])
                                && T_WHITESPACE === $tokens[$i+1][0]) {
                            $tokens[$i+1][1] = $match[2].$tokens[$i+1][1];
                            $tokens[$i+1][2] = $token[2];
                        } else {
                            $nTokens[] = new PhpToken(array(T_WHITESPACE, $match[2], $token[2]));
                        }
                    } else {
                        $nTokens[] = new PhpToken($token);
                    }
            }
        }

        return $nTokens;
    }
1314940
protected function getNamespaceAndClass($tokens)
    {
        $class = '';
        while (self::hasToken(
            [self::T_NS_SEPARATOR, self::T_STRING],
            $token = \array_pop($tokens)
        )) {
            if (self::needCompleteClass($token)) {
                continue;
            }

            $class = $token[1] . $class;
        }

        return $class;
    }
672553
protected function parseTokensContent(array $tokens): string
    {
        $statement = '';
        foreach ($tokens as $token) {
            $statement .= $token['content'];
        }

        return $statement;
    }
1271414
protected static function arrayGet(array $array, $key)
    {
        $key = strtolower($key);
        $array = array_change_key_case($array, CASE_LOWER);

        if (!array_key_exists($key, $array)) {
            throw new TokenException(
                sprintf('Did not find key %s in array: %s', json_encode($key), json_encode($array))
            );
        }

        return $array[$key];
    }