3 namespace PhpDocReader\PhpParser;
8 * Parses a file for "use" declarations.
10 * Class taken and adapted from doctrine/annotations to avoid pulling the whole package.
12 * @author Fabien Potencier <fabien@symfony.com>
13 * @author Christian Kaps <christian.kaps@mohiva.com>
15 class UseStatementParser
18 * @return array A list with use statements in the form (Alias => FQN).
20 public function parseUseStatements(\ReflectionClass $class)
22 if (false === $filename = $class->getFilename()) {
26 $content = $this->getFileContent($filename, $class->getStartLine());
28 if (null === $content) {
32 $namespace = preg_quote($class->getNamespaceName());
33 $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
34 $tokenizer = new TokenParser('<?php ' . $content);
36 $statements = $tokenizer->parseUseStatements($class->getNamespaceName());
42 * Gets the content of the file right up to the given line number.
44 * @param string $filename The name of the file to load.
45 * @param integer $lineNumber The number of lines to read from file.
47 * @return string The content of the file.
49 private function getFileContent($filename, $lineNumber)
51 if ( ! is_file($filename)) {
57 $file = new SplFileObject($filename);
58 while (!$file->eof()) {
59 if ($lineCnt++ == $lineNumber) {
63 $content .= $file->fgets();