DocParser.php 49 KB

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