final class PhpParser

Parses a file for namespaces/use/class declarations.

@author Fabien Potencier <fabien@symfony.com> @author Christian Kaps <christian.kaps@mohiva.com>

Hierarchy

  • class \Doctrine\Common\Annotations\PhpParser

Expanded class hierarchy of PhpParser

2 files declare their use of PhpParser
PerformanceTest.php in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Annotations/PerformanceTest.php
PhpParserTest.php in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/Annotations/PhpParserTest.php

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Annotations/PhpParser.php, line 30

Namespace

Doctrine\Common\Annotations
View source
final class PhpParser {

  /**
   * Parses a class.
   *
   * @param  \ReflectionClass $class A <code>ReflectionClass</code> object.
   * @return array            A list with use statements in the form (Alias => FQN).
   */
  public function parseClass(\ReflectionClass $class) {
    if (method_exists($class, 'getUseStatements')) {
      return $class
        ->getUseStatements();
    }
    if (false === ($filename = $class
      ->getFilename())) {
      return array();
    }
    $content = $this
      ->getFileContent($filename, $class
      ->getStartLine());
    $namespace = str_replace('\\', '\\\\', $class
      ->getNamespaceName());
    $content = preg_replace('/^.*?(\\bnamespace\\s+' . $namespace . '\\s*[;{].*)$/s', '\\1', $content);
    $tokenizer = new TokenParser('<?php ' . $content);
    $statements = $tokenizer
      ->parseUseStatements($class
      ->getNamespaceName());
    return $statements;
  }

  /**
   * Get the content of the file right up to the given line number.
   *
   * @param  string $filename   The name of the file to load.
   * @param  int    $lineNumber The number of lines to read from file.
   * @return string The content of the file.
   */
  private function getFileContent($filename, $lineNumber) {
    $content = '';
    $lineCnt = 0;
    $file = new SplFileObject($filename);
    while (!$file
      ->eof()) {
      if ($lineCnt++ == $lineNumber) {
        break;
      }
      $content .= $file
        ->fgets();
    }
    return $content;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpParser::getFileContent private function Get the content of the file right up to the given line number.
PhpParser::parseClass public function Parses a class.