PhpParser.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Doctrine\Common\Annotations;
  3. use ReflectionClass;
  4. use ReflectionFunction;
  5. use SplFileObject;
  6. use function is_file;
  7. use function method_exists;
  8. use function preg_quote;
  9. use function preg_replace;
  10. /**
  11. * Parses a file for namespaces/use/class declarations.
  12. */
  13. final class PhpParser
  14. {
  15. /**
  16. * Parses a class.
  17. *
  18. * @deprecated use parseUseStatements instead
  19. *
  20. * @param ReflectionClass $class A <code>ReflectionClass</code> object.
  21. *
  22. * @return array<string, class-string> A list with use statements in the form (Alias => FQN).
  23. */
  24. public function parseClass(ReflectionClass $class)
  25. {
  26. return $this->parseUseStatements($class);
  27. }
  28. /**
  29. * Parse a class or function for use statements.
  30. *
  31. * @param ReflectionClass|ReflectionFunction $reflection
  32. *
  33. * @psalm-return array<string, string> a list with use statements in the form (Alias => FQN).
  34. */
  35. public function parseUseStatements($reflection): array
  36. {
  37. if (method_exists($reflection, 'getUseStatements')) {
  38. return $reflection->getUseStatements();
  39. }
  40. $filename = $reflection->getFileName();
  41. if ($filename === false) {
  42. return [];
  43. }
  44. $content = $this->getFileContent($filename, $reflection->getStartLine());
  45. if ($content === null) {
  46. return [];
  47. }
  48. $namespace = preg_quote($reflection->getNamespaceName());
  49. $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
  50. $tokenizer = new TokenParser('<?php ' . $content);
  51. return $tokenizer->parseUseStatements($reflection->getNamespaceName());
  52. }
  53. /**
  54. * Gets the content of the file right up to the given line number.
  55. *
  56. * @param string $filename The name of the file to load.
  57. * @param int $lineNumber The number of lines to read from file.
  58. *
  59. * @return string|null The content of the file or null if the file does not exist.
  60. */
  61. private function getFileContent($filename, $lineNumber)
  62. {
  63. if (! is_file($filename)) {
  64. return null;
  65. }
  66. $content = '';
  67. $lineCnt = 0;
  68. $file = new SplFileObject($filename);
  69. while (! $file->eof()) {
  70. if ($lineCnt++ === $lineNumber) {
  71. break;
  72. }
  73. $content .= $file->fgets();
  74. }
  75. return $content;
  76. }
  77. }