PhpDocReader.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php declare(strict_types=1);
  2. namespace PhpDocReader;
  3. use PhpDocReader\PhpParser\UseStatementParser;
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. use ReflectionParameter;
  7. use ReflectionProperty;
  8. use Reflector;
  9. /**
  10. * PhpDoc reader
  11. */
  12. class PhpDocReader
  13. {
  14. /** @var UseStatementParser */
  15. private $parser;
  16. private const PRIMITIVE_TYPES = [
  17. 'bool' => 'bool',
  18. 'boolean' => 'bool',
  19. 'string' => 'string',
  20. 'int' => 'int',
  21. 'integer' => 'int',
  22. 'float' => 'float',
  23. 'double' => 'float',
  24. 'array' => 'array',
  25. 'object' => 'object',
  26. 'callable' => 'callable',
  27. 'resource' => 'resource',
  28. 'mixed' => 'mixed',
  29. 'iterable' => 'iterable',
  30. ];
  31. /** @var bool */
  32. private $ignorePhpDocErrors;
  33. /**
  34. * @param bool $ignorePhpDocErrors Enable or disable throwing errors when PhpDoc errors occur (when parsing annotations).
  35. */
  36. public function __construct(bool $ignorePhpDocErrors = false)
  37. {
  38. $this->parser = new UseStatementParser;
  39. $this->ignorePhpDocErrors = $ignorePhpDocErrors;
  40. }
  41. /**
  42. * Parse the docblock of the property to get the type (class or primitive type) of the var annotation.
  43. *
  44. * @return string|null Type of the property (content of var annotation)
  45. * @throws AnnotationException
  46. */
  47. public function getPropertyType(ReflectionProperty $property): ?string
  48. {
  49. return $this->readPropertyType($property, true);
  50. }
  51. /**
  52. * Parse the docblock of the property to get the class of the var annotation.
  53. *
  54. * @return string|null Type of the property (content of var annotation)
  55. * @throws AnnotationException
  56. */
  57. public function getPropertyClass(ReflectionProperty $property): ?string
  58. {
  59. return $this->readPropertyType($property, false);
  60. }
  61. private function readPropertyType(ReflectionProperty $property, bool $allowPrimitiveTypes): ?string
  62. {
  63. // Get the content of the @var annotation
  64. $docComment = $property->getDocComment();
  65. if (! $docComment) {
  66. return null;
  67. }
  68. if (preg_match('/@var\s+([^\s]+)/', $docComment, $matches)) {
  69. [, $type] = $matches;
  70. } else {
  71. return null;
  72. }
  73. // Ignore primitive types
  74. if (isset(self::PRIMITIVE_TYPES[$type])) {
  75. if ($allowPrimitiveTypes) {
  76. return self::PRIMITIVE_TYPES[$type];
  77. }
  78. return null;
  79. }
  80. // Ignore types containing special characters ([], <> ...)
  81. if (! preg_match('/^[a-zA-Z0-9\\\\_]+$/', $type)) {
  82. return null;
  83. }
  84. $class = $property->getDeclaringClass();
  85. // If the class name is not fully qualified (i.e. doesn't start with a \)
  86. if ($type[0] !== '\\') {
  87. // Try to resolve the FQN using the class context
  88. $resolvedType = $this->tryResolveFqn($type, $class, $property);
  89. if (! $resolvedType && ! $this->ignorePhpDocErrors) {
  90. throw new AnnotationException(sprintf(
  91. 'The @var annotation on %s::%s contains a non existent class "%s". '
  92. . 'Did you maybe forget to add a "use" statement for this annotation?',
  93. $class->name,
  94. $property->getName(),
  95. $type
  96. ));
  97. }
  98. $type = $resolvedType;
  99. }
  100. if (! $this->ignorePhpDocErrors && ! $this->classExists($type)) {
  101. throw new AnnotationException(sprintf(
  102. 'The @var annotation on %s::%s contains a non existent class "%s"',
  103. $class->name,
  104. $property->getName(),
  105. $type
  106. ));
  107. }
  108. // Remove the leading \ (FQN shouldn't contain it)
  109. $type = is_string($type) ? ltrim($type, '\\') : null;
  110. return $type;
  111. }
  112. /**
  113. * Parse the docblock of the property to get the type (class or primitive type) of the param annotation.
  114. *
  115. * @return string|null Type of the property (content of var annotation)
  116. * @throws AnnotationException
  117. */
  118. public function getParameterType(ReflectionParameter $parameter): ?string
  119. {
  120. return $this->readParameterClass($parameter, true);
  121. }
  122. /**
  123. * Parse the docblock of the property to get the class of the param annotation.
  124. *
  125. * @return string|null Type of the property (content of var annotation)
  126. * @throws AnnotationException
  127. */
  128. public function getParameterClass(ReflectionParameter $parameter): ?string
  129. {
  130. return $this->readParameterClass($parameter, false);
  131. }
  132. private function readParameterClass(ReflectionParameter $parameter, bool $allowPrimitiveTypes): ?string
  133. {
  134. // Use reflection
  135. $parameterType = $parameter->getType();
  136. if ($parameterType && $parameterType instanceof \ReflectionNamedType && ! $parameterType->isBuiltin()) {
  137. return $parameterType->getName();
  138. }
  139. $parameterName = $parameter->name;
  140. // Get the content of the @param annotation
  141. $method = $parameter->getDeclaringFunction();
  142. $docComment = $method->getDocComment();
  143. if (! $docComment) {
  144. return null;
  145. }
  146. if (preg_match('/@param\s+([^\s]+)\s+\$' . $parameterName . '/', $docComment, $matches)) {
  147. [, $type] = $matches;
  148. } else {
  149. return null;
  150. }
  151. // Ignore primitive types
  152. if (isset(self::PRIMITIVE_TYPES[$type])) {
  153. if ($allowPrimitiveTypes) {
  154. return self::PRIMITIVE_TYPES[$type];
  155. }
  156. return null;
  157. }
  158. // Ignore types containing special characters ([], <> ...)
  159. if (! preg_match('/^[a-zA-Z0-9\\\\_]+$/', $type)) {
  160. return null;
  161. }
  162. $class = $parameter->getDeclaringClass();
  163. // If the class name is not fully qualified (i.e. doesn't start with a \)
  164. if ($type[0] !== '\\') {
  165. // Try to resolve the FQN using the class context
  166. $resolvedType = $this->tryResolveFqn($type, $class, $parameter);
  167. if (! $resolvedType && ! $this->ignorePhpDocErrors) {
  168. throw new AnnotationException(sprintf(
  169. 'The @param annotation for parameter "%s" of %s::%s contains a non existent class "%s". '
  170. . 'Did you maybe forget to add a "use" statement for this annotation?',
  171. $parameterName,
  172. $class->name,
  173. $method->name,
  174. $type
  175. ));
  176. }
  177. $type = $resolvedType;
  178. }
  179. if (! $this->ignorePhpDocErrors && ! $this->classExists($type)) {
  180. throw new AnnotationException(sprintf(
  181. 'The @param annotation for parameter "%s" of %s::%s contains a non existent class "%s"',
  182. $parameterName,
  183. $class->name,
  184. $method->name,
  185. $type
  186. ));
  187. }
  188. // Remove the leading \ (FQN shouldn't contain it)
  189. $type = is_string($type) ? ltrim($type, '\\') : null;
  190. return $type;
  191. }
  192. /**
  193. * Attempts to resolve the FQN of the provided $type based on the $class and $member context.
  194. *
  195. * @return string|null Fully qualified name of the type, or null if it could not be resolved
  196. */
  197. private function tryResolveFqn(string $type, ReflectionClass $class, Reflector $member): ?string
  198. {
  199. $alias = ($pos = strpos($type, '\\')) === false ? $type : substr($type, 0, $pos);
  200. $loweredAlias = strtolower($alias);
  201. // Retrieve "use" statements
  202. $uses = $this->parser->parseUseStatements($class);
  203. if (isset($uses[$loweredAlias])) {
  204. // Imported classes
  205. if ($pos !== false) {
  206. return $uses[$loweredAlias] . substr($type, $pos);
  207. }
  208. return $uses[$loweredAlias];
  209. }
  210. if ($this->classExists($class->getNamespaceName() . '\\' . $type)) {
  211. return $class->getNamespaceName() . '\\' . $type;
  212. }
  213. if (isset($uses['__NAMESPACE__']) && $this->classExists($uses['__NAMESPACE__'] . '\\' . $type)) {
  214. // Class namespace
  215. return $uses['__NAMESPACE__'] . '\\' . $type;
  216. }
  217. if ($this->classExists($type)) {
  218. // No namespace
  219. return $type;
  220. }
  221. // If all fail, try resolving through related traits
  222. return $this->tryResolveFqnInTraits($type, $class, $member);
  223. }
  224. /**
  225. * Attempts to resolve the FQN of the provided $type based on the $class and $member context, specifically searching
  226. * through the traits that are used by the provided $class.
  227. *
  228. * @return string|null Fully qualified name of the type, or null if it could not be resolved
  229. */
  230. private function tryResolveFqnInTraits(string $type, ReflectionClass $class, Reflector $member): ?string
  231. {
  232. /** @var ReflectionClass[] $traits */
  233. $traits = [];
  234. // Get traits for the class and its parents
  235. while ($class) {
  236. $traits = array_merge($traits, $class->getTraits());
  237. $class = $class->getParentClass();
  238. }
  239. foreach ($traits as $trait) {
  240. // Eliminate traits that don't have the property/method/parameter
  241. if ($member instanceof ReflectionProperty && ! $trait->hasProperty($member->name)) {
  242. continue;
  243. }
  244. if ($member instanceof ReflectionMethod && ! $trait->hasMethod($member->name)) {
  245. continue;
  246. }
  247. if ($member instanceof ReflectionParameter && ! $trait->hasMethod($member->getDeclaringFunction()->name)) {
  248. continue;
  249. }
  250. // Run the resolver again with the ReflectionClass instance for the trait
  251. $resolvedType = $this->tryResolveFqn($type, $trait, $member);
  252. if ($resolvedType) {
  253. return $resolvedType;
  254. }
  255. }
  256. return null;
  257. }
  258. private function classExists(string $class): bool
  259. {
  260. return class_exists($class) || interface_exists($class);
  261. }
  262. }