You are here

protected function PHP_Token_Stream::parse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpunit/php-token-stream/src/Token/Stream.php \PHP_Token_Stream::parse()
5 calls to PHP_Token_Stream::parse()
PHP_Token_Stream::getClasses in vendor/phpunit/php-token-stream/src/Token/Stream.php
PHP_Token_Stream::getFunctionForLine in vendor/phpunit/php-token-stream/src/Token/Stream.php
Returns the name of the function or method a line belongs to.
PHP_Token_Stream::getFunctions in vendor/phpunit/php-token-stream/src/Token/Stream.php
PHP_Token_Stream::getInterfaces in vendor/phpunit/php-token-stream/src/Token/Stream.php
PHP_Token_Stream::getTraits in vendor/phpunit/php-token-stream/src/Token/Stream.php
@since Method available since Release 1.1.0

File

vendor/phpunit/php-token-stream/src/Token/Stream.php, line 349

Class

PHP_Token_Stream
A stream of PHP tokens.

Code

protected function parse() {
  $this->interfaces = array();
  $this->classes = array();
  $this->traits = array();
  $this->functions = array();
  $class = array();
  $classEndLine = array();
  $trait = false;
  $traitEndLine = false;
  $interface = false;
  $interfaceEndLine = false;
  foreach ($this->tokens as $token) {
    switch (get_class($token)) {
      case 'PHP_Token_HALT_COMPILER':
        return;
      case 'PHP_Token_INTERFACE':
        $interface = $token
          ->getName();
        $interfaceEndLine = $token
          ->getEndLine();
        $this->interfaces[$interface] = array(
          'methods' => array(),
          'parent' => $token
            ->getParent(),
          'keywords' => $token
            ->getKeywords(),
          'docblock' => $token
            ->getDocblock(),
          'startLine' => $token
            ->getLine(),
          'endLine' => $interfaceEndLine,
          'package' => $token
            ->getPackage(),
          'file' => $this->filename,
        );
        break;
      case 'PHP_Token_CLASS':
      case 'PHP_Token_TRAIT':
        $tmp = array(
          'methods' => array(),
          'parent' => $token
            ->getParent(),
          'interfaces' => $token
            ->getInterfaces(),
          'keywords' => $token
            ->getKeywords(),
          'docblock' => $token
            ->getDocblock(),
          'startLine' => $token
            ->getLine(),
          'endLine' => $token
            ->getEndLine(),
          'package' => $token
            ->getPackage(),
          'file' => $this->filename,
        );
        if ($token instanceof PHP_Token_CLASS) {
          $class[] = $token
            ->getName();
          $classEndLine[] = $token
            ->getEndLine();
          if ($class[count($class) - 1] != 'anonymous class') {
            $this->classes[$class[count($class) - 1]] = $tmp;
          }
        }
        else {
          $trait = $token
            ->getName();
          $traitEndLine = $token
            ->getEndLine();
          $this->traits[$trait] = $tmp;
        }
        break;
      case 'PHP_Token_FUNCTION':
        $name = $token
          ->getName();
        $tmp = array(
          'docblock' => $token
            ->getDocblock(),
          'keywords' => $token
            ->getKeywords(),
          'visibility' => $token
            ->getVisibility(),
          'signature' => $token
            ->getSignature(),
          'startLine' => $token
            ->getLine(),
          'endLine' => $token
            ->getEndLine(),
          'ccn' => $token
            ->getCCN(),
          'file' => $this->filename,
        );
        if (empty($class) && $trait === false && $interface === false) {
          $this->functions[$name] = $tmp;
          $this
            ->addFunctionToMap($name, $tmp['startLine'], $tmp['endLine']);
        }
        elseif (!empty($class) && $class[count($class) - 1] != 'anonymous class') {
          $this->classes[$class[count($class) - 1]]['methods'][$name] = $tmp;
          $this
            ->addFunctionToMap($class[count($class) - 1] . '::' . $name, $tmp['startLine'], $tmp['endLine']);
        }
        elseif ($trait !== false) {
          $this->traits[$trait]['methods'][$name] = $tmp;
          $this
            ->addFunctionToMap($trait . '::' . $name, $tmp['startLine'], $tmp['endLine']);
        }
        else {
          $this->interfaces[$interface]['methods'][$name] = $tmp;
        }
        break;
      case 'PHP_Token_CLOSE_CURLY':
        if (!empty($classEndLine) && $classEndLine[count($classEndLine) - 1] == $token
          ->getLine()) {
          array_pop($classEndLine);
          array_pop($class);
        }
        elseif ($traitEndLine !== false && $traitEndLine == $token
          ->getLine()) {
          $trait = false;
          $traitEndLine = false;
        }
        elseif ($interfaceEndLine !== false && $interfaceEndLine == $token
          ->getLine()) {
          $interface = false;
          $interfaceEndLine = false;
        }
        break;
    }
  }
}