123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506 |
- <?php
- namespace Doctrine\Common\Annotations;
- use Doctrine\Common\Annotations\Annotation\Attribute;
- use Doctrine\Common\Annotations\Annotation\Attributes;
- use Doctrine\Common\Annotations\Annotation\Enum;
- use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
- use Doctrine\Common\Annotations\Annotation\Target;
- use ReflectionClass;
- use ReflectionException;
- use ReflectionProperty;
- use RuntimeException;
- use stdClass;
- use Throwable;
- use function array_keys;
- use function array_map;
- use function array_pop;
- use function array_values;
- use function class_exists;
- use function constant;
- use function count;
- use function defined;
- use function explode;
- use function gettype;
- use function implode;
- use function in_array;
- use function interface_exists;
- use function is_array;
- use function is_object;
- use function json_encode;
- use function ltrim;
- use function preg_match;
- use function reset;
- use function rtrim;
- use function sprintf;
- use function stripos;
- use function strlen;
- use function strpos;
- use function strrpos;
- use function strtolower;
- use function substr;
- use function trim;
- use const PHP_VERSION_ID;
- /**
- * A parser for docblock annotations.
- *
- * It is strongly discouraged to change the default annotation parsing process.
- */
- final class DocParser
- {
- /**
- * An array of all valid tokens for a class name.
- *
- * @phpstan-var list<int>
- */
- private static $classIdentifiers = [
- DocLexer::T_IDENTIFIER,
- DocLexer::T_TRUE,
- DocLexer::T_FALSE,
- DocLexer::T_NULL,
- ];
- /**
- * The lexer.
- *
- * @var DocLexer
- */
- private $lexer;
- /**
- * Current target context.
- *
- * @var int
- */
- private $target;
- /**
- * Doc parser used to collect annotation target.
- *
- * @var DocParser
- */
- private static $metadataParser;
- /**
- * Flag to control if the current annotation is nested or not.
- *
- * @var bool
- */
- private $isNestedAnnotation = false;
- /**
- * Hashmap containing all use-statements that are to be used when parsing
- * the given doc block.
- *
- * @var array<string, class-string>
- */
- private $imports = [];
- /**
- * This hashmap is used internally to cache results of class_exists()
- * look-ups.
- *
- * @var array<class-string, bool>
- */
- private $classExists = [];
- /**
- * Whether annotations that have not been imported should be ignored.
- *
- * @var bool
- */
- private $ignoreNotImportedAnnotations = false;
- /**
- * An array of default namespaces if operating in simple mode.
- *
- * @var string[]
- */
- private $namespaces = [];
- /**
- * A list with annotations that are not causing exceptions when not resolved to an annotation class.
- *
- * The names must be the raw names as used in the class, not the fully qualified
- *
- * @var bool[] indexed by annotation name
- */
- private $ignoredAnnotationNames = [];
- /**
- * A list with annotations in namespaced format
- * that are not causing exceptions when not resolved to an annotation class.
- *
- * @var bool[] indexed by namespace name
- */
- private $ignoredAnnotationNamespaces = [];
- /** @var string */
- private $context = '';
- /**
- * Hash-map for caching annotation metadata.
- *
- * @var array<class-string, mixed[]>
- */
- private static $annotationMetadata = [
- Annotation\Target::class => [
- 'is_annotation' => true,
- 'has_constructor' => true,
- 'has_named_argument_constructor' => false,
- 'properties' => [],
- 'targets_literal' => 'ANNOTATION_CLASS',
- 'targets' => Target::TARGET_CLASS,
- 'default_property' => 'value',
- 'attribute_types' => [
- 'value' => [
- 'required' => false,
- 'type' => 'array',
- 'array_type' => 'string',
- 'value' => 'array<string>',
- ],
- ],
- ],
- Annotation\Attribute::class => [
- 'is_annotation' => true,
- 'has_constructor' => false,
- 'has_named_argument_constructor' => false,
- 'targets_literal' => 'ANNOTATION_ANNOTATION',
- 'targets' => Target::TARGET_ANNOTATION,
- 'default_property' => 'name',
- 'properties' => [
- 'name' => 'name',
- 'type' => 'type',
- 'required' => 'required',
- ],
- 'attribute_types' => [
- 'value' => [
- 'required' => true,
- 'type' => 'string',
- 'value' => 'string',
- ],
- 'type' => [
- 'required' => true,
- 'type' => 'string',
- 'value' => 'string',
- ],
- 'required' => [
- 'required' => false,
- 'type' => 'boolean',
- 'value' => 'boolean',
- ],
- ],
- ],
- Annotation\Attributes::class => [
- 'is_annotation' => true,
- 'has_constructor' => false,
- 'has_named_argument_constructor' => false,
- 'targets_literal' => 'ANNOTATION_CLASS',
- 'targets' => Target::TARGET_CLASS,
- 'default_property' => 'value',
- 'properties' => ['value' => 'value'],
- 'attribute_types' => [
- 'value' => [
- 'type' => 'array',
- 'required' => true,
- 'array_type' => Annotation\Attribute::class,
- 'value' => 'array<' . Annotation\Attribute::class . '>',
- ],
- ],
- ],
- Annotation\Enum::class => [
- 'is_annotation' => true,
- 'has_constructor' => true,
- 'has_named_argument_constructor' => false,
- 'targets_literal' => 'ANNOTATION_PROPERTY',
- 'targets' => Target::TARGET_PROPERTY,
- 'default_property' => 'value',
- 'properties' => ['value' => 'value'],
- 'attribute_types' => [
- 'value' => [
- 'type' => 'array',
- 'required' => true,
- ],
- 'literal' => [
- 'type' => 'array',
- 'required' => false,
- ],
- ],
- ],
- Annotation\NamedArgumentConstructor::class => [
- 'is_annotation' => true,
- 'has_constructor' => false,
- 'has_named_argument_constructor' => false,
- 'targets_literal' => 'ANNOTATION_CLASS',
- 'targets' => Target::TARGET_CLASS,
- 'default_property' => null,
- 'properties' => [],
- 'attribute_types' => [],
- ],
- ];
- /**
- * Hash-map for handle types declaration.
- *
- * @var array<string, string>
- */
- private static $typeMap = [
- 'float' => 'double',
- 'bool' => 'boolean',
- // allow uppercase Boolean in honor of George Boole
- 'Boolean' => 'boolean',
- 'int' => 'integer',
- ];
- /**
- * Constructs a new DocParser.
- */
- public function __construct()
- {
- $this->lexer = new DocLexer();
- }
- /**
- * Sets the annotation names that are ignored during the parsing process.
- *
- * The names are supposed to be the raw names as used in the class, not the
- * fully qualified class names.
- *
- * @param bool[] $names indexed by annotation name
- *
- * @return void
- */
- public function setIgnoredAnnotationNames(array $names)
- {
- $this->ignoredAnnotationNames = $names;
- }
- /**
- * Sets the annotation namespaces that are ignored during the parsing process.
- *
- * @param bool[] $ignoredAnnotationNamespaces indexed by annotation namespace name
- *
- * @return void
- */
- public function setIgnoredAnnotationNamespaces($ignoredAnnotationNamespaces)
- {
- $this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces;
- }
- /**
- * Sets ignore on not-imported annotations.
- *
- * @param bool $bool
- *
- * @return void
- */
- public function setIgnoreNotImportedAnnotations($bool)
- {
- $this->ignoreNotImportedAnnotations = (bool) $bool;
- }
- /**
- * Sets the default namespaces.
- *
- * @param string $namespace
- *
- * @return void
- *
- * @throws RuntimeException
- */
- public function addNamespace($namespace)
- {
- if ($this->imports) {
- throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.');
- }
- $this->namespaces[] = $namespace;
- }
- /**
- * Sets the imports.
- *
- * @param array<string, class-string> $imports
- *
- * @return void
- *
- * @throws RuntimeException
- */
- public function setImports(array $imports)
- {
- if ($this->namespaces) {
- throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.');
- }
- $this->imports = $imports;
- }
- /**
- * Sets current target context as bitmask.
- *
- * @param int $target
- *
- * @return void
- */
- public function setTarget($target)
- {
- $this->target = $target;
- }
- /**
- * Parses the given docblock string for annotations.
- *
- * @param string $input The docblock string to parse.
- * @param string $context The parsing context.
- *
- * @phpstan-return list<object> Array of annotations. If no annotations are found, an empty array is returned.
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- public function parse($input, $context = '')
- {
- $pos = $this->findInitialTokenPosition($input);
- if ($pos === null) {
- return [];
- }
- $this->context = $context;
- $this->lexer->setInput(trim(substr($input, $pos), '* /'));
- $this->lexer->moveNext();
- return $this->Annotations();
- }
- /**
- * Finds the first valid annotation
- *
- * @param string $input The docblock string to parse
- */
- private function findInitialTokenPosition($input): ?int
- {
- $pos = 0;
- // search for first valid annotation
- while (($pos = strpos($input, '@', $pos)) !== false) {
- $preceding = substr($input, $pos - 1, 1);
- // if the @ is preceded by a space, a tab or * it is valid
- if ($pos === 0 || $preceding === ' ' || $preceding === '*' || $preceding === "\t") {
- return $pos;
- }
- $pos++;
- }
- return null;
- }
- /**
- * Attempts to match the given token with the current lookahead token.
- * If they match, updates the lookahead token; otherwise raises a syntax error.
- *
- * @param int $token Type of token.
- *
- * @return bool True if tokens match; false otherwise.
- *
- * @throws AnnotationException
- */
- private function match(int $token): bool
- {
- if (! $this->lexer->isNextToken($token)) {
- throw $this->syntaxError($this->lexer->getLiteral($token));
- }
- return $this->lexer->moveNext();
- }
- /**
- * Attempts to match the current lookahead token with any of the given tokens.
- *
- * If any of them matches, this method updates the lookahead token; otherwise
- * a syntax error is raised.
- *
- * @phpstan-param list<mixed[]> $tokens
- *
- * @throws AnnotationException
- */
- private function matchAny(array $tokens): bool
- {
- if (! $this->lexer->isNextTokenAny($tokens)) {
- throw $this->syntaxError(implode(' or ', array_map([$this->lexer, 'getLiteral'], $tokens)));
- }
- return $this->lexer->moveNext();
- }
- /**
- * Generates a new syntax error.
- *
- * @param string $expected Expected string.
- * @param mixed[]|null $token Optional token.
- */
- private function syntaxError(string $expected, ?array $token = null): AnnotationException
- {
- if ($token === null) {
- $token = $this->lexer->lookahead;
- }
- $message = sprintf('Expected %s, got ', $expected);
- $message .= $this->lexer->lookahead === null
- ? 'end of string'
- : sprintf("'%s' at position %s", $token['value'], $token['position']);
- if (strlen($this->context)) {
- $message .= ' in ' . $this->context;
- }
- $message .= '.';
- return AnnotationException::syntaxError($message);
- }
- /**
- * Attempts to check if a class exists or not. This never goes through the PHP autoloading mechanism
- * but uses the {@link AnnotationRegistry} to load classes.
- *
- * @param class-string $fqcn
- */
- private function classExists(string $fqcn): bool
- {
- if (isset($this->classExists[$fqcn])) {
- return $this->classExists[$fqcn];
- }
- // first check if the class already exists, maybe loaded through another AnnotationReader
- if (class_exists($fqcn, false)) {
- return $this->classExists[$fqcn] = true;
- }
- // final check, does this class exist?
- return $this->classExists[$fqcn] = AnnotationRegistry::loadAnnotationClass($fqcn);
- }
- /**
- * Collects parsing metadata for a given annotation class
- *
- * @param class-string $name The annotation name
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function collectAnnotationMetadata(string $name): void
- {
- if (self::$metadataParser === null) {
- self::$metadataParser = new self();
- self::$metadataParser->setIgnoreNotImportedAnnotations(true);
- self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames);
- self::$metadataParser->setImports([
- 'enum' => Enum::class,
- 'target' => Target::class,
- 'attribute' => Attribute::class,
- 'attributes' => Attributes::class,
- 'namedargumentconstructor' => NamedArgumentConstructor::class,
- ]);
- // Make sure that annotations from metadata are loaded
- class_exists(Enum::class);
- class_exists(Target::class);
- class_exists(Attribute::class);
- class_exists(Attributes::class);
- class_exists(NamedArgumentConstructor::class);
- }
- $class = new ReflectionClass($name);
- $docComment = $class->getDocComment();
- // Sets default values for annotation metadata
- $constructor = $class->getConstructor();
- $metadata = [
- 'default_property' => null,
- 'has_constructor' => $constructor !== null && $constructor->getNumberOfParameters() > 0,
- 'constructor_args' => [],
- 'properties' => [],
- 'property_types' => [],
- 'attribute_types' => [],
- 'targets_literal' => null,
- 'targets' => Target::TARGET_ALL,
- 'is_annotation' => strpos($docComment, '@Annotation') !== false,
- ];
- $metadata['has_named_argument_constructor'] = $metadata['has_constructor']
- && $class->implementsInterface(NamedArgumentConstructorAnnotation::class);
- // verify that the class is really meant to be an annotation
- if ($metadata['is_annotation']) {
- self::$metadataParser->setTarget(Target::TARGET_CLASS);
- foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) {
- if ($annotation instanceof Target) {
- $metadata['targets'] = $annotation->targets;
- $metadata['targets_literal'] = $annotation->literal;
- continue;
- }
- if ($annotation instanceof NamedArgumentConstructor) {
- $metadata['has_named_argument_constructor'] = $metadata['has_constructor'];
- if ($metadata['has_named_argument_constructor']) {
- // choose the first argument as the default property
- $metadata['default_property'] = $constructor->getParameters()[0]->getName();
- }
- }
- if (! ($annotation instanceof Attributes)) {
- continue;
- }
- foreach ($annotation->value as $attribute) {
- $this->collectAttributeTypeMetadata($metadata, $attribute);
- }
- }
- // if not has a constructor will inject values into public properties
- if ($metadata['has_constructor'] === false) {
- // collect all public properties
- foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
- $metadata['properties'][$property->name] = $property->name;
- $propertyComment = $property->getDocComment();
- if ($propertyComment === false) {
- continue;
- }
- $attribute = new Attribute();
- $attribute->required = (strpos($propertyComment, '@Required') !== false);
- $attribute->name = $property->name;
- $attribute->type = (strpos($propertyComment, '@var') !== false &&
- preg_match('/@var\s+([^\s]+)/', $propertyComment, $matches))
- ? $matches[1]
- : 'mixed';
- $this->collectAttributeTypeMetadata($metadata, $attribute);
- // checks if the property has @Enum
- if (strpos($propertyComment, '@Enum') === false) {
- continue;
- }
- $context = 'property ' . $class->name . '::$' . $property->name;
- self::$metadataParser->setTarget(Target::TARGET_PROPERTY);
- foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) {
- if (! $annotation instanceof Enum) {
- continue;
- }
- $metadata['enum'][$property->name]['value'] = $annotation->value;
- $metadata['enum'][$property->name]['literal'] = (! empty($annotation->literal))
- ? $annotation->literal
- : $annotation->value;
- }
- }
- // choose the first property as default property
- $metadata['default_property'] = reset($metadata['properties']);
- } elseif ($metadata['has_named_argument_constructor']) {
- foreach ($constructor->getParameters() as $parameter) {
- if ($parameter->isVariadic()) {
- break;
- }
- $metadata['constructor_args'][$parameter->getName()] = [
- 'position' => $parameter->getPosition(),
- 'default' => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
- ];
- }
- }
- }
- self::$annotationMetadata[$name] = $metadata;
- }
- /**
- * Collects parsing metadata for a given attribute.
- *
- * @param mixed[] $metadata
- */
- private function collectAttributeTypeMetadata(array &$metadata, Attribute $attribute): void
- {
- // handle internal type declaration
- $type = self::$typeMap[$attribute->type] ?? $attribute->type;
- // handle the case if the property type is mixed
- if ($type === 'mixed') {
- return;
- }
- // Evaluate type
- $pos = strpos($type, '<');
- if ($pos !== false) {
- // Checks if the property has array<type>
- $arrayType = substr($type, $pos + 1, -1);
- $type = 'array';
- if (isset(self::$typeMap[$arrayType])) {
- $arrayType = self::$typeMap[$arrayType];
- }
- $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType;
- } else {
- // Checks if the property has type[]
- $pos = strrpos($type, '[');
- if ($pos !== false) {
- $arrayType = substr($type, 0, $pos);
- $type = 'array';
- if (isset(self::$typeMap[$arrayType])) {
- $arrayType = self::$typeMap[$arrayType];
- }
- $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType;
- }
- }
- $metadata['attribute_types'][$attribute->name]['type'] = $type;
- $metadata['attribute_types'][$attribute->name]['value'] = $attribute->type;
- $metadata['attribute_types'][$attribute->name]['required'] = $attribute->required;
- }
- /**
- * Annotations ::= Annotation {[ "*" ]* [Annotation]}*
- *
- * @phpstan-return list<object>
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function Annotations(): array
- {
- $annotations = [];
- while ($this->lexer->lookahead !== null) {
- if ($this->lexer->lookahead['type'] !== DocLexer::T_AT) {
- $this->lexer->moveNext();
- continue;
- }
- // make sure the @ is preceded by non-catchable pattern
- if (
- $this->lexer->token !== null &&
- $this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen(
- $this->lexer->token['value']
- )
- ) {
- $this->lexer->moveNext();
- continue;
- }
- // make sure the @ is followed by either a namespace separator, or
- // an identifier token
- $peek = $this->lexer->glimpse();
- if (
- ($peek === null)
- || ($peek['type'] !== DocLexer::T_NAMESPACE_SEPARATOR && ! in_array(
- $peek['type'],
- self::$classIdentifiers,
- true
- ))
- || $peek['position'] !== $this->lexer->lookahead['position'] + 1
- ) {
- $this->lexer->moveNext();
- continue;
- }
- $this->isNestedAnnotation = false;
- $annot = $this->Annotation();
- if ($annot === false) {
- continue;
- }
- $annotations[] = $annot;
- }
- return $annotations;
- }
- /**
- * Annotation ::= "@" AnnotationName MethodCall
- * AnnotationName ::= QualifiedName | SimpleName
- * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName
- * NameSpacePart ::= identifier | null | false | true
- * SimpleName ::= identifier | null | false | true
- *
- * @return object|false False if it is not a valid annotation.
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function Annotation()
- {
- $this->match(DocLexer::T_AT);
- // check if we have an annotation
- $name = $this->Identifier();
- if (
- $this->lexer->isNextToken(DocLexer::T_MINUS)
- && $this->lexer->nextTokenIsAdjacent()
- ) {
- // Annotations with dashes, such as "@foo-" or "@foo-bar", are to be discarded
- return false;
- }
- // only process names which are not fully qualified, yet
- // fully qualified names must start with a \
- $originalName = $name;
- if ($name[0] !== '\\') {
- $pos = strpos($name, '\\');
- $alias = ($pos === false) ? $name : substr($name, 0, $pos);
- $found = false;
- $loweredAlias = strtolower($alias);
- if ($this->namespaces) {
- foreach ($this->namespaces as $namespace) {
- if ($this->classExists($namespace . '\\' . $name)) {
- $name = $namespace . '\\' . $name;
- $found = true;
- break;
- }
- }
- } elseif (isset($this->imports[$loweredAlias])) {
- $namespace = ltrim($this->imports[$loweredAlias], '\\');
- $name = ($pos !== false)
- ? $namespace . substr($name, $pos)
- : $namespace;
- $found = $this->classExists($name);
- } elseif (
- ! isset($this->ignoredAnnotationNames[$name])
- && isset($this->imports['__NAMESPACE__'])
- && $this->classExists($this->imports['__NAMESPACE__'] . '\\' . $name)
- ) {
- $name = $this->imports['__NAMESPACE__'] . '\\' . $name;
- $found = true;
- } elseif (! isset($this->ignoredAnnotationNames[$name]) && $this->classExists($name)) {
- $found = true;
- }
- if (! $found) {
- if ($this->isIgnoredAnnotation($name)) {
- return false;
- }
- throw AnnotationException::semanticalError(sprintf(
- <<<'EXCEPTION'
- The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?
- EXCEPTION
- ,
- $name,
- $this->context
- ));
- }
- }
- $name = ltrim($name, '\\');
- if (! $this->classExists($name)) {
- throw AnnotationException::semanticalError(sprintf(
- 'The annotation "@%s" in %s does not exist, or could not be auto-loaded.',
- $name,
- $this->context
- ));
- }
- // at this point, $name contains the fully qualified class name of the
- // annotation, and it is also guaranteed that this class exists, and
- // that it is loaded
- // collects the metadata annotation only if there is not yet
- if (! isset(self::$annotationMetadata[$name])) {
- $this->collectAnnotationMetadata($name);
- }
- // verify that the class is really meant to be an annotation and not just any ordinary class
- if (self::$annotationMetadata[$name]['is_annotation'] === false) {
- if ($this->isIgnoredAnnotation($originalName) || $this->isIgnoredAnnotation($name)) {
- return false;
- }
- throw AnnotationException::semanticalError(sprintf(
- <<<'EXCEPTION'
- The class "%s" is not annotated with @Annotation.
- Are you sure this class can be used as annotation?
- If so, then you need to add @Annotation to the _class_ doc comment of "%s".
- If it is indeed no annotation, then you need to add @IgnoreAnnotation("%s") to the _class_ doc comment of %s.
- EXCEPTION
- ,
- $name,
- $name,
- $originalName,
- $this->context
- ));
- }
- //if target is nested annotation
- $target = $this->isNestedAnnotation ? Target::TARGET_ANNOTATION : $this->target;
- // Next will be nested
- $this->isNestedAnnotation = true;
- //if annotation does not support current target
- if ((self::$annotationMetadata[$name]['targets'] & $target) === 0 && $target) {
- throw AnnotationException::semanticalError(
- sprintf(
- <<<'EXCEPTION'
- Annotation @%s is not allowed to be declared on %s. You may only use this annotation on these code elements: %s.
- EXCEPTION
- ,
- $originalName,
- $this->context,
- self::$annotationMetadata[$name]['targets_literal']
- )
- );
- }
- $arguments = $this->MethodCall();
- $values = $this->resolvePositionalValues($arguments, $name);
- if (isset(self::$annotationMetadata[$name]['enum'])) {
- // checks all declared attributes
- foreach (self::$annotationMetadata[$name]['enum'] as $property => $enum) {
- // checks if the attribute is a valid enumerator
- if (isset($values[$property]) && ! in_array($values[$property], $enum['value'])) {
- throw AnnotationException::enumeratorError(
- $property,
- $name,
- $this->context,
- $enum['literal'],
- $values[$property]
- );
- }
- }
- }
- // checks all declared attributes
- foreach (self::$annotationMetadata[$name]['attribute_types'] as $property => $type) {
- if (
- $property === self::$annotationMetadata[$name]['default_property']
- && ! isset($values[$property]) && isset($values['value'])
- ) {
- $property = 'value';
- }
- // handle a not given attribute or null value
- if (! isset($values[$property])) {
- if ($type['required']) {
- throw AnnotationException::requiredError(
- $property,
- $originalName,
- $this->context,
- 'a(n) ' . $type['value']
- );
- }
- continue;
- }
- if ($type['type'] === 'array') {
- // handle the case of a single value
- if (! is_array($values[$property])) {
- $values[$property] = [$values[$property]];
- }
- // checks if the attribute has array type declaration, such as "array<string>"
- if (isset($type['array_type'])) {
- foreach ($values[$property] as $item) {
- if (gettype($item) !== $type['array_type'] && ! $item instanceof $type['array_type']) {
- throw AnnotationException::attributeTypeError(
- $property,
- $originalName,
- $this->context,
- 'either a(n) ' . $type['array_type'] . ', or an array of ' . $type['array_type'] . 's',
- $item
- );
- }
- }
- }
- } elseif (gettype($values[$property]) !== $type['type'] && ! $values[$property] instanceof $type['type']) {
- throw AnnotationException::attributeTypeError(
- $property,
- $originalName,
- $this->context,
- 'a(n) ' . $type['value'],
- $values[$property]
- );
- }
- }
- if (self::$annotationMetadata[$name]['has_named_argument_constructor']) {
- if (PHP_VERSION_ID >= 80000) {
- foreach ($values as $property => $value) {
- if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) {
- throw AnnotationException::creationError(sprintf(
- <<<'EXCEPTION'
- The annotation @%s declared on %s does not have a property named "%s"
- that can be set through its named arguments constructor.
- Available named arguments: %s
- EXCEPTION
- ,
- $originalName,
- $this->context,
- $property,
- implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args']))
- ));
- }
- }
- return $this->instantiateAnnotiation($originalName, $this->context, $name, $values);
- }
- $positionalValues = [];
- foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) {
- $positionalValues[$parameter['position']] = $parameter['default'];
- }
- foreach ($values as $property => $value) {
- if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) {
- throw AnnotationException::creationError(sprintf(
- <<<'EXCEPTION'
- The annotation @%s declared on %s does not have a property named "%s"
- that can be set through its named arguments constructor.
- Available named arguments: %s
- EXCEPTION
- ,
- $originalName,
- $this->context,
- $property,
- implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args']))
- ));
- }
- $positionalValues[self::$annotationMetadata[$name]['constructor_args'][$property]['position']] = $value;
- }
- return $this->instantiateAnnotiation($originalName, $this->context, $name, $positionalValues);
- }
- // check if the annotation expects values via the constructor,
- // or directly injected into public properties
- if (self::$annotationMetadata[$name]['has_constructor'] === true) {
- return $this->instantiateAnnotiation($originalName, $this->context, $name, [$values]);
- }
- $instance = $this->instantiateAnnotiation($originalName, $this->context, $name, []);
- foreach ($values as $property => $value) {
- if (! isset(self::$annotationMetadata[$name]['properties'][$property])) {
- if ($property !== 'value') {
- throw AnnotationException::creationError(sprintf(
- <<<'EXCEPTION'
- The annotation @%s declared on %s does not have a property named "%s".
- Available properties: %s
- EXCEPTION
- ,
- $originalName,
- $this->context,
- $property,
- implode(', ', self::$annotationMetadata[$name]['properties'])
- ));
- }
- // handle the case if the property has no annotations
- $property = self::$annotationMetadata[$name]['default_property'];
- if (! $property) {
- throw AnnotationException::creationError(sprintf(
- 'The annotation @%s declared on %s does not accept any values, but got %s.',
- $originalName,
- $this->context,
- json_encode($values)
- ));
- }
- }
- $instance->{$property} = $value;
- }
- return $instance;
- }
- /**
- * MethodCall ::= ["(" [Values] ")"]
- *
- * @return mixed[]
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function MethodCall(): array
- {
- $values = [];
- if (! $this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) {
- return $values;
- }
- $this->match(DocLexer::T_OPEN_PARENTHESIS);
- if (! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) {
- $values = $this->Values();
- }
- $this->match(DocLexer::T_CLOSE_PARENTHESIS);
- return $values;
- }
- /**
- * Values ::= Array | Value {"," Value}* [","]
- *
- * @return mixed[]
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function Values(): array
- {
- $values = [$this->Value()];
- while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
- $this->match(DocLexer::T_COMMA);
- if ($this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) {
- break;
- }
- $token = $this->lexer->lookahead;
- $value = $this->Value();
- $values[] = $value;
- }
- $namedArguments = [];
- $positionalArguments = [];
- foreach ($values as $k => $value) {
- if (is_object($value) && $value instanceof stdClass) {
- $namedArguments[$value->name] = $value->value;
- } else {
- $positionalArguments[$k] = $value;
- }
- }
- return ['named_arguments' => $namedArguments, 'positional_arguments' => $positionalArguments];
- }
- /**
- * Constant ::= integer | string | float | boolean
- *
- * @return mixed
- *
- * @throws AnnotationException
- */
- private function Constant()
- {
- $identifier = $this->Identifier();
- if (! defined($identifier) && strpos($identifier, '::') !== false && $identifier[0] !== '\\') {
- [$className, $const] = explode('::', $identifier);
- $pos = strpos($className, '\\');
- $alias = ($pos === false) ? $className : substr($className, 0, $pos);
- $found = false;
- $loweredAlias = strtolower($alias);
- switch (true) {
- case ! empty($this->namespaces):
- foreach ($this->namespaces as $ns) {
- if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) {
- $className = $ns . '\\' . $className;
- $found = true;
- break;
- }
- }
- break;
- case isset($this->imports[$loweredAlias]):
- $found = true;
- $className = ($pos !== false)
- ? $this->imports[$loweredAlias] . substr($className, $pos)
- : $this->imports[$loweredAlias];
- break;
- default:
- if (isset($this->imports['__NAMESPACE__'])) {
- $ns = $this->imports['__NAMESPACE__'];
- if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) {
- $className = $ns . '\\' . $className;
- $found = true;
- }
- }
- break;
- }
- if ($found) {
- $identifier = $className . '::' . $const;
- }
- }
- /**
- * Checks if identifier ends with ::class and remove the leading backslash if it exists.
- */
- if (
- $this->identifierEndsWithClassConstant($identifier) &&
- ! $this->identifierStartsWithBackslash($identifier)
- ) {
- return substr($identifier, 0, $this->getClassConstantPositionInIdentifier($identifier));
- }
- if ($this->identifierEndsWithClassConstant($identifier) && $this->identifierStartsWithBackslash($identifier)) {
- return substr($identifier, 1, $this->getClassConstantPositionInIdentifier($identifier) - 1);
- }
- if (! defined($identifier)) {
- throw AnnotationException::semanticalErrorConstants($identifier, $this->context);
- }
- return constant($identifier);
- }
- private function identifierStartsWithBackslash(string $identifier): bool
- {
- return $identifier[0] === '\\';
- }
- private function identifierEndsWithClassConstant(string $identifier): bool
- {
- return $this->getClassConstantPositionInIdentifier($identifier) === strlen($identifier) - strlen('::class');
- }
- /** @return int|false */
- private function getClassConstantPositionInIdentifier(string $identifier)
- {
- return stripos($identifier, '::class');
- }
- /**
- * Identifier ::= string
- *
- * @throws AnnotationException
- */
- private function Identifier(): string
- {
- // check if we have an annotation
- if (! $this->lexer->isNextTokenAny(self::$classIdentifiers)) {
- throw $this->syntaxError('namespace separator or identifier');
- }
- $this->lexer->moveNext();
- $className = $this->lexer->token['value'];
- while (
- $this->lexer->lookahead !== null &&
- $this->lexer->lookahead['position'] === ($this->lexer->token['position'] +
- strlen($this->lexer->token['value'])) &&
- $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)
- ) {
- $this->match(DocLexer::T_NAMESPACE_SEPARATOR);
- $this->matchAny(self::$classIdentifiers);
- $className .= '\\' . $this->lexer->token['value'];
- }
- return $className;
- }
- /**
- * Value ::= PlainValue | FieldAssignment
- *
- * @return mixed
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function Value()
- {
- $peek = $this->lexer->glimpse();
- if ($peek['type'] === DocLexer::T_EQUALS) {
- return $this->FieldAssignment();
- }
- return $this->PlainValue();
- }
- /**
- * PlainValue ::= integer | string | float | boolean | Array | Annotation
- *
- * @return mixed
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function PlainValue()
- {
- if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) {
- return $this->Arrayx();
- }
- if ($this->lexer->isNextToken(DocLexer::T_AT)) {
- return $this->Annotation();
- }
- if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) {
- return $this->Constant();
- }
- switch ($this->lexer->lookahead['type']) {
- case DocLexer::T_STRING:
- $this->match(DocLexer::T_STRING);
- return $this->lexer->token['value'];
- case DocLexer::T_INTEGER:
- $this->match(DocLexer::T_INTEGER);
- return (int) $this->lexer->token['value'];
- case DocLexer::T_FLOAT:
- $this->match(DocLexer::T_FLOAT);
- return (float) $this->lexer->token['value'];
- case DocLexer::T_TRUE:
- $this->match(DocLexer::T_TRUE);
- return true;
- case DocLexer::T_FALSE:
- $this->match(DocLexer::T_FALSE);
- return false;
- case DocLexer::T_NULL:
- $this->match(DocLexer::T_NULL);
- return null;
- default:
- throw $this->syntaxError('PlainValue');
- }
- }
- /**
- * FieldAssignment ::= FieldName "=" PlainValue
- * FieldName ::= identifier
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function FieldAssignment(): stdClass
- {
- $this->match(DocLexer::T_IDENTIFIER);
- $fieldName = $this->lexer->token['value'];
- $this->match(DocLexer::T_EQUALS);
- $item = new stdClass();
- $item->name = $fieldName;
- $item->value = $this->PlainValue();
- return $item;
- }
- /**
- * Array ::= "{" ArrayEntry {"," ArrayEntry}* [","] "}"
- *
- * @return mixed[]
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function Arrayx(): array
- {
- $array = $values = [];
- $this->match(DocLexer::T_OPEN_CURLY_BRACES);
- // If the array is empty, stop parsing and return.
- if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) {
- $this->match(DocLexer::T_CLOSE_CURLY_BRACES);
- return $array;
- }
- $values[] = $this->ArrayEntry();
- while ($this->lexer->isNextToken(DocLexer::T_COMMA)) {
- $this->match(DocLexer::T_COMMA);
- // optional trailing comma
- if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) {
- break;
- }
- $values[] = $this->ArrayEntry();
- }
- $this->match(DocLexer::T_CLOSE_CURLY_BRACES);
- foreach ($values as $value) {
- [$key, $val] = $value;
- if ($key !== null) {
- $array[$key] = $val;
- } else {
- $array[] = $val;
- }
- }
- return $array;
- }
- /**
- * ArrayEntry ::= Value | KeyValuePair
- * KeyValuePair ::= Key ("=" | ":") PlainValue | Constant
- * Key ::= string | integer | Constant
- *
- * @phpstan-return array{mixed, mixed}
- *
- * @throws AnnotationException
- * @throws ReflectionException
- */
- private function ArrayEntry(): array
- {
- $peek = $this->lexer->glimpse();
- if (
- $peek['type'] === DocLexer::T_EQUALS
- || $peek['type'] === DocLexer::T_COLON
- ) {
- if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) {
- $key = $this->Constant();
- } else {
- $this->matchAny([DocLexer::T_INTEGER, DocLexer::T_STRING]);
- $key = $this->lexer->token['value'];
- }
- $this->matchAny([DocLexer::T_EQUALS, DocLexer::T_COLON]);
- return [$key, $this->PlainValue()];
- }
- return [null, $this->Value()];
- }
- /**
- * Checks whether the given $name matches any ignored annotation name or namespace
- */
- private function isIgnoredAnnotation(string $name): bool
- {
- if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
- return true;
- }
- foreach (array_keys($this->ignoredAnnotationNamespaces) as $ignoredAnnotationNamespace) {
- $ignoredAnnotationNamespace = rtrim($ignoredAnnotationNamespace, '\\') . '\\';
- if (stripos(rtrim($name, '\\') . '\\', $ignoredAnnotationNamespace) === 0) {
- return true;
- }
- }
- return false;
- }
- /**
- * Resolve positional arguments (without name) to named ones
- *
- * @param array<string,mixed> $arguments
- *
- * @return array<string,mixed>
- */
- private function resolvePositionalValues(array $arguments, string $name): array
- {
- $positionalArguments = $arguments['positional_arguments'] ?? [];
- $values = $arguments['named_arguments'] ?? [];
- if (
- self::$annotationMetadata[$name]['has_named_argument_constructor']
- && self::$annotationMetadata[$name]['default_property'] !== null
- ) {
- // We must ensure that we don't have positional arguments after named ones
- $positions = array_keys($positionalArguments);
- $lastPosition = null;
- foreach ($positions as $position) {
- if (
- ($lastPosition === null && $position !== 0) ||
- ($lastPosition !== null && $position !== $lastPosition + 1)
- ) {
- throw $this->syntaxError('Positional arguments after named arguments is not allowed');
- }
- $lastPosition = $position;
- }
- foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) {
- $position = $parameter['position'];
- if (isset($values[$property]) || ! isset($positionalArguments[$position])) {
- continue;
- }
- $values[$property] = $positionalArguments[$position];
- }
- } else {
- if (count($positionalArguments) > 0 && ! isset($values['value'])) {
- if (count($positionalArguments) === 1) {
- $value = array_pop($positionalArguments);
- } else {
- $value = array_values($positionalArguments);
- }
- $values['value'] = $value;
- }
- }
- return $values;
- }
- /**
- * Try to instantiate the annotation and catch and process any exceptions related to failure
- *
- * @param class-string $name
- * @param array<string,mixed> $arguments
- *
- * @return object
- *
- * @throws AnnotationException
- */
- private function instantiateAnnotiation(string $originalName, string $context, string $name, array $arguments)
- {
- try {
- return new $name(...$arguments);
- } catch (Throwable $exception) {
- throw AnnotationException::creationError(
- sprintf(
- 'An error occurred while instantiating the annotation @%s declared on %s: "%s".',
- $originalName,
- $context,
- $exception->getMessage()
- ),
- $exception
- );
- }
- }
- }
|