| Total Complexity | 178 |
| Total Lines | 1109 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DocParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | final class DocParser |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * An array of all valid tokens for a class name. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private static $classIdentifiers = [ |
||
| 39 | DocLexer::T_IDENTIFIER, |
||
| 40 | DocLexer::T_TRUE, |
||
| 41 | DocLexer::T_FALSE, |
||
| 42 | DocLexer::T_NULL |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The lexer. |
||
| 47 | * |
||
| 48 | * @var \Doctrine\Annotations\DocLexer |
||
| 49 | */ |
||
| 50 | private $lexer; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Current target context. |
||
| 54 | * |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | private $target; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Doc parser used to collect annotation target. |
||
| 61 | * |
||
| 62 | * @var \Doctrine\Annotations\DocParser |
||
| 63 | */ |
||
| 64 | private static $metadataParser; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Flag to control if the current annotation is nested or not. |
||
| 68 | * |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | private $isNestedAnnotation = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Hashmap containing all use-statements that are to be used when parsing |
||
| 75 | * the given doc block. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $imports = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * This hashmap is used internally to cache results of class_exists() |
||
| 83 | * look-ups. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $classExists = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Whether annotations that have not been imported should be ignored. |
||
| 91 | * |
||
| 92 | * @var boolean |
||
| 93 | */ |
||
| 94 | private $ignoreNotImportedAnnotations = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * An array of default namespaces if operating in simple mode. |
||
| 98 | * |
||
| 99 | * @var string[] |
||
| 100 | */ |
||
| 101 | private $namespaces = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * A list with annotations that are not causing exceptions when not resolved to an annotation class. |
||
| 105 | * |
||
| 106 | * The names must be the raw names as used in the class, not the fully qualified |
||
| 107 | * class names. |
||
| 108 | * |
||
| 109 | * @var bool[] indexed by annotation name |
||
| 110 | */ |
||
| 111 | private $ignoredAnnotationNames = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * A list with annotations in namespaced format |
||
| 115 | * that are not causing exceptions when not resolved to an annotation class. |
||
| 116 | * |
||
| 117 | * @var bool[] indexed by namespace name |
||
| 118 | */ |
||
| 119 | private $ignoredAnnotationNamespaces = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private $context = ''; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Hash-map for caching annotation metadata. |
||
| 128 | * |
||
| 129 | * @var MetadataCollection |
||
| 130 | */ |
||
| 131 | private $metadata; |
||
| 132 | |||
| 133 | /** @var array<string, bool> */ |
||
| 134 | private $nonAnnotationClasses = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Hash-map for handle types declaration. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private static $typeMap = [ |
||
| 142 | 'float' => 'double', |
||
| 143 | 'bool' => 'boolean', |
||
| 144 | // allow uppercase Boolean in honor of George Boole |
||
| 145 | 'Boolean' => 'boolean', |
||
| 146 | 'int' => 'integer', |
||
| 147 | ]; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Constructs a new DocParser. |
||
| 151 | */ |
||
| 152 | public function __construct() |
||
| 153 | { |
||
| 154 | $this->lexer = new DocLexer; |
||
| 155 | $this->metadata = InternalAnnotations::createMetadata(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the annotation names that are ignored during the parsing process. |
||
| 160 | * |
||
| 161 | * The names are supposed to be the raw names as used in the class, not the |
||
| 162 | * fully qualified class names. |
||
| 163 | * |
||
| 164 | * @param bool[] $names indexed by annotation name |
||
| 165 | * |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public function setIgnoredAnnotationNames(array $names) |
||
| 169 | { |
||
| 170 | $this->ignoredAnnotationNames = $names; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets the annotation namespaces that are ignored during the parsing process. |
||
| 175 | * |
||
| 176 | * @param bool[] $ignoredAnnotationNamespaces indexed by annotation namespace name |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function setIgnoredAnnotationNamespaces($ignoredAnnotationNamespaces) |
||
| 181 | { |
||
| 182 | $this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Sets ignore on not-imported annotations. |
||
| 187 | * |
||
| 188 | * @param boolean $bool |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function setIgnoreNotImportedAnnotations($bool) |
||
| 193 | { |
||
| 194 | $this->ignoreNotImportedAnnotations = (boolean) $bool; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Sets the default namespaces. |
||
| 199 | * |
||
| 200 | * @param string $namespace |
||
| 201 | * |
||
| 202 | * @return void |
||
| 203 | * |
||
| 204 | * @throws \RuntimeException |
||
| 205 | */ |
||
| 206 | public function addNamespace($namespace) |
||
| 207 | { |
||
| 208 | if ($this->imports) { |
||
|
|
|||
| 209 | throw new \RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); |
||
| 210 | } |
||
| 211 | |||
| 212 | $this->namespaces[] = $namespace; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets the imports. |
||
| 217 | * |
||
| 218 | * @param array $imports |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | * |
||
| 222 | * @throws \RuntimeException |
||
| 223 | */ |
||
| 224 | public function setImports(array $imports) |
||
| 225 | { |
||
| 226 | if ($this->namespaces) { |
||
| 227 | throw new \RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); |
||
| 228 | } |
||
| 229 | |||
| 230 | $this->imports = $imports; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sets current target context as bitmask. |
||
| 235 | * |
||
| 236 | * @param integer $target |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function setTarget($target) |
||
| 241 | { |
||
| 242 | $this->target = $target; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Parses the given docblock string for annotations. |
||
| 247 | * |
||
| 248 | * @param string $input The docblock string to parse. |
||
| 249 | * @param string $context The parsing context. |
||
| 250 | * |
||
| 251 | * @return array Array of annotations. If no annotations are found, an empty array is returned. |
||
| 252 | */ |
||
| 253 | public function parse($input, $context = '') |
||
| 254 | { |
||
| 255 | $pos = $this->findInitialTokenPosition($input); |
||
| 256 | if ($pos === null) { |
||
| 257 | return []; |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->context = $context; |
||
| 261 | |||
| 262 | $this->lexer->setInput(trim(substr($input, $pos), '* /')); |
||
| 263 | $this->lexer->moveNext(); |
||
| 264 | |||
| 265 | return $this->Annotations(); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Finds the first valid annotation |
||
| 270 | * |
||
| 271 | * @param string $input The docblock string to parse |
||
| 272 | * |
||
| 273 | * @return int|null |
||
| 274 | */ |
||
| 275 | private function findInitialTokenPosition($input) |
||
| 276 | { |
||
| 277 | $pos = 0; |
||
| 278 | |||
| 279 | // search for first valid annotation |
||
| 280 | while (($pos = strpos($input, '@', $pos)) !== false) { |
||
| 281 | $preceding = substr($input, $pos - 1, 1); |
||
| 282 | |||
| 283 | // if the @ is preceded by a space, a tab or * it is valid |
||
| 284 | if ($pos === 0 || $preceding === ' ' || $preceding === '*' || $preceding === "\t") { |
||
| 285 | return $pos; |
||
| 286 | } |
||
| 287 | |||
| 288 | $pos++; |
||
| 289 | } |
||
| 290 | |||
| 291 | return null; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Attempts to match the given token with the current lookahead token. |
||
| 296 | * If they match, updates the lookahead token; otherwise raises a syntax error. |
||
| 297 | * |
||
| 298 | * @param integer $token Type of token. |
||
| 299 | * |
||
| 300 | * @return boolean True if tokens match; false otherwise. |
||
| 301 | */ |
||
| 302 | private function match($token) |
||
| 303 | { |
||
| 304 | if ( ! $this->lexer->isNextToken($token) ) { |
||
| 305 | $this->syntaxError($this->lexer->getLiteral($token)); |
||
| 306 | } |
||
| 307 | |||
| 308 | return $this->lexer->moveNext(); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Attempts to match the current lookahead token with any of the given tokens. |
||
| 313 | * |
||
| 314 | * If any of them matches, this method updates the lookahead token; otherwise |
||
| 315 | * a syntax error is raised. |
||
| 316 | * |
||
| 317 | * @param array $tokens |
||
| 318 | * |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | private function matchAny(array $tokens) |
||
| 322 | { |
||
| 323 | if ( ! $this->lexer->isNextTokenAny($tokens)) { |
||
| 324 | $this->syntaxError(implode(' or ', array_map([$this->lexer, 'getLiteral'], $tokens))); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $this->lexer->moveNext(); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Generates a new syntax error. |
||
| 332 | * |
||
| 333 | * @param string $expected Expected string. |
||
| 334 | * @param array|null $token Optional token. |
||
| 335 | * |
||
| 336 | * @return void |
||
| 337 | * |
||
| 338 | * @throws AnnotationException |
||
| 339 | */ |
||
| 340 | private function syntaxError($expected, $token = null) |
||
| 341 | { |
||
| 342 | if ($token === null) { |
||
| 343 | $token = $this->lexer->lookahead; |
||
| 344 | } |
||
| 345 | |||
| 346 | $message = sprintf('Expected %s, got ', $expected); |
||
| 347 | $message .= ($this->lexer->lookahead === null) |
||
| 348 | ? 'end of string' |
||
| 349 | : sprintf("'%s' at position %s", $token['value'], $token['position']); |
||
| 350 | |||
| 351 | if (strlen($this->context)) { |
||
| 352 | $message .= ' in ' . $this->context; |
||
| 353 | } |
||
| 354 | |||
| 355 | $message .= '.'; |
||
| 356 | |||
| 357 | throw AnnotationException::syntaxError($message); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Attempts to check if a class exists or not. This always uses PHP autoloading mechanism. |
||
| 362 | * |
||
| 363 | * @param string $fqcn |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | private function classExists($fqcn) |
||
| 368 | { |
||
| 369 | if (isset($this->classExists[$fqcn])) { |
||
| 370 | return $this->classExists[$fqcn]; |
||
| 371 | } |
||
| 372 | |||
| 373 | // final check, does this class exist? |
||
| 374 | return $this->classExists[$fqcn] = class_exists($fqcn); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Collects parsing metadata for a given annotation class |
||
| 379 | * |
||
| 380 | * @param string $name The annotation name |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | private function collectAnnotationMetadata($name) |
||
| 385 | { |
||
| 386 | if (self::$metadataParser === null) { |
||
| 387 | self::$metadataParser = new self(); |
||
| 388 | |||
| 389 | self::$metadataParser->setIgnoreNotImportedAnnotations(true); |
||
| 390 | self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames); |
||
| 391 | self::$metadataParser->setImports([ |
||
| 392 | 'enum' => 'Doctrine\Annotations\Annotation\Enum', |
||
| 393 | 'target' => 'Doctrine\Annotations\Annotation\Target', |
||
| 394 | 'attribute' => 'Doctrine\Annotations\Annotation\Attribute', |
||
| 395 | 'attributes' => 'Doctrine\Annotations\Annotation\Attributes' |
||
| 396 | ]); |
||
| 397 | } |
||
| 398 | |||
| 399 | $class = new ReflectionClass($name); |
||
| 400 | $docComment = $class->getDocComment(); |
||
| 401 | |||
| 402 | // verify that the class is really meant to be an annotation |
||
| 403 | if (strpos($docComment, '@Annotation') === false) { |
||
| 404 | $this->nonAnnotationClasses[$name] = true; |
||
| 405 | return; |
||
| 406 | } |
||
| 407 | |||
| 408 | $constructor = $class->getConstructor(); |
||
| 409 | $useConstructor = $constructor !== null && $constructor->getNumberOfParameters() > 0; |
||
| 410 | $annotationBuilder = new AnnotationMetadataBuilder($name); |
||
| 411 | |||
| 412 | if ($useConstructor) { |
||
| 413 | $annotationBuilder->withUsingConstructor(); |
||
| 414 | } |
||
| 415 | |||
| 416 | self::$metadataParser->setTarget(Target::TARGET_CLASS); |
||
| 417 | |||
| 418 | foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) { |
||
| 419 | if ($annotation instanceof Target) { |
||
| 420 | $annotationBuilder->withTarget(AnnotationTarget::fromAnnotation($annotation)); |
||
| 421 | |||
| 422 | continue; |
||
| 423 | } |
||
| 424 | |||
| 425 | if ($annotation instanceof Attributes) { |
||
| 426 | foreach ($annotation->value as $attribute) { |
||
| 427 | $propertyBuilder = new PropertyMetadataBuilder($attribute->name); |
||
| 428 | |||
| 429 | $this->collectAttributeTypeMetadata($propertyBuilder, $attribute); |
||
| 430 | $annotationBuilder->withProperty($propertyBuilder->build()); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | if ($useConstructor) { |
||
| 436 | $this->metadata->add($annotationBuilder->build()); |
||
| 437 | |||
| 438 | return; |
||
| 439 | } |
||
| 440 | |||
| 441 | // if there is no constructor we will inject values into public properties |
||
| 442 | |||
| 443 | $defaultFound = false; |
||
| 444 | $propertiesBuilders = []; |
||
| 445 | |||
| 446 | // collect all public properties |
||
| 447 | foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 448 | $propertyBuilder = new PropertyMetadataBuilder($property->getName()); |
||
| 449 | $propertyComment = $property->getDocComment(); |
||
| 450 | |||
| 451 | if ($propertyComment === false) { |
||
| 452 | $propertiesBuilders[] = $propertyBuilder; |
||
| 453 | |||
| 454 | continue; |
||
| 455 | } |
||
| 456 | |||
| 457 | $default = strpos($propertyComment, '@Implicit') !== false; |
||
| 458 | $defaultFound = $defaultFound || $default; |
||
| 459 | |||
| 460 | $attribute = new Attribute(); |
||
| 461 | $attribute->required = (false !== strpos($propertyComment, '@Required')); |
||
| 462 | $attribute->default = $default; |
||
| 463 | $attribute->name = $property->name; |
||
| 464 | $attribute->type = (false !== strpos($propertyComment, '@var') && preg_match('/@var\s+([^\s]+)/',$propertyComment, $matches)) |
||
| 465 | ? $matches[1] |
||
| 466 | : 'mixed'; |
||
| 467 | |||
| 468 | $this->collectAttributeTypeMetadata($propertyBuilder, $attribute); |
||
| 469 | |||
| 470 | // checks if the property has @Enum |
||
| 471 | if (false !== strpos($propertyComment, '@Enum')) { |
||
| 472 | $context = 'property ' . $class->name . "::\$" . $property->name; |
||
| 473 | |||
| 474 | self::$metadataParser->setTarget(Target::TARGET_PROPERTY); |
||
| 475 | |||
| 476 | foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) { |
||
| 477 | if ( ! $annotation instanceof Enum) { |
||
| 478 | continue; |
||
| 479 | } |
||
| 480 | |||
| 481 | $propertyBuilder = $propertyBuilder->withEnum([ |
||
| 482 | 'value' => $annotation->value, |
||
| 483 | 'literal' => ( ! empty($annotation->literal)) |
||
| 484 | ? $annotation->literal |
||
| 485 | : $annotation->value, |
||
| 486 | ]); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | $propertiesBuilders[] = $propertyBuilder; |
||
| 491 | } |
||
| 492 | |||
| 493 | if (! $defaultFound && count($propertiesBuilders) !== 0) { |
||
| 494 | $propertiesBuilders[0]->withBeingDefault(); |
||
| 495 | } |
||
| 496 | |||
| 497 | foreach ($propertiesBuilders as $propertyBuilder) { |
||
| 498 | $annotationBuilder->withProperty($propertyBuilder->build()); |
||
| 499 | } |
||
| 500 | |||
| 501 | $this->metadata->add($annotationBuilder->build()); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Collects parsing metadata for a given attribute. |
||
| 506 | */ |
||
| 507 | private function collectAttributeTypeMetadata( |
||
| 508 | PropertyMetadataBuilder $metadata, |
||
| 509 | Attribute $attribute |
||
| 510 | ) : void { |
||
| 511 | // handle internal type declaration |
||
| 512 | $type = self::$typeMap[$attribute->type] ?? $attribute->type; |
||
| 513 | |||
| 514 | // handle the case if the property type is mixed |
||
| 515 | if ('mixed' === $type) { |
||
| 516 | return; |
||
| 517 | } |
||
| 518 | |||
| 519 | if ($attribute->required) { |
||
| 520 | $metadata->withBeingRequired(); |
||
| 521 | } |
||
| 522 | |||
| 523 | if ($attribute->default) { |
||
| 524 | $metadata->withBeingDefault(); |
||
| 525 | } |
||
| 526 | |||
| 527 | // Evaluate type |
||
| 528 | |||
| 529 | // Checks if the property has array<type> |
||
| 530 | if (false !== $pos = strpos($type, '<')) { |
||
| 531 | $arrayType = substr($type, $pos + 1, -1); |
||
| 532 | |||
| 533 | $metadata->withType([ |
||
| 534 | 'type' => 'array', |
||
| 535 | 'array_type' => self::$typeMap[$arrayType] ?? $arrayType, |
||
| 536 | ]); |
||
| 537 | |||
| 538 | return; |
||
| 539 | } |
||
| 540 | |||
| 541 | // Checks if the property has type[] |
||
| 542 | if (false !== $pos = strrpos($type, '[')) { |
||
| 543 | $arrayType = substr($type, 0, $pos); |
||
| 544 | |||
| 545 | $metadata->withType([ |
||
| 546 | 'type' => 'array', |
||
| 547 | 'array_type' => self::$typeMap[$arrayType] ?? $arrayType, |
||
| 548 | ]); |
||
| 549 | |||
| 550 | return; |
||
| 551 | } |
||
| 552 | |||
| 553 | $metadata->withType([ |
||
| 554 | 'type' => $type, |
||
| 555 | 'value' => $attribute->type, |
||
| 556 | ]); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Annotations ::= Annotation {[ "*" ]* [Annotation]}* |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | private function Annotations() |
||
| 565 | { |
||
| 566 | $annotations = []; |
||
| 567 | |||
| 568 | while (null !== $this->lexer->lookahead) { |
||
| 569 | if (DocLexer::T_AT !== $this->lexer->lookahead['type']) { |
||
| 570 | $this->lexer->moveNext(); |
||
| 571 | continue; |
||
| 572 | } |
||
| 573 | |||
| 574 | // make sure the @ is preceded by non-catchable pattern |
||
| 575 | if (null !== $this->lexer->token && $this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen($this->lexer->token['value'])) { |
||
| 576 | $this->lexer->moveNext(); |
||
| 577 | continue; |
||
| 578 | } |
||
| 579 | |||
| 580 | // make sure the @ is followed by either a namespace separator, or |
||
| 581 | // an identifier token |
||
| 582 | if ((null === $peek = $this->lexer->glimpse()) |
||
| 583 | || (DocLexer::T_NAMESPACE_SEPARATOR !== $peek['type'] && !in_array($peek['type'], self::$classIdentifiers, true)) |
||
| 584 | || $peek['position'] !== $this->lexer->lookahead['position'] + 1) { |
||
| 585 | $this->lexer->moveNext(); |
||
| 586 | continue; |
||
| 587 | } |
||
| 588 | |||
| 589 | $this->isNestedAnnotation = false; |
||
| 590 | if (false !== $annot = $this->Annotation()) { |
||
| 591 | $annotations[] = $annot; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | return $annotations; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Annotation ::= "@" AnnotationName MethodCall |
||
| 600 | * AnnotationName ::= QualifiedName | SimpleName |
||
| 601 | * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName |
||
| 602 | * NameSpacePart ::= identifier | null | false | true |
||
| 603 | * SimpleName ::= identifier | null | false | true |
||
| 604 | * |
||
| 605 | * @return mixed False if it is not a valid annotation. |
||
| 606 | * |
||
| 607 | * @throws AnnotationException |
||
| 608 | */ |
||
| 609 | private function Annotation() |
||
| 610 | { |
||
| 611 | $this->match(DocLexer::T_AT); |
||
| 612 | |||
| 613 | // check if we have an annotation |
||
| 614 | $name = $this->Identifier(); |
||
| 615 | |||
| 616 | if ($this->lexer->isNextToken(DocLexer::T_MINUS) |
||
| 617 | && $this->lexer->nextTokenIsAdjacent() |
||
| 618 | ) { |
||
| 619 | // Annotations with dashes, such as "@foo-" or "@foo-bar", are to be discarded |
||
| 620 | return false; |
||
| 621 | } |
||
| 622 | |||
| 623 | // only process names which are not fully qualified, yet |
||
| 624 | // fully qualified names must start with a \ |
||
| 625 | $originalName = $name; |
||
| 626 | |||
| 627 | if ('\\' !== $name[0]) { |
||
| 628 | $pos = strpos($name, '\\'); |
||
| 629 | $alias = (false === $pos)? $name : substr($name, 0, $pos); |
||
| 630 | $found = false; |
||
| 631 | $loweredAlias = strtolower($alias); |
||
| 632 | |||
| 633 | if ($this->namespaces) { |
||
| 634 | foreach ($this->namespaces as $namespace) { |
||
| 635 | if ($this->classExists($namespace.'\\'.$name)) { |
||
| 636 | $name = $namespace.'\\'.$name; |
||
| 637 | $found = true; |
||
| 638 | break; |
||
| 639 | } |
||
| 640 | } |
||
| 641 | } elseif (isset($this->imports[$loweredAlias])) { |
||
| 642 | $found = true; |
||
| 643 | $name = (false !== $pos) |
||
| 644 | ? $this->imports[$loweredAlias] . substr($name, $pos) |
||
| 645 | : $this->imports[$loweredAlias]; |
||
| 646 | } elseif ( ! isset($this->ignoredAnnotationNames[$name]) |
||
| 647 | && isset($this->imports['__NAMESPACE__']) |
||
| 648 | && $this->classExists($this->imports['__NAMESPACE__'] . '\\' . $name) |
||
| 649 | ) { |
||
| 650 | $name = $this->imports['__NAMESPACE__'].'\\'.$name; |
||
| 651 | $found = true; |
||
| 652 | } elseif (! isset($this->ignoredAnnotationNames[$name]) && $this->classExists($name)) { |
||
| 653 | $found = true; |
||
| 654 | } |
||
| 655 | |||
| 656 | if ( ! $found) { |
||
| 657 | if ($this->isIgnoredAnnotation($name)) { |
||
| 658 | return false; |
||
| 659 | } |
||
| 660 | |||
| 661 | throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?', $name, $this->context)); |
||
| 662 | } |
||
| 663 | } |
||
| 664 | |||
| 665 | $name = ltrim($name,'\\'); |
||
| 666 | |||
| 667 | if ( ! $this->classExists($name)) { |
||
| 668 | throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s does not exist, or could not be auto-loaded.', $name, $this->context)); |
||
| 669 | } |
||
| 670 | |||
| 671 | // at this point, $name contains the fully qualified class name of the |
||
| 672 | // annotation, and it is also guaranteed that this class exists, and |
||
| 673 | // that it is loaded |
||
| 674 | |||
| 675 | |||
| 676 | // collects the metadata annotation only if there is not yet |
||
| 677 | if (! $this->metadata->has($name) && ! array_key_exists($name, $this->nonAnnotationClasses)) { |
||
| 678 | $this->collectAnnotationMetadata($name); |
||
| 679 | } |
||
| 680 | |||
| 681 | // verify that the class is really meant to be an annotation and not just any ordinary class |
||
| 682 | if (array_key_exists($name, $this->nonAnnotationClasses)) { |
||
| 683 | if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$originalName])) { |
||
| 684 | return false; |
||
| 685 | } |
||
| 686 | |||
| 687 | throw AnnotationException::semanticalError(sprintf('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.', $name, $name, $originalName, $this->context)); |
||
| 688 | } |
||
| 689 | |||
| 690 | //if target is nested annotation |
||
| 691 | $target = $this->isNestedAnnotation ? Target::TARGET_ANNOTATION : $this->target; |
||
| 692 | |||
| 693 | // Next will be nested |
||
| 694 | $this->isNestedAnnotation = true; |
||
| 695 | $metadata = $this->metadata->get($name); |
||
| 696 | |||
| 697 | //if annotation does not support current target |
||
| 698 | if (($metadata->getTarget()->unwrap() & $target) === 0 && $target) { |
||
| 699 | throw AnnotationException::semanticalError( |
||
| 700 | sprintf('Annotation @%s is not allowed to be declared on %s. You may only use this annotation on these code elements: %s.', |
||
| 701 | $originalName, $this->context, $metadata->getTarget()->describe()) |
||
| 702 | ); |
||
| 703 | } |
||
| 704 | |||
| 705 | $values = $this->MethodCall(); |
||
| 706 | |||
| 707 | // checks all declared attributes for enums |
||
| 708 | foreach ($metadata->getProperties() as $property) { |
||
| 709 | $propertyName = $property->getName(); |
||
| 710 | $enum = $property->getEnum(); |
||
| 711 | |||
| 712 | // checks if the attribute is a valid enumerator |
||
| 713 | if ($enum !== null && isset($values[$propertyName]) && ! in_array($values[$propertyName], $enum['value'])) { |
||
| 714 | throw AnnotationException::enumeratorError($propertyName, $name, $this->context, $enum['literal'], $values[$propertyName]); |
||
| 715 | } |
||
| 716 | } |
||
| 717 | |||
| 718 | // checks all declared attributes |
||
| 719 | foreach ($metadata->getProperties() as $property) { |
||
| 720 | $propertyName = $property->getName(); |
||
| 721 | $valueName = $propertyName; |
||
| 722 | $type = $property->getType(); |
||
| 723 | |||
| 724 | if ($property->isDefault() && !isset($values[$propertyName]) && isset($values['value'])) { |
||
| 725 | $valueName = 'value'; |
||
| 726 | } |
||
| 727 | |||
| 728 | // handle a not given attribute or null value |
||
| 729 | if (! isset($values[$valueName])) { |
||
| 730 | if ($property->isRequired()) { |
||
| 731 | throw AnnotationException::requiredError($propertyName, $originalName, $this->context, 'a(n) ' . $type['value']); |
||
| 732 | } |
||
| 733 | |||
| 734 | continue; |
||
| 735 | } |
||
| 736 | |||
| 737 | if ($type !== null && $type['type'] === 'array') { |
||
| 738 | // handle the case of a single value |
||
| 739 | if ( ! is_array($values[$valueName])) { |
||
| 740 | $values[$valueName] = [$values[$valueName]]; |
||
| 741 | } |
||
| 742 | |||
| 743 | // checks if the attribute has array type declaration, such as "array<string>" |
||
| 744 | if (isset($type['array_type'])) { |
||
| 745 | foreach ($values[$valueName] as $item) { |
||
| 746 | if (gettype($item) !== $type['array_type'] && !$item instanceof $type['array_type']) { |
||
| 747 | throw AnnotationException::attributeTypeError($propertyName, $originalName, $this->context, 'either a(n) '.$type['array_type'].', or an array of '.$type['array_type'].'s', $item); |
||
| 748 | } |
||
| 749 | } |
||
| 750 | } |
||
| 751 | } elseif ($type !== null && gettype($values[$valueName]) !== $type['type'] && !$values[$valueName] instanceof $type['type']) { |
||
| 752 | throw AnnotationException::attributeTypeError($propertyName, $originalName, $this->context, 'a(n) '.$type['value'], $values[$valueName]); |
||
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | // check if the annotation expects values via the constructor, |
||
| 757 | // or directly injected into public properties |
||
| 758 | if ($metadata->usesConstructor()) { |
||
| 759 | return new $name($values); |
||
| 760 | } |
||
| 761 | |||
| 762 | $instance = new $name(); |
||
| 763 | |||
| 764 | foreach ($values as $property => $value) { |
||
| 765 | if (! isset($metadata->getProperties()[$property])) { |
||
| 766 | if ('value' !== $property) { |
||
| 767 | throw AnnotationException::creationError( |
||
| 768 | sprintf( |
||
| 769 | 'The annotation @%s declared on %s does not have a property named "%s". Available properties: %s', |
||
| 770 | $originalName, |
||
| 771 | $this->context, |
||
| 772 | $property, |
||
| 773 | implode(', ', array_keys($metadata->getProperties())) |
||
| 774 | ) |
||
| 775 | ); |
||
| 776 | } |
||
| 777 | |||
| 778 | $defaultProperty = $metadata->getDefaultProperty(); |
||
| 779 | |||
| 780 | // handle the case if the property has no annotations |
||
| 781 | if ($defaultProperty === null) { |
||
| 782 | throw AnnotationException::creationError(sprintf('The annotation @%s declared on %s does not accept any values, but got %s.', $originalName, $this->context, json_encode($values))); |
||
| 783 | } |
||
| 784 | |||
| 785 | $property = $defaultProperty->getName(); |
||
| 786 | } |
||
| 787 | |||
| 788 | $instance->{$property} = $value; |
||
| 789 | } |
||
| 790 | |||
| 791 | return $instance; |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * MethodCall ::= ["(" [Values] ")"] |
||
| 796 | * |
||
| 797 | * @return array |
||
| 798 | */ |
||
| 799 | private function MethodCall() |
||
| 800 | { |
||
| 801 | $values = []; |
||
| 802 | |||
| 803 | if ( ! $this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) { |
||
| 804 | return $values; |
||
| 805 | } |
||
| 806 | |||
| 807 | $this->match(DocLexer::T_OPEN_PARENTHESIS); |
||
| 808 | |||
| 809 | if ( ! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) { |
||
| 810 | $values = $this->Values(); |
||
| 811 | } |
||
| 812 | |||
| 813 | $this->match(DocLexer::T_CLOSE_PARENTHESIS); |
||
| 814 | |||
| 815 | return $values; |
||
| 816 | } |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Values ::= Array | Value {"," Value}* [","] |
||
| 820 | * |
||
| 821 | * @return array |
||
| 822 | */ |
||
| 823 | private function Values() |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Constant ::= integer | string | float | boolean |
||
| 865 | * |
||
| 866 | * @return mixed |
||
| 867 | * |
||
| 868 | * @throws AnnotationException |
||
| 869 | */ |
||
| 870 | private function Constant() |
||
| 871 | { |
||
| 872 | $identifier = $this->Identifier(); |
||
| 873 | |||
| 874 | if ( ! defined($identifier) && false !== strpos($identifier, '::') && '\\' !== $identifier[0]) { |
||
| 875 | list($className, $const) = explode('::', $identifier); |
||
| 876 | |||
| 877 | $pos = strpos($className, '\\'); |
||
| 878 | $alias = (false === $pos) ? $className : substr($className, 0, $pos); |
||
| 879 | $found = false; |
||
| 880 | $loweredAlias = strtolower($alias); |
||
| 881 | |||
| 882 | switch (true) { |
||
| 883 | case !empty ($this->namespaces): |
||
| 884 | foreach ($this->namespaces as $ns) { |
||
| 885 | if (class_exists($ns.'\\'.$className) || interface_exists($ns.'\\'.$className)) { |
||
| 886 | $className = $ns.'\\'.$className; |
||
| 887 | $found = true; |
||
| 888 | break; |
||
| 889 | } |
||
| 890 | } |
||
| 891 | break; |
||
| 892 | |||
| 893 | case isset($this->imports[$loweredAlias]): |
||
| 894 | $found = true; |
||
| 895 | $className = (false !== $pos) |
||
| 896 | ? $this->imports[$loweredAlias] . substr($className, $pos) |
||
| 897 | : $this->imports[$loweredAlias]; |
||
| 898 | break; |
||
| 899 | |||
| 900 | default: |
||
| 901 | if(isset($this->imports['__NAMESPACE__'])) { |
||
| 902 | $ns = $this->imports['__NAMESPACE__']; |
||
| 903 | |||
| 904 | if (class_exists($ns.'\\'.$className) || interface_exists($ns.'\\'.$className)) { |
||
| 905 | $className = $ns.'\\'.$className; |
||
| 906 | $found = true; |
||
| 907 | } |
||
| 908 | } |
||
| 909 | break; |
||
| 910 | } |
||
| 911 | |||
| 912 | if ($found) { |
||
| 913 | $identifier = $className . '::' . $const; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | |||
| 917 | // checks if identifier ends with ::class, \strlen('::class') === 7 |
||
| 918 | $classPos = stripos($identifier, '::class'); |
||
| 919 | if ($classPos === strlen($identifier) - 7) { |
||
| 920 | return substr($identifier, 0, $classPos); |
||
| 921 | } |
||
| 922 | |||
| 923 | if (!defined($identifier)) { |
||
| 924 | throw AnnotationException::semanticalErrorConstants($identifier, $this->context); |
||
| 925 | } |
||
| 926 | |||
| 927 | return constant($identifier); |
||
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Identifier ::= string |
||
| 932 | * |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | private function Identifier() |
||
| 936 | { |
||
| 937 | // check if we have an annotation |
||
| 938 | if ( ! $this->lexer->isNextTokenAny(self::$classIdentifiers)) { |
||
| 939 | $this->syntaxError('namespace separator or identifier'); |
||
| 940 | } |
||
| 941 | |||
| 942 | $this->lexer->moveNext(); |
||
| 943 | |||
| 944 | $className = $this->lexer->token['value']; |
||
| 945 | |||
| 946 | while ($this->lexer->lookahead['position'] === ($this->lexer->token['position'] + strlen($this->lexer->token['value'])) |
||
| 947 | && $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)) { |
||
| 948 | |||
| 949 | $this->match(DocLexer::T_NAMESPACE_SEPARATOR); |
||
| 950 | $this->matchAny(self::$classIdentifiers); |
||
| 951 | |||
| 952 | $className .= '\\' . $this->lexer->token['value']; |
||
| 953 | } |
||
| 954 | |||
| 955 | return $className; |
||
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Value ::= PlainValue | FieldAssignment |
||
| 960 | * |
||
| 961 | * @return mixed |
||
| 962 | */ |
||
| 963 | private function Value() |
||
| 964 | { |
||
| 965 | $peek = $this->lexer->glimpse(); |
||
| 966 | |||
| 967 | if (DocLexer::T_EQUALS === $peek['type']) { |
||
| 968 | return $this->FieldAssignment(); |
||
| 969 | } |
||
| 970 | |||
| 971 | return $this->PlainValue(); |
||
| 972 | } |
||
| 973 | |||
| 974 | /** |
||
| 975 | * PlainValue ::= integer | string | float | boolean | Array | Annotation |
||
| 976 | * |
||
| 977 | * @return mixed |
||
| 978 | */ |
||
| 979 | private function PlainValue() |
||
| 980 | { |
||
| 981 | if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) { |
||
| 982 | return $this->Arrayx(); |
||
| 983 | } |
||
| 984 | |||
| 985 | if ($this->lexer->isNextToken(DocLexer::T_AT)) { |
||
| 986 | return $this->Annotation(); |
||
| 987 | } |
||
| 988 | |||
| 989 | if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) { |
||
| 990 | return $this->Constant(); |
||
| 991 | } |
||
| 992 | |||
| 993 | switch ($this->lexer->lookahead['type']) { |
||
| 994 | case DocLexer::T_STRING: |
||
| 995 | $this->match(DocLexer::T_STRING); |
||
| 996 | return $this->lexer->token['value']; |
||
| 997 | |||
| 998 | case DocLexer::T_INTEGER: |
||
| 999 | $this->match(DocLexer::T_INTEGER); |
||
| 1000 | return (int)$this->lexer->token['value']; |
||
| 1001 | |||
| 1002 | case DocLexer::T_FLOAT: |
||
| 1003 | $this->match(DocLexer::T_FLOAT); |
||
| 1004 | return (float)$this->lexer->token['value']; |
||
| 1005 | |||
| 1006 | case DocLexer::T_TRUE: |
||
| 1007 | $this->match(DocLexer::T_TRUE); |
||
| 1008 | return true; |
||
| 1009 | |||
| 1010 | case DocLexer::T_FALSE: |
||
| 1011 | $this->match(DocLexer::T_FALSE); |
||
| 1012 | return false; |
||
| 1013 | |||
| 1014 | case DocLexer::T_NULL: |
||
| 1015 | $this->match(DocLexer::T_NULL); |
||
| 1016 | return null; |
||
| 1017 | |||
| 1018 | default: |
||
| 1019 | $this->syntaxError('PlainValue'); |
||
| 1020 | } |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * FieldAssignment ::= FieldName "=" PlainValue |
||
| 1025 | * FieldName ::= identifier |
||
| 1026 | * |
||
| 1027 | * @return \stdClass |
||
| 1028 | */ |
||
| 1029 | private function FieldAssignment() |
||
| 1030 | { |
||
| 1031 | $this->match(DocLexer::T_IDENTIFIER); |
||
| 1032 | $fieldName = $this->lexer->token['value']; |
||
| 1033 | |||
| 1034 | $this->match(DocLexer::T_EQUALS); |
||
| 1035 | |||
| 1036 | $item = new \stdClass(); |
||
| 1037 | $item->name = $fieldName; |
||
| 1038 | $item->value = $this->PlainValue(); |
||
| 1039 | |||
| 1040 | return $item; |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Array ::= "{" ArrayEntry {"," ArrayEntry}* [","] "}" |
||
| 1045 | * |
||
| 1046 | * @return array |
||
| 1047 | */ |
||
| 1048 | private function Arrayx() |
||
| 1049 | { |
||
| 1050 | $array = $values = []; |
||
| 1051 | |||
| 1052 | $this->match(DocLexer::T_OPEN_CURLY_BRACES); |
||
| 1053 | |||
| 1054 | // If the array is empty, stop parsing and return. |
||
| 1055 | if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
||
| 1056 | $this->match(DocLexer::T_CLOSE_CURLY_BRACES); |
||
| 1057 | |||
| 1058 | return $array; |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | $values[] = $this->ArrayEntry(); |
||
| 1062 | |||
| 1063 | while ($this->lexer->isNextToken(DocLexer::T_COMMA)) { |
||
| 1064 | $this->match(DocLexer::T_COMMA); |
||
| 1065 | |||
| 1066 | // optional trailing comma |
||
| 1067 | if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
||
| 1068 | break; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | $values[] = $this->ArrayEntry(); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | $this->match(DocLexer::T_CLOSE_CURLY_BRACES); |
||
| 1075 | |||
| 1076 | foreach ($values as $value) { |
||
| 1077 | list ($key, $val) = $value; |
||
| 1078 | |||
| 1079 | if ($key !== null) { |
||
| 1080 | $array[$key] = $val; |
||
| 1081 | } else { |
||
| 1082 | $array[] = $val; |
||
| 1083 | } |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | return $array; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * ArrayEntry ::= Value | KeyValuePair |
||
| 1091 | * KeyValuePair ::= Key ("=" | ":") PlainValue | Constant |
||
| 1092 | * Key ::= string | integer | Constant |
||
| 1093 | * |
||
| 1094 | * @return array |
||
| 1095 | */ |
||
| 1096 | private function ArrayEntry() |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Checks whether the given $name matches any ignored annotation name or namespace |
||
| 1120 | * |
||
| 1121 | * @param string $name |
||
| 1122 | * |
||
| 1123 | * @return bool |
||
| 1124 | */ |
||
| 1125 | private function isIgnoredAnnotation($name) |
||
| 1126 | { |
||
| 1127 | if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) { |
||
| 1128 | return true; |
||
| 1129 | } |
||
| 1130 | |||
| 1140 | } |
||
| 1141 | } |
||
| 1142 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.