DocParser.php 47 KB

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