PhpDocExtractor.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\PropertyInfo\Extractor;
  11. use phpDocumentor\Reflection\DocBlock;
  12. use phpDocumentor\Reflection\DocBlockFactory;
  13. use phpDocumentor\Reflection\DocBlockFactoryInterface;
  14. use phpDocumentor\Reflection\Types\Context;
  15. use phpDocumentor\Reflection\Types\ContextFactory;
  16. use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
  17. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  18. use Symfony\Component\PropertyInfo\Type;
  19. use Symfony\Component\PropertyInfo\Util\PhpDocTypeHelper;
  20. /**
  21. * Extracts data using a PHPDoc parser.
  22. *
  23. * @author Kévin Dunglas <dunglas@gmail.com>
  24. *
  25. * @final
  26. */
  27. class PhpDocExtractor implements PropertyDescriptionExtractorInterface, PropertyTypeExtractorInterface
  28. {
  29. const PROPERTY = 0;
  30. const ACCESSOR = 1;
  31. const MUTATOR = 2;
  32. /**
  33. * @var DocBlock[]
  34. */
  35. private $docBlocks = [];
  36. /**
  37. * @var Context[]
  38. */
  39. private $contexts = [];
  40. private $docBlockFactory;
  41. private $contextFactory;
  42. private $phpDocTypeHelper;
  43. private $mutatorPrefixes;
  44. private $accessorPrefixes;
  45. private $arrayMutatorPrefixes;
  46. /**
  47. * @param string[]|null $mutatorPrefixes
  48. * @param string[]|null $accessorPrefixes
  49. * @param string[]|null $arrayMutatorPrefixes
  50. */
  51. public function __construct(DocBlockFactoryInterface $docBlockFactory = null, array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null)
  52. {
  53. if (!class_exists(DocBlockFactory::class)) {
  54. throw new \LogicException(sprintf('Unable to use the "%s" class as the "phpdocumentor/reflection-docblock" package is not installed.', __CLASS__));
  55. }
  56. $this->docBlockFactory = $docBlockFactory ?: DocBlockFactory::createInstance();
  57. $this->contextFactory = new ContextFactory();
  58. $this->phpDocTypeHelper = new PhpDocTypeHelper();
  59. $this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : ReflectionExtractor::$defaultMutatorPrefixes;
  60. $this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : ReflectionExtractor::$defaultAccessorPrefixes;
  61. $this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : ReflectionExtractor::$defaultArrayMutatorPrefixes;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getShortDescription(string $class, string $property, array $context = []): ?string
  67. {
  68. /** @var $docBlock DocBlock */
  69. list($docBlock) = $this->getDocBlock($class, $property);
  70. if (!$docBlock) {
  71. return null;
  72. }
  73. $shortDescription = $docBlock->getSummary();
  74. if (!empty($shortDescription)) {
  75. return $shortDescription;
  76. }
  77. foreach ($docBlock->getTagsByName('var') as $var) {
  78. $varDescription = $var->getDescription()->render();
  79. if (!empty($varDescription)) {
  80. return $varDescription;
  81. }
  82. }
  83. return null;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getLongDescription(string $class, string $property, array $context = []): ?string
  89. {
  90. /** @var $docBlock DocBlock */
  91. list($docBlock) = $this->getDocBlock($class, $property);
  92. if (!$docBlock) {
  93. return null;
  94. }
  95. $contents = $docBlock->getDescription()->render();
  96. return '' === $contents ? null : $contents;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getTypes(string $class, string $property, array $context = []): ?array
  102. {
  103. /** @var $docBlock DocBlock */
  104. list($docBlock, $source, $prefix) = $this->getDocBlock($class, $property);
  105. if (!$docBlock) {
  106. return null;
  107. }
  108. switch ($source) {
  109. case self::PROPERTY:
  110. $tag = 'var';
  111. break;
  112. case self::ACCESSOR:
  113. $tag = 'return';
  114. break;
  115. case self::MUTATOR:
  116. $tag = 'param';
  117. break;
  118. }
  119. $types = [];
  120. /** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
  121. foreach ($docBlock->getTagsByName($tag) as $tag) {
  122. if ($tag && null !== $tag->getType()) {
  123. $types = array_merge($types, $this->phpDocTypeHelper->getTypes($tag->getType()));
  124. }
  125. }
  126. if (!isset($types[0])) {
  127. return null;
  128. }
  129. if (!\in_array($prefix, $this->arrayMutatorPrefixes)) {
  130. return $types;
  131. }
  132. return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $types[0])];
  133. }
  134. private function getDocBlock(string $class, string $property): array
  135. {
  136. $propertyHash = sprintf('%s::%s', $class, $property);
  137. if (isset($this->docBlocks[$propertyHash])) {
  138. return $this->docBlocks[$propertyHash];
  139. }
  140. $ucFirstProperty = ucfirst($property);
  141. switch (true) {
  142. case $docBlock = $this->getDocBlockFromProperty($class, $property):
  143. $data = [$docBlock, self::PROPERTY, null];
  144. break;
  145. case list($docBlock) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::ACCESSOR):
  146. $data = [$docBlock, self::ACCESSOR, null];
  147. break;
  148. case list($docBlock, $prefix) = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::MUTATOR):
  149. $data = [$docBlock, self::MUTATOR, $prefix];
  150. break;
  151. default:
  152. $data = [null, null, null];
  153. }
  154. return $this->docBlocks[$propertyHash] = $data;
  155. }
  156. private function getDocBlockFromProperty(string $class, string $property): ?DocBlock
  157. {
  158. // Use a ReflectionProperty instead of $class to get the parent class if applicable
  159. try {
  160. $reflectionProperty = new \ReflectionProperty($class, $property);
  161. } catch (\ReflectionException $e) {
  162. return null;
  163. }
  164. try {
  165. return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflectionProperty->getDeclaringClass()));
  166. } catch (\InvalidArgumentException $e) {
  167. return null;
  168. } catch (\RuntimeException $e) {
  169. return null;
  170. }
  171. }
  172. private function getDocBlockFromMethod(string $class, string $ucFirstProperty, int $type): ?array
  173. {
  174. $prefixes = self::ACCESSOR === $type ? $this->accessorPrefixes : $this->mutatorPrefixes;
  175. $prefix = null;
  176. foreach ($prefixes as $prefix) {
  177. $methodName = $prefix.$ucFirstProperty;
  178. try {
  179. $reflectionMethod = new \ReflectionMethod($class, $methodName);
  180. if ($reflectionMethod->isStatic()) {
  181. continue;
  182. }
  183. if (
  184. (self::ACCESSOR === $type && 0 === $reflectionMethod->getNumberOfRequiredParameters()) ||
  185. (self::MUTATOR === $type && $reflectionMethod->getNumberOfParameters() >= 1)
  186. ) {
  187. break;
  188. }
  189. } catch (\ReflectionException $e) {
  190. // Try the next prefix if the method doesn't exist
  191. }
  192. }
  193. if (!isset($reflectionMethod)) {
  194. return null;
  195. }
  196. try {
  197. return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix];
  198. } catch (\InvalidArgumentException $e) {
  199. return null;
  200. } catch (\RuntimeException $e) {
  201. return null;
  202. }
  203. }
  204. /**
  205. * Prevents a lot of redundant calls to ContextFactory::createForNamespace().
  206. */
  207. private function createFromReflector(\ReflectionClass $reflector): Context
  208. {
  209. $cacheKey = $reflector->getNamespaceName().':'.$reflector->getFileName();
  210. if (isset($this->contexts[$cacheKey])) {
  211. return $this->contexts[$cacheKey];
  212. }
  213. $this->contexts[$cacheKey] = $this->contextFactory->createFromReflector($reflector);
  214. return $this->contexts[$cacheKey];
  215. }
  216. }