|
15 | 15 |
|
16 | 16 | use ArrayIterator;
|
17 | 17 | use InvalidArgumentException;
|
| 18 | +use phpDocumentor\Reflection\PseudoTypes\IntegerRange; |
18 | 19 | use phpDocumentor\Reflection\PseudoTypes\List_;
|
19 | 20 | use phpDocumentor\Reflection\Types\Array_;
|
20 | 21 | use phpDocumentor\Reflection\Types\ArrayKey;
|
|
41 | 42 | use function current;
|
42 | 43 | use function end;
|
43 | 44 | use function in_array;
|
| 45 | +use function is_numeric; |
44 | 46 | use function key;
|
45 | 47 | use function preg_split;
|
46 | 48 | use function strpos;
|
@@ -83,10 +85,12 @@ final class TypeResolver
|
83 | 85 | 'non-empty-lowercase-string' => PseudoTypes\NonEmptyLowercaseString::class,
|
84 | 86 | 'non-empty-string' => PseudoTypes\NonEmptyString::class,
|
85 | 87 | 'numeric-string' => PseudoTypes\NumericString::class,
|
| 88 | + 'numeric' => PseudoTypes\Numeric_::class, |
86 | 89 | 'trait-string' => PseudoTypes\TraitString::class,
|
87 | 90 | 'int' => Types\Integer::class,
|
88 | 91 | 'integer' => Types\Integer::class,
|
89 | 92 | 'positive-int' => PseudoTypes\PositiveInteger::class,
|
| 93 | + 'negative-int' => PseudoTypes\NegativeInteger::class, |
90 | 94 | 'bool' => Types\Boolean::class,
|
91 | 95 | 'boolean' => Types\Boolean::class,
|
92 | 96 | 'real' => Types\Float_::class,
|
@@ -258,6 +262,8 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
|
258 | 262 | if ($classType !== null) {
|
259 | 263 | if ((string) $classType === 'class-string') {
|
260 | 264 | $types[] = $this->resolveClassString($tokens, $context);
|
| 265 | + } elseif ((string) $classType === 'int') { |
| 266 | + $types[] = $this->resolveIntRange($tokens); |
261 | 267 | } elseif ((string) $classType === 'interface-string') {
|
262 | 268 | $types[] = $this->resolveInterfaceString($tokens, $context);
|
263 | 269 | } else {
|
@@ -484,6 +490,75 @@ private function resolveClassString(ArrayIterator $tokens, Context $context): Ty
|
484 | 490 | return new ClassString($classType->getFqsen());
|
485 | 491 | }
|
486 | 492 |
|
| 493 | + /** |
| 494 | + * Resolves integer ranges |
| 495 | + * |
| 496 | + * @param ArrayIterator<int, (string|null)> $tokens |
| 497 | + */ |
| 498 | + private function resolveIntRange(ArrayIterator $tokens): Type |
| 499 | + { |
| 500 | + $tokens->next(); |
| 501 | + |
| 502 | + $token = ''; |
| 503 | + $minValue = null; |
| 504 | + $maxValue = null; |
| 505 | + $commaFound = false; |
| 506 | + $tokenCounter = 0; |
| 507 | + while ($tokens->valid()) { |
| 508 | + $tokenCounter++; |
| 509 | + $token = $tokens->current(); |
| 510 | + if ($token === null) { |
| 511 | + throw new RuntimeException( |
| 512 | + 'Unexpected nullable character' |
| 513 | + ); |
| 514 | + } |
| 515 | + |
| 516 | + $token = trim($token); |
| 517 | + |
| 518 | + if ($token === '>') { |
| 519 | + break; |
| 520 | + } |
| 521 | + |
| 522 | + if ($token === ',') { |
| 523 | + $commaFound = true; |
| 524 | + } |
| 525 | + |
| 526 | + if ($commaFound === false && $minValue === null) { |
| 527 | + if (is_numeric($token) || $token === 'max' || $token === 'min') { |
| 528 | + $minValue = $token; |
| 529 | + } |
| 530 | + } |
| 531 | + |
| 532 | + if ($commaFound === true && $maxValue === null) { |
| 533 | + if (is_numeric($token) || $token === 'max' || $token === 'min') { |
| 534 | + $maxValue = $token; |
| 535 | + } |
| 536 | + } |
| 537 | + |
| 538 | + $tokens->next(); |
| 539 | + } |
| 540 | + |
| 541 | + if ($token !== '>') { |
| 542 | + if (empty($token)) { |
| 543 | + throw new RuntimeException( |
| 544 | + 'interface-string: ">" is missing' |
| 545 | + ); |
| 546 | + } |
| 547 | + |
| 548 | + throw new RuntimeException( |
| 549 | + 'Unexpected character "' . $token . '", ">" is missing' |
| 550 | + ); |
| 551 | + } |
| 552 | + |
| 553 | + if (!$minValue || !$maxValue || $tokenCounter > 4) { |
| 554 | + throw new RuntimeException( |
| 555 | + 'int<min,max> has not the correct format' |
| 556 | + ); |
| 557 | + } |
| 558 | + |
| 559 | + return new IntegerRange($minValue, $maxValue); |
| 560 | + } |
| 561 | + |
487 | 562 | /**
|
488 | 563 | * Resolves class string
|
489 | 564 | *
|
|
0 commit comments