vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php line 488

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Common\Annotations;
  3. use Doctrine\Common\Annotations\Annotation\Attribute;
  4. use Doctrine\Common\Annotations\Annotation\Attributes;
  5. use Doctrine\Common\Annotations\Annotation\Enum;
  6. use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
  7. use Doctrine\Common\Annotations\Annotation\Target;
  8. use ReflectionClass;
  9. use ReflectionException;
  10. use ReflectionProperty;
  11. use RuntimeException;
  12. use stdClass;
  13. use Throwable;
  14. use function array_keys;
  15. use function array_map;
  16. use function array_pop;
  17. use function array_values;
  18. use function class_exists;
  19. use function constant;
  20. use function count;
  21. use function defined;
  22. use function explode;
  23. use function gettype;
  24. use function implode;
  25. use function in_array;
  26. use function interface_exists;
  27. use function is_array;
  28. use function is_object;
  29. use function json_encode;
  30. use function ltrim;
  31. use function preg_match;
  32. use function reset;
  33. use function rtrim;
  34. use function sprintf;
  35. use function stripos;
  36. use function strlen;
  37. use function strpos;
  38. use function strrpos;
  39. use function strtolower;
  40. use function substr;
  41. use function trim;
  42. use const PHP_VERSION_ID;
  43. /**
  44. * A parser for docblock annotations.
  45. *
  46. * It is strongly discouraged to change the default annotation parsing process.
  47. *
  48. * @psalm-type Arguments = array{positional_arguments?: array<int, mixed>, named_arguments?: array<string, mixed>}
  49. */
  50. final class DocParser
  51. {
  52. /**
  53. * An array of all valid tokens for a class name.
  54. *
  55. * @phpstan-var list<int>
  56. */
  57. private static $classIdentifiers = [
  58. DocLexer::T_IDENTIFIER,
  59. DocLexer::T_TRUE,
  60. DocLexer::T_FALSE,
  61. DocLexer::T_NULL,
  62. ];
  63. /**
  64. * The lexer.
  65. *
  66. * @var DocLexer
  67. */
  68. private $lexer;
  69. /**
  70. * Current target context.
  71. *
  72. * @var int
  73. */
  74. private $target;
  75. /**
  76. * Doc parser used to collect annotation target.
  77. *
  78. * @var DocParser
  79. */
  80. private static $metadataParser;
  81. /**
  82. * Flag to control if the current annotation is nested or not.
  83. *
  84. * @var bool
  85. */
  86. private $isNestedAnnotation = false;
  87. /**
  88. * Hashmap containing all use-statements that are to be used when parsing
  89. * the given doc block.
  90. *
  91. * @var array<string, class-string>
  92. */
  93. private $imports = [];
  94. /**
  95. * This hashmap is used internally to cache results of class_exists()
  96. * look-ups.
  97. *
  98. * @var array<class-string, bool>
  99. */
  100. private $classExists = [];
  101. /**
  102. * Whether annotations that have not been imported should be ignored.
  103. *
  104. * @var bool
  105. */
  106. private $ignoreNotImportedAnnotations = false;
  107. /**
  108. * An array of default namespaces if operating in simple mode.
  109. *
  110. * @var string[]
  111. */
  112. private $namespaces = [];
  113. /**
  114. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  115. *
  116. * The names must be the raw names as used in the class, not the fully qualified
  117. *
  118. * @var bool[] indexed by annotation name
  119. */
  120. private $ignoredAnnotationNames = [];
  121. /**
  122. * A list with annotations in namespaced format
  123. * that are not causing exceptions when not resolved to an annotation class.
  124. *
  125. * @var bool[] indexed by namespace name
  126. */
  127. private $ignoredAnnotationNamespaces = [];
  128. /** @var string */
  129. private $context = '';
  130. /**
  131. * Hash-map for caching annotation metadata.
  132. *
  133. * @var array<class-string, mixed[]>
  134. */
  135. private static $annotationMetadata = [
  136. Annotation\Target::class => [
  137. 'is_annotation' => true,
  138. 'has_constructor' => true,
  139. 'has_named_argument_constructor' => false,
  140. 'properties' => [],
  141. 'targets_literal' => 'ANNOTATION_CLASS',
  142. 'targets' => Target::TARGET_CLASS,
  143. 'default_property' => 'value',
  144. 'attribute_types' => [
  145. 'value' => [
  146. 'required' => false,
  147. 'type' => 'array',
  148. 'array_type' => 'string',
  149. 'value' => 'array<string>',
  150. ],
  151. ],
  152. ],
  153. Annotation\Attribute::class => [
  154. 'is_annotation' => true,
  155. 'has_constructor' => false,
  156. 'has_named_argument_constructor' => false,
  157. 'targets_literal' => 'ANNOTATION_ANNOTATION',
  158. 'targets' => Target::TARGET_ANNOTATION,
  159. 'default_property' => 'name',
  160. 'properties' => [
  161. 'name' => 'name',
  162. 'type' => 'type',
  163. 'required' => 'required',
  164. ],
  165. 'attribute_types' => [
  166. 'value' => [
  167. 'required' => true,
  168. 'type' => 'string',
  169. 'value' => 'string',
  170. ],
  171. 'type' => [
  172. 'required' => true,
  173. 'type' => 'string',
  174. 'value' => 'string',
  175. ],
  176. 'required' => [
  177. 'required' => false,
  178. 'type' => 'boolean',
  179. 'value' => 'boolean',
  180. ],
  181. ],
  182. ],
  183. Annotation\Attributes::class => [
  184. 'is_annotation' => true,
  185. 'has_constructor' => false,
  186. 'has_named_argument_constructor' => false,
  187. 'targets_literal' => 'ANNOTATION_CLASS',
  188. 'targets' => Target::TARGET_CLASS,
  189. 'default_property' => 'value',
  190. 'properties' => ['value' => 'value'],
  191. 'attribute_types' => [
  192. 'value' => [
  193. 'type' => 'array',
  194. 'required' => true,
  195. 'array_type' => Annotation\Attribute::class,
  196. 'value' => 'array<' . Annotation\Attribute::class . '>',
  197. ],
  198. ],
  199. ],
  200. Annotation\Enum::class => [
  201. 'is_annotation' => true,
  202. 'has_constructor' => true,
  203. 'has_named_argument_constructor' => false,
  204. 'targets_literal' => 'ANNOTATION_PROPERTY',
  205. 'targets' => Target::TARGET_PROPERTY,
  206. 'default_property' => 'value',
  207. 'properties' => ['value' => 'value'],
  208. 'attribute_types' => [
  209. 'value' => [
  210. 'type' => 'array',
  211. 'required' => true,
  212. ],
  213. 'literal' => [
  214. 'type' => 'array',
  215. 'required' => false,
  216. ],
  217. ],
  218. ],
  219. Annotation\NamedArgumentConstructor::class => [
  220. 'is_annotation' => true,
  221. 'has_constructor' => false,
  222. 'has_named_argument_constructor' => false,
  223. 'targets_literal' => 'ANNOTATION_CLASS',
  224. 'targets' => Target::TARGET_CLASS,
  225. 'default_property' => null,
  226. 'properties' => [],
  227. 'attribute_types' => [],
  228. ],
  229. ];
  230. /**
  231. * Hash-map for handle types declaration.
  232. *
  233. * @var array<string, string>
  234. */
  235. private static $typeMap = [
  236. 'float' => 'double',
  237. 'bool' => 'boolean',
  238. // allow uppercase Boolean in honor of George Boole
  239. 'Boolean' => 'boolean',
  240. 'int' => 'integer',
  241. ];
  242. /**
  243. * Constructs a new DocParser.
  244. */
  245. public function __construct()
  246. {
  247. $this->lexer = new DocLexer();
  248. }
  249. /**
  250. * Sets the annotation names that are ignored during the parsing process.
  251. *
  252. * The names are supposed to be the raw names as used in the class, not the
  253. * fully qualified class names.
  254. *
  255. * @param bool[] $names indexed by annotation name
  256. *
  257. * @return void
  258. */
  259. public function setIgnoredAnnotationNames(array $names)
  260. {
  261. $this->ignoredAnnotationNames = $names;
  262. }
  263. /**
  264. * Sets the annotation namespaces that are ignored during the parsing process.
  265. *
  266. * @param bool[] $ignoredAnnotationNamespaces indexed by annotation namespace name
  267. *
  268. * @return void
  269. */
  270. public function setIgnoredAnnotationNamespaces($ignoredAnnotationNamespaces)
  271. {
  272. $this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces;
  273. }
  274. /**
  275. * Sets ignore on not-imported annotations.
  276. *
  277. * @param bool $bool
  278. *
  279. * @return void
  280. */
  281. public function setIgnoreNotImportedAnnotations($bool)
  282. {
  283. $this->ignoreNotImportedAnnotations = (bool) $bool;
  284. }
  285. /**
  286. * Sets the default namespaces.
  287. *
  288. * @param string $namespace
  289. *
  290. * @return void
  291. *
  292. * @throws RuntimeException
  293. */
  294. public function addNamespace($namespace)
  295. {
  296. if ($this->imports) {
  297. throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.');
  298. }
  299. $this->namespaces[] = $namespace;
  300. }
  301. /**
  302. * Sets the imports.
  303. *
  304. * @param array<string, class-string> $imports
  305. *
  306. * @return void
  307. *
  308. * @throws RuntimeException
  309. */
  310. public function setImports(array $imports)
  311. {
  312. if ($this->namespaces) {
  313. throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.');
  314. }
  315. $this->imports = $imports;
  316. }
  317. /**
  318. * Sets current target context as bitmask.
  319. *
  320. * @param int $target
  321. *
  322. * @return void
  323. */
  324. public function setTarget($target)
  325. {
  326. $this->target = $target;
  327. }
  328. /**
  329. * Parses the given docblock string for annotations.
  330. *
  331. * @param string $input The docblock string to parse.
  332. * @param string $context The parsing context.
  333. *
  334. * @phpstan-return list<object> Array of annotations. If no annotations are found, an empty array is returned.
  335. *
  336. * @throws AnnotationException
  337. * @throws ReflectionException
  338. */
  339. public function parse($input, $context = '')
  340. {
  341. $pos = $this->findInitialTokenPosition($input);
  342. if ($pos === null) {
  343. return [];
  344. }
  345. $this->context = $context;
  346. $this->lexer->setInput(trim(substr($input, $pos), '* /'));
  347. $this->lexer->moveNext();
  348. return $this->Annotations();
  349. }
  350. /**
  351. * Finds the first valid annotation
  352. *
  353. * @param string $input The docblock string to parse
  354. */
  355. private function findInitialTokenPosition($input): ?int
  356. {
  357. $pos = 0;
  358. // search for first valid annotation
  359. while (($pos = strpos($input, '@', $pos)) !== false) {
  360. $preceding = substr($input, $pos - 1, 1);
  361. // if the @ is preceded by a space, a tab or * it is valid
  362. if ($pos === 0 || $preceding === ' ' || $preceding === '*' || $preceding === "\t") {
  363. return $pos;
  364. }
  365. $pos++;
  366. }
  367. return null;
  368. }
  369. /**
  370. * Attempts to match the given token with the current lookahead token.
  371. * If they match, updates the lookahead token; otherwise raises a syntax error.
  372. *
  373. * @param int $token Type of token.
  374. *
  375. * @return bool True if tokens match; false otherwise.
  376. *
  377. * @throws AnnotationException
  378. */
  379. private function match(int $token): bool
  380. {
  381. if (! $this->lexer->isNextToken($token)) {
  382. throw $this->syntaxError($this->lexer->getLiteral($token));
  383. }
  384. return $this->lexer->moveNext();
  385. }
  386. /**
  387. * Attempts to match the current lookahead token with any of the given tokens.
  388. *
  389. * If any of them matches, this method updates the lookahead token; otherwise
  390. * a syntax error is raised.
  391. *
  392. * @phpstan-param list<mixed[]> $tokens
  393. *
  394. * @throws AnnotationException
  395. */
  396. private function matchAny(array $tokens): bool
  397. {
  398. if (! $this->lexer->isNextTokenAny($tokens)) {
  399. throw $this->syntaxError(implode(' or ', array_map([$this->lexer, 'getLiteral'], $tokens)));
  400. }
  401. return $this->lexer->moveNext();
  402. }
  403. /**
  404. * Generates a new syntax error.
  405. *
  406. * @param string $expected Expected string.
  407. * @param mixed[]|null $token Optional token.
  408. */
  409. private function syntaxError(string $expected, ?array $token = null): AnnotationException
  410. {
  411. if ($token === null) {
  412. $token = $this->lexer->lookahead;
  413. }
  414. $message = sprintf('Expected %s, got ', $expected);
  415. $message .= $this->lexer->lookahead === null
  416. ? 'end of string'
  417. : sprintf("'%s' at position %s", $token['value'], $token['position']);
  418. if (strlen($this->context)) {
  419. $message .= ' in ' . $this->context;
  420. }
  421. $message .= '.';
  422. return AnnotationException::syntaxError($message);
  423. }
  424. /**
  425. * Attempts to check if a class exists or not. This never goes through the PHP autoloading mechanism
  426. * but uses the {@link AnnotationRegistry} to load classes.
  427. *
  428. * @param class-string $fqcn
  429. */
  430. private function classExists(string $fqcn): bool
  431. {
  432. if (isset($this->classExists[$fqcn])) {
  433. return $this->classExists[$fqcn];
  434. }
  435. // first check if the class already exists, maybe loaded through another AnnotationReader
  436. if (class_exists($fqcn, false)) {
  437. return $this->classExists[$fqcn] = true;
  438. }
  439. // final check, does this class exist?
  440. return $this->classExists[$fqcn] = AnnotationRegistry::loadAnnotationClass($fqcn);
  441. }
  442. /**
  443. * Collects parsing metadata for a given annotation class
  444. *
  445. * @param class-string $name The annotation name
  446. *
  447. * @throws AnnotationException
  448. * @throws ReflectionException
  449. */
  450. private function collectAnnotationMetadata(string $name): void
  451. {
  452. if (self::$metadataParser === null) {
  453. self::$metadataParser = new self();
  454. self::$metadataParser->setIgnoreNotImportedAnnotations(true);
  455. self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames);
  456. self::$metadataParser->setImports([
  457. 'enum' => Enum::class,
  458. 'target' => Target::class,
  459. 'attribute' => Attribute::class,
  460. 'attributes' => Attributes::class,
  461. 'namedargumentconstructor' => NamedArgumentConstructor::class,
  462. ]);
  463. // Make sure that annotations from metadata are loaded
  464. class_exists(Enum::class);
  465. class_exists(Target::class);
  466. class_exists(Attribute::class);
  467. class_exists(Attributes::class);
  468. class_exists(NamedArgumentConstructor::class);
  469. }
  470. $class = new ReflectionClass($name);
  471. $docComment = $class->getDocComment();
  472. // Sets default values for annotation metadata
  473. $constructor = $class->getConstructor();
  474. $metadata = [
  475. 'default_property' => null,
  476. 'has_constructor' => $constructor !== null && $constructor->getNumberOfParameters() > 0,
  477. 'constructor_args' => [],
  478. 'properties' => [],
  479. 'property_types' => [],
  480. 'attribute_types' => [],
  481. 'targets_literal' => null,
  482. 'targets' => Target::TARGET_ALL,
  483. 'is_annotation' => strpos($docComment, '@Annotation') !== false,
  484. ];
  485. $metadata['has_named_argument_constructor'] = $metadata['has_constructor']
  486. && $class->implementsInterface(NamedArgumentConstructorAnnotation::class);
  487. // verify that the class is really meant to be an annotation
  488. if ($metadata['is_annotation']) {
  489. self::$metadataParser->setTarget(Target::TARGET_CLASS);
  490. foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) {
  491. if ($annotation instanceof Target) {
  492. $metadata['targets'] = $annotation->targets;
  493. $metadata['targets_literal'] = $annotation->literal;
  494. continue;
  495. }
  496. if ($annotation instanceof NamedArgumentConstructor) {
  497. $metadata['has_named_argument_constructor'] = $metadata['has_constructor'];
  498. if ($metadata['has_named_argument_constructor']) {
  499. // choose the first argument as the default property
  500. $metadata['default_property'] = $constructor->getParameters()[0]->getName();
  501. }
  502. }
  503. if (! ($annotation instanceof Attributes)) {
  504. continue;
  505. }
  506. foreach ($annotation->value as $attribute) {
  507. $this->collectAttributeTypeMetadata($metadata, $attribute);
  508. }
  509. }
  510. // if not has a constructor will inject values into public properties
  511. if ($metadata['has_constructor'] === false) {
  512. // collect all public properties
  513. foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  514. $metadata['properties'][$property->name] = $property->name;
  515. $propertyComment = $property->getDocComment();
  516. if ($propertyComment === false) {
  517. continue;
  518. }
  519. $attribute = new Attribute();
  520. $attribute->required = (strpos($propertyComment, '@Required') !== false);
  521. $attribute->name = $property->name;
  522. $attribute->type = (strpos($propertyComment, '@var') !== false &&
  523. preg_match('/@var\s+([^\s]+)/', $propertyComment, $matches))
  524. ? $matches[1]
  525. : 'mixed';
  526. $this->collectAttributeTypeMetadata($metadata, $attribute);
  527. // checks if the property has @Enum
  528. if (strpos($propertyComment, '@Enum') === false) {
  529. continue;
  530. }
  531. $context = 'property ' . $class->name . '::$' . $property->name;
  532. self::$metadataParser->setTarget(Target::TARGET_PROPERTY);
  533. foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) {
  534. if (! $annotation instanceof Enum) {
  535. continue;
  536. }
  537. $metadata['enum'][$property->name]['value'] = $annotation->value;
  538. $metadata['enum'][$property->name]['literal'] = (! empty($annotation->literal))
  539. ? $annotation->literal
  540. : $annotation->value;
  541. }
  542. }
  543. // choose the first property as default property
  544. $metadata['default_property'] = reset($metadata['properties']);
  545. } elseif ($metadata['has_named_argument_constructor']) {
  546. foreach ($constructor->getParameters() as $parameter) {
  547. if ($parameter->isVariadic()) {
  548. break;
  549. }
  550. $metadata['constructor_args'][$parameter->getName()] = [
  551. 'position' => $parameter->getPosition(),
  552. 'default' => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
  553. ];
  554. }
  555. }
  556. }
  557. self::$annotationMetadata[$name] = $metadata;
  558. }
  559. /**
  560. * Collects parsing metadata for a given attribute.
  561. *
  562. * @param mixed[] $metadata
  563. */
  564. private function collectAttributeTypeMetadata(array &$metadata, Attribute $attribute): void
  565. {
  566. // handle internal type declaration
  567. $type = self::$typeMap[$attribute->type] ?? $attribute->type;
  568. // handle the case if the property type is mixed
  569. if ($type === 'mixed') {
  570. return;
  571. }
  572. // Evaluate type
  573. $pos = strpos($type, '<');
  574. if ($pos !== false) {
  575. // Checks if the property has array<type>
  576. $arrayType = substr($type, $pos + 1, -1);
  577. $type = 'array';
  578. if (isset(self::$typeMap[$arrayType])) {
  579. $arrayType = self::$typeMap[$arrayType];
  580. }
  581. $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType;
  582. } else {
  583. // Checks if the property has type[]
  584. $pos = strrpos($type, '[');
  585. if ($pos !== false) {
  586. $arrayType = substr($type, 0, $pos);
  587. $type = 'array';
  588. if (isset(self::$typeMap[$arrayType])) {
  589. $arrayType = self::$typeMap[$arrayType];
  590. }
  591. $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType;
  592. }
  593. }
  594. $metadata['attribute_types'][$attribute->name]['type'] = $type;
  595. $metadata['attribute_types'][$attribute->name]['value'] = $attribute->type;
  596. $metadata['attribute_types'][$attribute->name]['required'] = $attribute->required;
  597. }
  598. /**
  599. * Annotations ::= Annotation {[ "*" ]* [Annotation]}*
  600. *
  601. * @phpstan-return list<object>
  602. *
  603. * @throws AnnotationException
  604. * @throws ReflectionException
  605. */
  606. private function Annotations(): array
  607. {
  608. $annotations = [];
  609. while ($this->lexer->lookahead !== null) {
  610. if ($this->lexer->lookahead['type'] !== DocLexer::T_AT) {
  611. $this->lexer->moveNext();
  612. continue;
  613. }
  614. // make sure the @ is preceded by non-catchable pattern
  615. if (
  616. $this->lexer->token !== null &&
  617. $this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen(
  618. $this->lexer->token['value']
  619. )
  620. ) {
  621. $this->lexer->moveNext();
  622. continue;
  623. }
  624. // make sure the @ is followed by either a namespace separator, or
  625. // an identifier token
  626. $peek = $this->lexer->glimpse();
  627. if (
  628. ($peek === null)
  629. || ($peek['type'] !== DocLexer::T_NAMESPACE_SEPARATOR && ! in_array(
  630. $peek['type'],
  631. self::$classIdentifiers,
  632. true
  633. ))
  634. || $peek['position'] !== $this->lexer->lookahead['position'] + 1
  635. ) {
  636. $this->lexer->moveNext();
  637. continue;
  638. }
  639. $this->isNestedAnnotation = false;
  640. $annot = $this->Annotation();
  641. if ($annot === false) {
  642. continue;
  643. }
  644. $annotations[] = $annot;
  645. }
  646. return $annotations;
  647. }
  648. /**
  649. * Annotation ::= "@" AnnotationName MethodCall
  650. * AnnotationName ::= QualifiedName | SimpleName
  651. * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName
  652. * NameSpacePart ::= identifier | null | false | true
  653. * SimpleName ::= identifier | null | false | true
  654. *
  655. * @return object|false False if it is not a valid annotation.
  656. *
  657. * @throws AnnotationException
  658. * @throws ReflectionException
  659. */
  660. private function Annotation()
  661. {
  662. $this->match(DocLexer::T_AT);
  663. // check if we have an annotation
  664. $name = $this->Identifier();
  665. if (
  666. $this->lexer->isNextToken(DocLexer::T_MINUS)
  667. && $this->lexer->nextTokenIsAdjacent()
  668. ) {
  669. // Annotations with dashes, such as "@foo-" or "@foo-bar", are to be discarded
  670. return false;
  671. }
  672. // only process names which are not fully qualified, yet
  673. // fully qualified names must start with a \
  674. $originalName = $name;
  675. if ($name[0] !== '\\') {
  676. $pos = strpos($name, '\\');
  677. $alias = ($pos === false) ? $name : substr($name, 0, $pos);
  678. $found = false;
  679. $loweredAlias = strtolower($alias);
  680. if ($this->namespaces) {
  681. foreach ($this->namespaces as $namespace) {
  682. if ($this->classExists($namespace . '\\' . $name)) {
  683. $name = $namespace . '\\' . $name;
  684. $found = true;
  685. break;
  686. }
  687. }
  688. } elseif (isset($this->imports[$loweredAlias])) {
  689. $namespace = ltrim($this->imports[$loweredAlias], '\\');
  690. $name = ($pos !== false)
  691. ? $namespace . substr($name, $pos)
  692. : $namespace;
  693. $found = $this->classExists($name);
  694. } elseif (
  695. ! isset($this->ignoredAnnotationNames[$name])
  696. && isset($this->imports['__NAMESPACE__'])
  697. && $this->classExists($this->imports['__NAMESPACE__'] . '\\' . $name)
  698. ) {
  699. $name = $this->imports['__NAMESPACE__'] . '\\' . $name;
  700. $found = true;
  701. } elseif (! isset($this->ignoredAnnotationNames[$name]) && $this->classExists($name)) {
  702. $found = true;
  703. }
  704. if (! $found) {
  705. if ($this->isIgnoredAnnotation($name)) {
  706. return false;
  707. }
  708. throw AnnotationException::semanticalError(sprintf(
  709. <<<'EXCEPTION'
  710. The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?
  711. EXCEPTION
  712. ,
  713. $name,
  714. $this->context
  715. ));
  716. }
  717. }
  718. $name = ltrim($name, '\\');
  719. if (! $this->classExists($name)) {
  720. throw AnnotationException::semanticalError(sprintf(
  721. 'The annotation "@%s" in %s does not exist, or could not be auto-loaded.',
  722. $name,
  723. $this->context
  724. ));
  725. }
  726. // at this point, $name contains the fully qualified class name of the
  727. // annotation, and it is also guaranteed that this class exists, and
  728. // that it is loaded
  729. // collects the metadata annotation only if there is not yet
  730. if (! isset(self::$annotationMetadata[$name])) {
  731. $this->collectAnnotationMetadata($name);
  732. }
  733. // verify that the class is really meant to be an annotation and not just any ordinary class
  734. if (self::$annotationMetadata[$name]['is_annotation'] === false) {
  735. if ($this->isIgnoredAnnotation($originalName) || $this->isIgnoredAnnotation($name)) {
  736. return false;
  737. }
  738. throw AnnotationException::semanticalError(sprintf(
  739. <<<'EXCEPTION'
  740. The class "%s" is not annotated with @Annotation.
  741. Are you sure this class can be used as annotation?
  742. If so, then you need to add @Annotation to the _class_ doc comment of "%s".
  743. If it is indeed no annotation, then you need to add @IgnoreAnnotation("%s") to the _class_ doc comment of %s.
  744. EXCEPTION
  745. ,
  746. $name,
  747. $name,
  748. $originalName,
  749. $this->context
  750. ));
  751. }
  752. //if target is nested annotation
  753. $target = $this->isNestedAnnotation ? Target::TARGET_ANNOTATION : $this->target;
  754. // Next will be nested
  755. $this->isNestedAnnotation = true;
  756. //if annotation does not support current target
  757. if ((self::$annotationMetadata[$name]['targets'] & $target) === 0 && $target) {
  758. throw AnnotationException::semanticalError(
  759. sprintf(
  760. <<<'EXCEPTION'
  761. Annotation @%s is not allowed to be declared on %s. You may only use this annotation on these code elements: %s.
  762. EXCEPTION
  763. ,
  764. $originalName,
  765. $this->context,
  766. self::$annotationMetadata[$name]['targets_literal']
  767. )
  768. );
  769. }
  770. $arguments = $this->MethodCall();
  771. $values = $this->resolvePositionalValues($arguments, $name);
  772. if (isset(self::$annotationMetadata[$name]['enum'])) {
  773. // checks all declared attributes
  774. foreach (self::$annotationMetadata[$name]['enum'] as $property => $enum) {
  775. // checks if the attribute is a valid enumerator
  776. if (isset($values[$property]) && ! in_array($values[$property], $enum['value'])) {
  777. throw AnnotationException::enumeratorError(
  778. $property,
  779. $name,
  780. $this->context,
  781. $enum['literal'],
  782. $values[$property]
  783. );
  784. }
  785. }
  786. }
  787. // checks all declared attributes
  788. foreach (self::$annotationMetadata[$name]['attribute_types'] as $property => $type) {
  789. if (
  790. $property === self::$annotationMetadata[$name]['default_property']
  791. && ! isset($values[$property]) && isset($values['value'])
  792. ) {
  793. $property = 'value';
  794. }
  795. // handle a not given attribute or null value
  796. if (! isset($values[$property])) {
  797. if ($type['required']) {
  798. throw AnnotationException::requiredError(
  799. $property,
  800. $originalName,
  801. $this->context,
  802. 'a(n) ' . $type['value']
  803. );
  804. }
  805. continue;
  806. }
  807. if ($type['type'] === 'array') {
  808. // handle the case of a single value
  809. if (! is_array($values[$property])) {
  810. $values[$property] = [$values[$property]];
  811. }
  812. // checks if the attribute has array type declaration, such as "array<string>"
  813. if (isset($type['array_type'])) {
  814. foreach ($values[$property] as $item) {
  815. if (gettype($item) !== $type['array_type'] && ! $item instanceof $type['array_type']) {
  816. throw AnnotationException::attributeTypeError(
  817. $property,
  818. $originalName,
  819. $this->context,
  820. 'either a(n) ' . $type['array_type'] . ', or an array of ' . $type['array_type'] . 's',
  821. $item
  822. );
  823. }
  824. }
  825. }
  826. } elseif (gettype($values[$property]) !== $type['type'] && ! $values[$property] instanceof $type['type']) {
  827. throw AnnotationException::attributeTypeError(
  828. $property,
  829. $originalName,
  830. $this->context,
  831. 'a(n) ' . $type['value'],
  832. $values[$property]
  833. );
  834. }
  835. }
  836. if (self::$annotationMetadata[$name]['has_named_argument_constructor']) {
  837. if (PHP_VERSION_ID >= 80000) {
  838. foreach ($values as $property => $value) {
  839. if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) {
  840. throw AnnotationException::creationError(sprintf(
  841. <<<'EXCEPTION'
  842. The annotation @%s declared on %s does not have a property named "%s"
  843. that can be set through its named arguments constructor.
  844. Available named arguments: %s
  845. EXCEPTION
  846. ,
  847. $originalName,
  848. $this->context,
  849. $property,
  850. implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args']))
  851. ));
  852. }
  853. }
  854. return $this->instantiateAnnotiation($originalName, $this->context, $name, $values);
  855. }
  856. $positionalValues = [];
  857. foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) {
  858. $positionalValues[$parameter['position']] = $parameter['default'];
  859. }
  860. foreach ($values as $property => $value) {
  861. if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) {
  862. throw AnnotationException::creationError(sprintf(
  863. <<<'EXCEPTION'
  864. The annotation @%s declared on %s does not have a property named "%s"
  865. that can be set through its named arguments constructor.
  866. Available named arguments: %s
  867. EXCEPTION
  868. ,
  869. $originalName,
  870. $this->context,
  871. $property,
  872. implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args']))
  873. ));
  874. }
  875. $positionalValues[self::$annotationMetadata[$name]['constructor_args'][$property]['position']] = $value;
  876. }
  877. return $this->instantiateAnnotiation($originalName, $this->context, $name, $positionalValues);
  878. }
  879. // check if the annotation expects values via the constructor,
  880. // or directly injected into public properties
  881. if (self::$annotationMetadata[$name]['has_constructor'] === true) {
  882. return $this->instantiateAnnotiation($originalName, $this->context, $name, [$values]);
  883. }
  884. $instance = $this->instantiateAnnotiation($originalName, $this->context, $name, []);
  885. foreach ($values as $property => $value) {
  886. if (! isset(self::$annotationMetadata[$name]['properties'][$property])) {
  887. if ($property !== 'value') {
  888. throw AnnotationException::creationError(sprintf(
  889. <<<'EXCEPTION'
  890. The annotation @%s declared on %s does not have a property named "%s".
  891. Available properties: %s
  892. EXCEPTION
  893. ,
  894. $originalName,
  895. $this->context,
  896. $property,
  897. implode(', ', self::$annotationMetadata[$name]['properties'])
  898. ));
  899. }
  900. // handle the case if the property has no annotations
  901. $property = self::$annotationMetadata[$name]['default_property'];
  902. if (! $property) {
  903. throw AnnotationException::creationError(sprintf(
  904. 'The annotation @%s declared on %s does not accept any values, but got %s.',
  905. $originalName,
  906. $this->context,
  907. json_encode($values)
  908. ));
  909. }
  910. }
  911. $instance->{$property} = $value;
  912. }
  913. return $instance;
  914. }
  915. /**
  916. * MethodCall ::= ["(" [Values] ")"]
  917. *
  918. * @psalm-return Arguments
  919. *
  920. * @throws AnnotationException
  921. * @throws ReflectionException
  922. */
  923. private function MethodCall(): array
  924. {
  925. $values = [];
  926. if (! $this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) {
  927. return $values;
  928. }
  929. $this->match(DocLexer::T_OPEN_PARENTHESIS);
  930. if (! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) {
  931. $values = $this->Values();
  932. }
  933. $this->match(DocLexer::T_CLOSE_PARENTHESIS);
  934. return $values;
  935. }
  936. /**
  937. * Values ::= Array | Value {"," Value}* [","]
  938. *
  939. * @psalm-return Arguments
  940. *
  941. * @throws AnnotationException
  942. * @throws ReflectionException
  943. */
  944. private function Values(): array
  945. {
  946. $values = [$this->Value()];
  947. while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
  948. $this->match(DocLexer::T_COMMA);
  949. if ($this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) {
  950. break;
  951. }
  952. $token = $this->lexer->lookahead;
  953. $value = $this->Value();
  954. $values[] = $value;
  955. }
  956. $namedArguments = [];
  957. $positionalArguments = [];
  958. foreach ($values as $k => $value) {
  959. if (is_object($value) && $value instanceof stdClass) {
  960. $namedArguments[$value->name] = $value->value;
  961. } else {
  962. $positionalArguments[$k] = $value;
  963. }
  964. }
  965. return ['named_arguments' => $namedArguments, 'positional_arguments' => $positionalArguments];
  966. }
  967. /**
  968. * Constant ::= integer | string | float | boolean
  969. *
  970. * @return mixed
  971. *
  972. * @throws AnnotationException
  973. */
  974. private function Constant()
  975. {
  976. $identifier = $this->Identifier();
  977. if (! defined($identifier) && strpos($identifier, '::') !== false && $identifier[0] !== '\\') {
  978. [$className, $const] = explode('::', $identifier);
  979. $pos = strpos($className, '\\');
  980. $alias = ($pos === false) ? $className : substr($className, 0, $pos);
  981. $found = false;
  982. $loweredAlias = strtolower($alias);
  983. switch (true) {
  984. case ! empty($this->namespaces):
  985. foreach ($this->namespaces as $ns) {
  986. if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) {
  987. $className = $ns . '\\' . $className;
  988. $found = true;
  989. break;
  990. }
  991. }
  992. break;
  993. case isset($this->imports[$loweredAlias]):
  994. $found = true;
  995. $className = ($pos !== false)
  996. ? $this->imports[$loweredAlias] . substr($className, $pos)
  997. : $this->imports[$loweredAlias];
  998. break;
  999. default:
  1000. if (isset($this->imports['__NAMESPACE__'])) {
  1001. $ns = $this->imports['__NAMESPACE__'];
  1002. if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) {
  1003. $className = $ns . '\\' . $className;
  1004. $found = true;
  1005. }
  1006. }
  1007. break;
  1008. }
  1009. if ($found) {
  1010. $identifier = $className . '::' . $const;
  1011. }
  1012. }
  1013. /**
  1014. * Checks if identifier ends with ::class and remove the leading backslash if it exists.
  1015. */
  1016. if (
  1017. $this->identifierEndsWithClassConstant($identifier) &&
  1018. ! $this->identifierStartsWithBackslash($identifier)
  1019. ) {
  1020. return substr($identifier, 0, $this->getClassConstantPositionInIdentifier($identifier));
  1021. }
  1022. if ($this->identifierEndsWithClassConstant($identifier) && $this->identifierStartsWithBackslash($identifier)) {
  1023. return substr($identifier, 1, $this->getClassConstantPositionInIdentifier($identifier) - 1);
  1024. }
  1025. if (! defined($identifier)) {
  1026. throw AnnotationException::semanticalErrorConstants($identifier, $this->context);
  1027. }
  1028. return constant($identifier);
  1029. }
  1030. private function identifierStartsWithBackslash(string $identifier): bool
  1031. {
  1032. return $identifier[0] === '\\';
  1033. }
  1034. private function identifierEndsWithClassConstant(string $identifier): bool
  1035. {
  1036. return $this->getClassConstantPositionInIdentifier($identifier) === strlen($identifier) - strlen('::class');
  1037. }
  1038. /** @return int|false */
  1039. private function getClassConstantPositionInIdentifier(string $identifier)
  1040. {
  1041. return stripos($identifier, '::class');
  1042. }
  1043. /**
  1044. * Identifier ::= string
  1045. *
  1046. * @throws AnnotationException
  1047. */
  1048. private function Identifier(): string
  1049. {
  1050. // check if we have an annotation
  1051. if (! $this->lexer->isNextTokenAny(self::$classIdentifiers)) {
  1052. throw $this->syntaxError('namespace separator or identifier');
  1053. }
  1054. $this->lexer->moveNext();
  1055. $className = $this->lexer->token['value'];
  1056. while (
  1057. $this->lexer->lookahead !== null &&
  1058. $this->lexer->lookahead['position'] === ($this->lexer->token['position'] +
  1059. strlen($this->lexer->token['value'])) &&
  1060. $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)
  1061. ) {
  1062. $this->match(DocLexer::T_NAMESPACE_SEPARATOR);
  1063. $this->matchAny(self::$classIdentifiers);
  1064. $className .= '\\' . $this->lexer->token['value'];
  1065. }
  1066. return $className;
  1067. }
  1068. /**
  1069. * Value ::= PlainValue | FieldAssignment
  1070. *
  1071. * @return mixed
  1072. *
  1073. * @throws AnnotationException
  1074. * @throws ReflectionException
  1075. */
  1076. private function Value()
  1077. {
  1078. $peek = $this->lexer->glimpse();
  1079. if ($peek['type'] === DocLexer::T_EQUALS) {
  1080. return $this->FieldAssignment();
  1081. }
  1082. return $this->PlainValue();
  1083. }
  1084. /**
  1085. * PlainValue ::= integer | string | float | boolean | Array | Annotation
  1086. *
  1087. * @return mixed
  1088. *
  1089. * @throws AnnotationException
  1090. * @throws ReflectionException
  1091. */
  1092. private function PlainValue()
  1093. {
  1094. if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) {
  1095. return $this->Arrayx();
  1096. }
  1097. if ($this->lexer->isNextToken(DocLexer::T_AT)) {
  1098. return $this->Annotation();
  1099. }
  1100. if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) {
  1101. return $this->Constant();
  1102. }
  1103. switch ($this->lexer->lookahead['type']) {
  1104. case DocLexer::T_STRING:
  1105. $this->match(DocLexer::T_STRING);
  1106. return $this->lexer->token['value'];
  1107. case DocLexer::T_INTEGER:
  1108. $this->match(DocLexer::T_INTEGER);
  1109. return (int) $this->lexer->token['value'];
  1110. case DocLexer::T_FLOAT:
  1111. $this->match(DocLexer::T_FLOAT);
  1112. return (float) $this->lexer->token['value'];
  1113. case DocLexer::T_TRUE:
  1114. $this->match(DocLexer::T_TRUE);
  1115. return true;
  1116. case DocLexer::T_FALSE:
  1117. $this->match(DocLexer::T_FALSE);
  1118. return false;
  1119. case DocLexer::T_NULL:
  1120. $this->match(DocLexer::T_NULL);
  1121. return null;
  1122. default:
  1123. throw $this->syntaxError('PlainValue');
  1124. }
  1125. }
  1126. /**
  1127. * FieldAssignment ::= FieldName "=" PlainValue
  1128. * FieldName ::= identifier
  1129. *
  1130. * @throws AnnotationException
  1131. * @throws ReflectionException
  1132. */
  1133. private function FieldAssignment(): stdClass
  1134. {
  1135. $this->match(DocLexer::T_IDENTIFIER);
  1136. $fieldName = $this->lexer->token['value'];
  1137. $this->match(DocLexer::T_EQUALS);
  1138. $item = new stdClass();
  1139. $item->name = $fieldName;
  1140. $item->value = $this->PlainValue();
  1141. return $item;
  1142. }
  1143. /**
  1144. * Array ::= "{" ArrayEntry {"," ArrayEntry}* [","] "}"
  1145. *
  1146. * @return mixed[]
  1147. *
  1148. * @throws AnnotationException
  1149. * @throws ReflectionException
  1150. */
  1151. private function Arrayx(): array
  1152. {
  1153. $array = $values = [];
  1154. $this->match(DocLexer::T_OPEN_CURLY_BRACES);
  1155. // If the array is empty, stop parsing and return.
  1156. if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) {
  1157. $this->match(DocLexer::T_CLOSE_CURLY_BRACES);
  1158. return $array;
  1159. }
  1160. $values[] = $this->ArrayEntry();
  1161. while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
  1162. $this->match(DocLexer::T_COMMA);
  1163. // optional trailing comma
  1164. if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) {
  1165. break;
  1166. }
  1167. $values[] = $this->ArrayEntry();
  1168. }
  1169. $this->match(DocLexer::T_CLOSE_CURLY_BRACES);
  1170. foreach ($values as $value) {
  1171. [$key, $val] = $value;
  1172. if ($key !== null) {
  1173. $array[$key] = $val;
  1174. } else {
  1175. $array[] = $val;
  1176. }
  1177. }
  1178. return $array;
  1179. }
  1180. /**
  1181. * ArrayEntry ::= Value | KeyValuePair
  1182. * KeyValuePair ::= Key ("=" | ":") PlainValue | Constant
  1183. * Key ::= string | integer | Constant
  1184. *
  1185. * @phpstan-return array{mixed, mixed}
  1186. *
  1187. * @throws AnnotationException
  1188. * @throws ReflectionException
  1189. */
  1190. private function ArrayEntry(): array
  1191. {
  1192. $peek = $this->lexer->glimpse();
  1193. if (
  1194. $peek['type'] === DocLexer::T_EQUALS
  1195. || $peek['type'] === DocLexer::T_COLON
  1196. ) {
  1197. if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) {
  1198. $key = $this->Constant();
  1199. } else {
  1200. $this->matchAny([DocLexer::T_INTEGER, DocLexer::T_STRING]);
  1201. $key = $this->lexer->token['value'];
  1202. }
  1203. $this->matchAny([DocLexer::T_EQUALS, DocLexer::T_COLON]);
  1204. return [$key, $this->PlainValue()];
  1205. }
  1206. return [null, $this->Value()];
  1207. }
  1208. /**
  1209. * Checks whether the given $name matches any ignored annotation name or namespace
  1210. */
  1211. private function isIgnoredAnnotation(string $name): bool
  1212. {
  1213. if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
  1214. return true;
  1215. }
  1216. foreach (array_keys($this->ignoredAnnotationNamespaces) as $ignoredAnnotationNamespace) {
  1217. $ignoredAnnotationNamespace = rtrim($ignoredAnnotationNamespace, '\\') . '\\';
  1218. if (stripos(rtrim($name, '\\') . '\\', $ignoredAnnotationNamespace) === 0) {
  1219. return true;
  1220. }
  1221. }
  1222. return false;
  1223. }
  1224. /**
  1225. * Resolve positional arguments (without name) to named ones
  1226. *
  1227. * @psalm-param Arguments $arguments
  1228. *
  1229. * @return array<string,mixed>
  1230. */
  1231. private function resolvePositionalValues(array $arguments, string $name): array
  1232. {
  1233. $positionalArguments = $arguments['positional_arguments'] ?? [];
  1234. $values = $arguments['named_arguments'] ?? [];
  1235. if (
  1236. self::$annotationMetadata[$name]['has_named_argument_constructor']
  1237. && self::$annotationMetadata[$name]['default_property'] !== null
  1238. ) {
  1239. // We must ensure that we don't have positional arguments after named ones
  1240. $positions = array_keys($positionalArguments);
  1241. $lastPosition = null;
  1242. foreach ($positions as $position) {
  1243. if (
  1244. ($lastPosition === null && $position !== 0) ||
  1245. ($lastPosition !== null && $position !== $lastPosition + 1)
  1246. ) {
  1247. throw $this->syntaxError('Positional arguments after named arguments is not allowed');
  1248. }
  1249. $lastPosition = $position;
  1250. }
  1251. foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) {
  1252. $position = $parameter['position'];
  1253. if (isset($values[$property]) || ! isset($positionalArguments[$position])) {
  1254. continue;
  1255. }
  1256. $values[$property] = $positionalArguments[$position];
  1257. }
  1258. } else {
  1259. if (count($positionalArguments) > 0 && ! isset($values['value'])) {
  1260. if (count($positionalArguments) === 1) {
  1261. $value = array_pop($positionalArguments);
  1262. } else {
  1263. $value = array_values($positionalArguments);
  1264. }
  1265. $values['value'] = $value;
  1266. }
  1267. }
  1268. return $values;
  1269. }
  1270. /**
  1271. * Try to instantiate the annotation and catch and process any exceptions related to failure
  1272. *
  1273. * @param class-string $name
  1274. * @param array<string,mixed> $arguments
  1275. *
  1276. * @return object
  1277. *
  1278. * @throws AnnotationException
  1279. */
  1280. private function instantiateAnnotiation(string $originalName, string $context, string $name, array $arguments)
  1281. {
  1282. try {
  1283. return new $name(...$arguments);
  1284. } catch (Throwable $exception) {
  1285. throw AnnotationException::creationError(
  1286. sprintf(
  1287. 'An error occurred while instantiating the annotation @%s declared on %s: "%s".',
  1288. $originalName,
  1289. $context,
  1290. $exception->getMessage()
  1291. ),
  1292. $exception
  1293. );
  1294. }
  1295. }
  1296. }