Extracts a namespace from tokenized output of token_get_all().

@param array $tokens

@return string
966725
protected function extractNamespace(array $tokens) : string {
    $namespace = '';
    $found = FALSE;
    foreach ($tokens as $token) {
      if (is_array($token) && $token[0] == T_NAMESPACE) {
        $found = TRUE;
      }
      if (is_array($token) && ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING)) {
        $namespace .= $token[1];
      }
      elseif ($found && $token == ';') {
        return $namespace;
      }
    }
    return $namespace;
  }
343434
private function parseUseStatement(\ArrayIterator $tokens)
    {
        $uses = [];
        $continue = true;

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

            $uses = array_merge($uses, $this->extractUseStatements($tokens));
            if ($tokens->current()[0] === self::T_LITERAL_END_OF_USE) {
                $continue = false;
            }
        }

        return $uses;
    }
1072698
protected function getNamespace($key, array $tokens)
    {
        $namespace = null;
        $tokenCount = count($tokens);
        for ($i = $key; $i < $tokenCount; $i++) {
            if ($this->isPartOfNamespace($tokens[$i])) {
                $namespace .= $tokens[$i][1];
            } elseif ($tokens[$i] == ';') {
                return $namespace;
            }
        }
    }
594549
protected function getNamedCapturesExpressions(array $tokens)
	{
		$exprs = [];
		foreach ($tokens as $token)
		{
			if ($token['type'] !== 'capturingSubpatternStart' || !isset($token['name']))
			{
				continue;
			}

			$expr = $token['content'];
			if (strpos($expr, '|') !== false)
			{
				$expr = '(?:' . $expr . ')';
			}

			$exprs[$token['name']] = $expr;
		}

		return $exprs;
	}
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;
    }
982737
protected function filterByNamespace($namespace)
    {
        return function ($file) use ($namespace) {
            $content = file_get_contents($file);
            $tokens = token_get_all($content);

            $count = count($tokens);
            $token_namespace = null;

            for ($i = 0; $i < $count; ++$i) {
                $token = $tokens[$i];

                if (is_array($token) && $token[0] === T_NAMESPACE) {
                    $token_namespace .= '\\';
                    while (++$i < $count) {
                        if ($tokens[$i] === ';') {
                            $namespace = trim($namespace);
                            break;
                        }
                        $token_namespace .= is_array($tokens[$i]) ? trim($tokens[$i][1]) : '';
                    }

                    if ($token_namespace !== $namespace) {
                        return false;
                    }

                    break;
                }
            }

            if (!isset($token_namespace)) {
                return false;
            }
        };
    }
1367179
private function cleanupGroupDelimiters(array &$tokens)
    {
        $indexes = $this->getUnmatchedGroupDelimiterIndexes($tokens);

        while (!empty($indexes)) {
            $lastIndex = array_pop($indexes);
            $token = $tokens[$lastIndex];
            unset($tokens[$lastIndex]);

            if ($token->type === Tokenizer::TOKEN_GROUP_BEGIN) {
                $this->addCorrection(
                    self::CORRECTION_UNMATCHED_GROUP_LEFT_DELIMITER_IGNORED,
                    $token
                );
            } else {
                $this->addCorrection(
                    self::CORRECTION_UNMATCHED_GROUP_RIGHT_DELIMITER_IGNORED,
                    $token
                );
            }
        }
    }
788897
private function parseNamespace(\ArrayIterator $tokens)
    {
        // skip to the first string or namespace separator
        $this->skipToNextStringOrNamespaceSeparator($tokens);

        $name = '';
        while ($tokens->valid() && ($tokens->current()[0] === T_STRING || $tokens->current()[0] === T_NS_SEPARATOR)
        ) {
            $name .= $tokens->current()[1];
            $tokens->next();
        }

        return $name;
    }
394907
public function parse(array $tokens = array())
    {
        $this->lineNum    = -1;
        $this->lineTokens = 0;
        $this->pragmas    = $this->defaultPragmas;

        $this->pragmaFilters = isset($this->pragmas[Mustache_Engine::PRAGMA_FILTERS]);
        $this->pragmaBlocks  = isset($this->pragmas[Mustache_Engine::PRAGMA_BLOCKS]);

        return $this->buildTree($tokens);
    }
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;
    }
123243
protected function extractNamespace($file)
    {
        $namespace = false;

        foreach ($file as $line) {
            if (preg_match('/^namespace (?P<namespace>.*);$/i', $line, $matches)) {
                $namespace = $matches['namespace'];
                continue;
            }
        }
        return $namespace;
    }
835597
public static function tokenHasExtraWhiteSpaceLeft($token)
    {
        return
            $token[Tokenizer::TOKEN_VALUE] === '.'
            || $token[Tokenizer::TOKEN_VALUE] === ','
            || $token[Tokenizer::TOKEN_VALUE] === ';';
    }
392297
private function collectUsedNamespace(array $tokens, $stackPointer)
    {
        $namespace = '';
        $next      = 2;
        do
        {
            $type = $tokens[$stackPointer + $next]['type'];
            if ($type === 'T_STRING' || $type === 'T_NS_SEPARATOR')
            {
                $namespace .= $tokens[$stackPointer + $next]['content'];
            }
            $next++;
        } while ($tokens[$stackPointer]['line'] === $tokens[$stackPointer + $next]['line']);

        if ($namespace === '')
        {
            $type  = 'Use without namespace found.';
            $data  = [$tokens[$stackPointer]['content']];
            $error = 'Use without qualified namespace found.';
            $this->file->addWarning($error, $stackPointer, $type, $data);
        }
        else
        {
            $this->usePaths[] = trim($namespace);
        }
    }
818234
public static function tokenHasExtraWhiteSpaces($token)
    {
        return \strpos($token[Tokenizer::TOKEN_VALUE], ' ') !== false
        || \strpos($token[Tokenizer::TOKEN_VALUE], "\n") !== false
        || \strpos($token[Tokenizer::TOKEN_VALUE], "\t") !== false;
    }
835396
public function parse(array $tokens)
    {
        $symbolNodes = $this->detectSymbols($tokens);

        $nodes = $this->createTreeByBrackets($symbolNodes);

        $nodes = $this->transformTreeByFunctions($nodes);

        $this->checkGrammar($nodes);

        // Wrap the nodes in an array node.
        $rootNode = new ContainerNode($nodes);

        return $rootNode;
    }
1055562
public function _getProgramElementName(&$tokens, $key)
    {
        $name = '';
        $key++;
        while (
            $tokens[$key] && (
                $tokens[$key] == '&' || (
                    isset($tokens[$key][0]) && isset($tokens[$key][1]) && (
                    $tokens[$key][0] == T_WHITESPACE ||
                    $tokens[$key][0] == T_STRING ||
                    $tokens[$key][0] == T_NS_SEPARATOR
                )
            )
        )) {
            if (isset($tokens[$key][1])) {
                $name .= $tokens[$key][1];
            }
            $key++;
        }

        return trim($name);
    }
474546
public function getNamespace($namespace)
    {
        $classes = [];
        $definitions = $this->classes->extract();

        foreach (array_keys($definitions) as $class) {
            if (strpos($class, $namespace, 0) === 0) {
                $classes[$class] = $this->get($class);
            }
        }

        return $classes;
    }