DocParser.php 48 KB

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