1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Parser;
4:
5: use LogicException;
6: use PHPStan\PhpDocParser\Ast;
7: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
8: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
9: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
10: use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine;
11: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
12: use PHPStan\PhpDocParser\Lexer\Lexer;
13: use PHPStan\PhpDocParser\ParserConfig;
14: use PHPStan\ShouldNotHappenException;
15: use function array_key_exists;
16: use function count;
17: use function rtrim;
18: use function trim;
19:
20: /**
21: * @phpstan-import-type ValueType from Doctrine\DoctrineArgument as DoctrineValueType
22: */
23: class PhpDocParser
24: {
25:
26: private const DISALLOWED_DESCRIPTION_START_TOKENS = [
27: Lexer::TOKEN_UNION,
28: Lexer::TOKEN_INTERSECTION,
29: ];
30:
31: private ParserConfig $config;
32:
33: private TypeParser $typeParser;
34:
35: private ConstExprParser $constantExprParser;
36:
37: private ConstExprParser $doctrineConstantExprParser;
38:
39: public function __construct(
40: ParserConfig $config,
41: TypeParser $typeParser,
42: ConstExprParser $constantExprParser
43: )
44: {
45: $this->config = $config;
46: $this->typeParser = $typeParser;
47: $this->constantExprParser = $constantExprParser;
48: $this->doctrineConstantExprParser = $constantExprParser->toDoctrine();
49: }
50:
51: public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
52: {
53: $tokens->consumeTokenType(Lexer::TOKEN_OPEN_PHPDOC);
54: $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
55:
56: $children = [];
57:
58: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
59: $lastChild = $this->parseChild($tokens);
60: $children[] = $lastChild;
61: while (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
62: if (
63: $lastChild instanceof Ast\PhpDoc\PhpDocTagNode
64: && (
65: $lastChild->value instanceof Doctrine\DoctrineTagValueNode
66: || $lastChild->value instanceof Ast\PhpDoc\GenericTagValueNode
67: )
68: ) {
69: $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
70: if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
71: break;
72: }
73: $lastChild = $this->parseChild($tokens);
74: $children[] = $lastChild;
75: continue;
76: }
77:
78: if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
79: break;
80: }
81: if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
82: break;
83: }
84:
85: $lastChild = $this->parseChild($tokens);
86: $children[] = $lastChild;
87: }
88: }
89:
90: try {
91: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
92: } catch (ParserException $e) {
93: $name = '';
94: $startLine = $tokens->currentTokenLine();
95: $startIndex = $tokens->currentTokenIndex();
96: if (count($children) > 0) {
97: $lastChild = $children[count($children) - 1];
98: if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode) {
99: $name = $lastChild->name;
100: $startLine = $tokens->currentTokenLine();
101: $startIndex = $tokens->currentTokenIndex();
102: }
103: }
104:
105: $tag = new Ast\PhpDoc\PhpDocTagNode(
106: $name,
107: $this->enrichWithAttributes(
108: $tokens,
109: new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e),
110: $startLine,
111: $startIndex,
112: ),
113: );
114:
115: $tokens->forwardToTheEnd();
116:
117: $comments = $tokens->flushComments();
118: if ($comments !== []) {
119: throw new LogicException('Comments should already be flushed');
120: }
121:
122: return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode([$this->enrichWithAttributes($tokens, $tag, $startLine, $startIndex)]), 1, 0);
123: }
124:
125: $comments = $tokens->flushComments();
126: if ($comments !== []) {
127: throw new LogicException('Comments should already be flushed');
128: }
129:
130: return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode($children), 1, 0);
131: }
132:
133: /** @phpstan-impure */
134: private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
135: {
136: if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG)) {
137: $startLine = $tokens->currentTokenLine();
138: $startIndex = $tokens->currentTokenIndex();
139: return $this->enrichWithAttributes($tokens, $this->parseTag($tokens), $startLine, $startIndex);
140: }
141:
142: if ($tokens->isCurrentTokenType(Lexer::TOKEN_DOCTRINE_TAG)) {
143: $startLine = $tokens->currentTokenLine();
144: $startIndex = $tokens->currentTokenIndex();
145: $tag = $tokens->currentTokenValue();
146: $tokens->next();
147:
148: $tagStartLine = $tokens->currentTokenLine();
149: $tagStartIndex = $tokens->currentTokenIndex();
150:
151: try {
152: $tokens->pushSavePoint();
153: $tagValue = $this->parseDoctrineTagValue($tokens, $tag);
154: $tokens->dropSavePoint();
155:
156: } catch (ParserException $e) {
157: $tokens->rollback();
158: $tagValue = new Ast\PhpDoc\InvalidTagValueNode($this->parseOptionalDescription($tokens, false), $e);
159: }
160:
161: return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocTagNode(
162: $tag,
163: $this->enrichWithAttributes(
164: $tokens,
165: $tagValue,
166: $tagStartLine,
167: $tagStartIndex,
168: ),
169: ), $startLine, $startIndex);
170: }
171:
172: $startLine = $tokens->currentTokenLine();
173: $startIndex = $tokens->currentTokenIndex();
174: $text = $this->parseText($tokens);
175:
176: return $this->enrichWithAttributes($tokens, $text, $startLine, $startIndex);
177: }
178:
179: /**
180: * @template T of Ast\Node
181: * @param T $tag
182: * @return T
183: */
184: private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int $startLine, int $startIndex): Ast\Node
185: {
186: if ($this->config->useLinesAttributes) {
187: $tag->setAttribute(Ast\Attribute::START_LINE, $startLine);
188: $tag->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
189: }
190:
191: if ($this->config->useIndexAttributes) {
192: $tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
193: $tag->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
194: }
195:
196: return $tag;
197: }
198:
199: private function parseText(TokenIterator $tokens): Ast\PhpDoc\PhpDocTextNode
200: {
201: $text = '';
202:
203: $endTokens = [Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END];
204:
205: $savepoint = false;
206:
207: // if the next token is EOL, everything below is skipped and empty string is returned
208: while (true) {
209: $tmpText = $tokens->getSkippedHorizontalWhiteSpaceIfAny() . $tokens->joinUntil(Lexer::TOKEN_PHPDOC_EOL, ...$endTokens);
210: $text .= $tmpText;
211:
212: // stop if we're not at EOL - meaning it's the end of PHPDoc
213: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC)) {
214: break;
215: }
216:
217: if (!$savepoint) {
218: $tokens->pushSavePoint();
219: $savepoint = true;
220: } elseif ($tmpText !== '') {
221: $tokens->dropSavePoint();
222: $tokens->pushSavePoint();
223: }
224:
225: $tokens->pushSavePoint();
226: $tokens->next();
227:
228: // if we're at EOL, check what's next
229: // if next is a PHPDoc tag, EOL, or end of PHPDoc, stop
230: if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_DOCTRINE_TAG, ...$endTokens)) {
231: $tokens->rollback();
232: break;
233: }
234:
235: // otherwise if the next is text, continue building the description string
236:
237: $tokens->dropSavePoint();
238: $text .= $tokens->getDetectedNewline() ?? "\n";
239: }
240:
241: if ($savepoint) {
242: $tokens->rollback();
243: $text = rtrim($text, $tokens->getDetectedNewline() ?? "\n");
244: }
245:
246: return new Ast\PhpDoc\PhpDocTextNode(trim($text, " \t"));
247: }
248:
249: private function parseOptionalDescriptionAfterDoctrineTag(TokenIterator $tokens): string
250: {
251: $text = '';
252:
253: $endTokens = [Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END];
254:
255: $savepoint = false;
256:
257: // if the next token is EOL, everything below is skipped and empty string is returned
258: while (true) {
259: $tmpText = $tokens->getSkippedHorizontalWhiteSpaceIfAny() . $tokens->joinUntil(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_DOCTRINE_TAG, Lexer::TOKEN_PHPDOC_EOL, ...$endTokens);
260: $text .= $tmpText;
261:
262: // stop if we're not at EOL - meaning it's the end of PHPDoc
263: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC)) {
264: if (!$tokens->isPrecededByHorizontalWhitespace()) {
265: return trim($text . $this->parseText($tokens)->text, " \t");
266: }
267: if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG)) {
268: $tokens->pushSavePoint();
269: $child = $this->parseChild($tokens);
270: if ($child instanceof Ast\PhpDoc\PhpDocTagNode) {
271: if (
272: $child->value instanceof Ast\PhpDoc\GenericTagValueNode
273: || $child->value instanceof Doctrine\DoctrineTagValueNode
274: ) {
275: $tokens->rollback();
276: break;
277: }
278: if ($child->value instanceof Ast\PhpDoc\InvalidTagValueNode) {
279: $tokens->rollback();
280: $tokens->pushSavePoint();
281: $tokens->next();
282: if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
283: $tokens->rollback();
284: break;
285: }
286: $tokens->rollback();
287: return trim($text . $this->parseText($tokens)->text, " \t");
288: }
289: }
290:
291: $tokens->rollback();
292: return trim($text . $this->parseText($tokens)->text, " \t");
293: }
294: break;
295: }
296:
297: if (!$savepoint) {
298: $tokens->pushSavePoint();
299: $savepoint = true;
300: } elseif ($tmpText !== '') {
301: $tokens->dropSavePoint();
302: $tokens->pushSavePoint();
303: }
304:
305: $tokens->pushSavePoint();
306: $tokens->next();
307:
308: // if we're at EOL, check what's next
309: // if next is a PHPDoc tag, EOL, or end of PHPDoc, stop
310: if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_DOCTRINE_TAG, ...$endTokens)) {
311: $tokens->rollback();
312: break;
313: }
314:
315: // otherwise if the next is text, continue building the description string
316:
317: $tokens->dropSavePoint();
318: $text .= $tokens->getDetectedNewline() ?? "\n";
319: }
320:
321: if ($savepoint) {
322: $tokens->rollback();
323: $text = rtrim($text, $tokens->getDetectedNewline() ?? "\n");
324: }
325:
326: return trim($text, " \t");
327: }
328:
329: public function parseTag(TokenIterator $tokens): Ast\PhpDoc\PhpDocTagNode
330: {
331: $tag = $tokens->currentTokenValue();
332: $tokens->next();
333: $value = $this->parseTagValue($tokens, $tag);
334:
335: return new Ast\PhpDoc\PhpDocTagNode($tag, $value);
336: }
337:
338: public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\PhpDocTagValueNode
339: {
340: $startLine = $tokens->currentTokenLine();
341: $startIndex = $tokens->currentTokenIndex();
342:
343: try {
344: $tokens->pushSavePoint();
345:
346: switch ($tag) {
347: case '@param':
348: case '@phpstan-param':
349: case '@psalm-param':
350: case '@phan-param':
351: $tagValue = $this->parseParamTagValue($tokens);
352: break;
353:
354: case '@param-immediately-invoked-callable':
355: case '@phpstan-param-immediately-invoked-callable':
356: $tagValue = $this->parseParamImmediatelyInvokedCallableTagValue($tokens);
357: break;
358:
359: case '@param-later-invoked-callable':
360: case '@phpstan-param-later-invoked-callable':
361: $tagValue = $this->parseParamLaterInvokedCallableTagValue($tokens);
362: break;
363:
364: case '@param-closure-this':
365: case '@phpstan-param-closure-this':
366: $tagValue = $this->parseParamClosureThisTagValue($tokens);
367: break;
368:
369: case '@pure-unless-callable-is-impure':
370: case '@phpstan-pure-unless-callable-is-impure':
371: $tagValue = $this->parsePureUnlessCallableIsImpureTagValue($tokens);
372: break;
373:
374: case '@pure-unless-parameter-passed':
375: case '@phpstan-pure-unless-parameter-passed':
376: $tagValue = $this->parsePureUnlessParameterIsPassed($tokens);
377: break;
378:
379: case '@var':
380: case '@phpstan-var':
381: case '@psalm-var':
382: case '@phan-var':
383: $tagValue = $this->parseVarTagValue($tokens);
384: break;
385:
386: case '@return':
387: case '@phpstan-return':
388: case '@psalm-return':
389: case '@phan-return':
390: case '@phan-real-return':
391: $tagValue = $this->parseReturnTagValue($tokens);
392: break;
393:
394: case '@throws':
395: case '@phpstan-throws':
396: $tagValue = $this->parseThrowsTagValue($tokens);
397: break;
398:
399: case '@mixin':
400: case '@phan-mixin':
401: $tagValue = $this->parseMixinTagValue($tokens);
402: break;
403:
404: case '@psalm-require-extends':
405: case '@phpstan-require-extends':
406: $tagValue = $this->parseRequireExtendsTagValue($tokens);
407: break;
408:
409: case '@psalm-require-implements':
410: case '@phpstan-require-implements':
411: $tagValue = $this->parseRequireImplementsTagValue($tokens);
412: break;
413:
414: case '@psalm-inheritors':
415: case '@phpstan-sealed':
416: $tagValue = $this->parseSealedTagValue($tokens);
417: break;
418:
419: case '@deprecated':
420: $tagValue = $this->parseDeprecatedTagValue($tokens);
421: break;
422:
423: case '@property':
424: case '@property-read':
425: case '@property-write':
426: case '@phpstan-property':
427: case '@phpstan-property-read':
428: case '@phpstan-property-write':
429: case '@psalm-property':
430: case '@psalm-property-read':
431: case '@psalm-property-write':
432: case '@phan-property':
433: case '@phan-property-read':
434: case '@phan-property-write':
435: $tagValue = $this->parsePropertyTagValue($tokens);
436: break;
437:
438: case '@method':
439: case '@phpstan-method':
440: case '@psalm-method':
441: case '@phan-method':
442: $tagValue = $this->parseMethodTagValue($tokens);
443: break;
444:
445: case '@template':
446: case '@phpstan-template':
447: case '@psalm-template':
448: case '@phan-template':
449: case '@template-covariant':
450: case '@phpstan-template-covariant':
451: case '@psalm-template-covariant':
452: case '@template-contravariant':
453: case '@phpstan-template-contravariant':
454: case '@psalm-template-contravariant':
455: $tagValue = $this->typeParser->parseTemplateTagValue(
456: $tokens,
457: fn ($tokens) => $this->parseOptionalDescription($tokens, true),
458: );
459: break;
460:
461: case '@extends':
462: case '@phpstan-extends':
463: case '@phan-extends':
464: case '@phan-inherits':
465: case '@template-extends':
466: $tagValue = $this->parseExtendsTagValue('@extends', $tokens);
467: break;
468:
469: case '@implements':
470: case '@phpstan-implements':
471: case '@template-implements':
472: $tagValue = $this->parseExtendsTagValue('@implements', $tokens);
473: break;
474:
475: case '@use':
476: case '@phpstan-use':
477: case '@template-use':
478: $tagValue = $this->parseExtendsTagValue('@use', $tokens);
479: break;
480:
481: case '@phpstan-type':
482: case '@psalm-type':
483: case '@phan-type':
484: $tagValue = $this->parseTypeAliasTagValue($tokens);
485: break;
486:
487: case '@phpstan-import-type':
488: case '@psalm-import-type':
489: $tagValue = $this->parseTypeAliasImportTagValue($tokens);
490: break;
491:
492: case '@phpstan-assert':
493: case '@phpstan-assert-if-true':
494: case '@phpstan-assert-if-false':
495: case '@psalm-assert':
496: case '@psalm-assert-if-true':
497: case '@psalm-assert-if-false':
498: case '@phan-assert':
499: case '@phan-assert-if-true':
500: case '@phan-assert-if-false':
501: $tagValue = $this->parseAssertTagValue($tokens);
502: break;
503:
504: case '@phpstan-this-out':
505: case '@phpstan-self-out':
506: case '@psalm-this-out':
507: case '@psalm-self-out':
508: $tagValue = $this->parseSelfOutTagValue($tokens);
509: break;
510:
511: case '@param-out':
512: case '@phpstan-param-out':
513: case '@psalm-param-out':
514: $tagValue = $this->parseParamOutTagValue($tokens);
515: break;
516:
517: default:
518: if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
519: $tagValue = $this->parseDoctrineTagValue($tokens, $tag);
520: } else {
521: $tagValue = new Ast\PhpDoc\GenericTagValueNode($this->parseOptionalDescriptionAfterDoctrineTag($tokens));
522: }
523: break;
524: }
525:
526: $tokens->dropSavePoint();
527:
528: } catch (ParserException $e) {
529: $tokens->rollback();
530: $tagValue = new Ast\PhpDoc\InvalidTagValueNode($this->parseOptionalDescription($tokens, false), $e);
531: }
532:
533: return $this->enrichWithAttributes($tokens, $tagValue, $startLine, $startIndex);
534: }
535:
536: private function parseDoctrineTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\PhpDocTagValueNode
537: {
538: $startLine = $tokens->currentTokenLine();
539: $startIndex = $tokens->currentTokenIndex();
540:
541: return new Doctrine\DoctrineTagValueNode(
542: $this->enrichWithAttributes(
543: $tokens,
544: new Doctrine\DoctrineAnnotation($tag, $this->parseDoctrineArguments($tokens, false)),
545: $startLine,
546: $startIndex,
547: ),
548: $this->parseOptionalDescriptionAfterDoctrineTag($tokens),
549: );
550: }
551:
552: /**
553: * @return list<Doctrine\DoctrineArgument>
554: */
555: private function parseDoctrineArguments(TokenIterator $tokens, bool $deep): array
556: {
557: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
558: return [];
559: }
560:
561: if (!$deep) {
562: $tokens->addEndOfLineToSkippedTokens();
563: }
564:
565: $arguments = [];
566:
567: try {
568: $tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
569:
570: do {
571: if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
572: break;
573: }
574: $arguments[] = $this->parseDoctrineArgument($tokens);
575: } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
576: } finally {
577: if (!$deep) {
578: $tokens->removeEndOfLineFromSkippedTokens();
579: }
580: }
581:
582: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
583:
584: return $arguments;
585: }
586:
587: private function parseDoctrineArgument(TokenIterator $tokens): Doctrine\DoctrineArgument
588: {
589: // a quoted key, e.g. "key"="value", is not valid Doctrine syntax, but is accepted as the key
590: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER, Lexer::TOKEN_DOUBLE_QUOTED_STRING, Lexer::TOKEN_DOCTRINE_ANNOTATION_STRING)) {
591: $startLine = $tokens->currentTokenLine();
592: $startIndex = $tokens->currentTokenIndex();
593:
594: return $this->enrichWithAttributes(
595: $tokens,
596: new Doctrine\DoctrineArgument(null, $this->parseDoctrineArgumentValue($tokens)),
597: $startLine,
598: $startIndex,
599: );
600: }
601:
602: $startLine = $tokens->currentTokenLine();
603: $startIndex = $tokens->currentTokenIndex();
604:
605: try {
606: $tokens->pushSavePoint();
607: $isIdentifier = $tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER);
608: $currentValue = $tokens->currentTokenValue();
609: $tokens->next();
610: if (!$isIdentifier) {
611: $currentValue = $this->doctrineConstantExprParser->parseDoctrineString($currentValue, $tokens)->value;
612: }
613:
614: $key = $this->enrichWithAttributes(
615: $tokens,
616: new IdentifierTypeNode($currentValue),
617: $startLine,
618: $startIndex,
619: );
620: $tokens->consumeTokenType(Lexer::TOKEN_EQUAL);
621:
622: $value = $this->parseDoctrineArgumentValue($tokens);
623:
624: $tokens->dropSavePoint();
625:
626: return $this->enrichWithAttributes(
627: $tokens,
628: new Doctrine\DoctrineArgument($key, $value),
629: $startLine,
630: $startIndex,
631: );
632: } catch (ParserException $e) {
633: $tokens->rollback();
634:
635: return $this->enrichWithAttributes(
636: $tokens,
637: new Doctrine\DoctrineArgument(null, $this->parseDoctrineArgumentValue($tokens)),
638: $startLine,
639: $startIndex,
640: );
641: }
642: }
643:
644: /**
645: * @return DoctrineValueType
646: */
647: private function parseDoctrineArgumentValue(TokenIterator $tokens)
648: {
649: $startLine = $tokens->currentTokenLine();
650: $startIndex = $tokens->currentTokenIndex();
651:
652: if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_DOCTRINE_TAG)) {
653: $name = $tokens->currentTokenValue();
654: $tokens->next();
655:
656: return $this->enrichWithAttributes(
657: $tokens,
658: new Doctrine\DoctrineAnnotation($name, $this->parseDoctrineArguments($tokens, true)),
659: $startLine,
660: $startIndex,
661: );
662: }
663:
664: if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET)) {
665: $items = [];
666: do {
667: if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) {
668: break;
669: }
670: $items[] = $this->parseDoctrineArrayItem($tokens);
671: } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
672:
673: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);
674:
675: return $this->enrichWithAttributes(
676: $tokens,
677: new Doctrine\DoctrineArray($items),
678: $startLine,
679: $startIndex,
680: );
681: }
682:
683: $currentTokenValue = $tokens->currentTokenValue();
684: $tokens->pushSavePoint(); // because of ConstFetchNode
685: if ($tokens->tryConsumeTokenType(Lexer::TOKEN_IDENTIFIER)) {
686: $identifier = $this->enrichWithAttributes(
687: $tokens,
688: new Ast\Type\IdentifierTypeNode($currentTokenValue),
689: $startLine,
690: $startIndex,
691: );
692: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
693: $tokens->dropSavePoint();
694: return $identifier;
695: }
696:
697: $tokens->rollback(); // because of ConstFetchNode
698: } else {
699: $tokens->dropSavePoint(); // because of ConstFetchNode
700: }
701:
702: $currentTokenValue = $tokens->currentTokenValue();
703: $currentTokenType = $tokens->currentTokenType();
704: $currentTokenOffset = $tokens->currentTokenOffset();
705: $currentTokenLine = $tokens->currentTokenLine();
706:
707: try {
708: $constExpr = $this->doctrineConstantExprParser->parse($tokens);
709: if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
710: throw new ParserException(
711: $currentTokenValue,
712: $currentTokenType,
713: $currentTokenOffset,
714: Lexer::TOKEN_IDENTIFIER,
715: null,
716: $currentTokenLine,
717: );
718: }
719:
720: return $constExpr;
721: } catch (LogicException $e) {
722: throw new ParserException(
723: $currentTokenValue,
724: $currentTokenType,
725: $currentTokenOffset,
726: Lexer::TOKEN_IDENTIFIER,
727: null,
728: $currentTokenLine,
729: );
730: }
731: }
732:
733: private function parseDoctrineArrayItem(TokenIterator $tokens): Doctrine\DoctrineArrayItem
734: {
735: $startLine = $tokens->currentTokenLine();
736: $startIndex = $tokens->currentTokenIndex();
737:
738: try {
739: $tokens->pushSavePoint();
740:
741: $key = $this->parseDoctrineArrayKey($tokens);
742: if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL)) {
743: if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_COLON)) {
744: $tokens->consumeTokenType(Lexer::TOKEN_EQUAL); // will throw exception
745: }
746: }
747:
748: $value = $this->parseDoctrineArgumentValue($tokens);
749:
750: $tokens->dropSavePoint();
751:
752: return $this->enrichWithAttributes(
753: $tokens,
754: new Doctrine\DoctrineArrayItem($key, $value),
755: $startLine,
756: $startIndex,
757: );
758: } catch (ParserException $e) {
759: $tokens->rollback();
760:
761: return $this->enrichWithAttributes(
762: $tokens,
763: new Doctrine\DoctrineArrayItem(null, $this->parseDoctrineArgumentValue($tokens)),
764: $startLine,
765: $startIndex,
766: );
767: }
768: }
769:
770: /**
771: * @return ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|ConstFetchNode
772: */
773: private function parseDoctrineArrayKey(TokenIterator $tokens)
774: {
775: $startLine = $tokens->currentTokenLine();
776: $startIndex = $tokens->currentTokenIndex();
777:
778: if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) {
779: $key = new Ast\ConstExpr\ConstExprIntegerNode(IntegerLiteralNormalizer::normalizeToDecimal($tokens->currentTokenValue()));
780: $tokens->next();
781:
782: } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_DOCTRINE_ANNOTATION_STRING)) {
783: $key = $this->doctrineConstantExprParser->parseDoctrineString($tokens->currentTokenValue(), $tokens);
784:
785: $tokens->next();
786:
787: } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) {
788: $key = new Ast\ConstExpr\ConstExprStringNode(StringUnescaper::unescapeString($tokens->currentTokenValue()), Ast\ConstExpr\ConstExprStringNode::SINGLE_QUOTED);
789: $tokens->next();
790:
791: } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_QUOTED_STRING)) {
792: $value = $tokens->currentTokenValue();
793: $tokens->next();
794: $key = $this->doctrineConstantExprParser->parseDoctrineString($value, $tokens);
795:
796: } else {
797: $currentTokenValue = $tokens->currentTokenValue();
798: $tokens->pushSavePoint(); // because of ConstFetchNode
799: if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_IDENTIFIER)) {
800: $tokens->dropSavePoint();
801: throw new ParserException(
802: $tokens->currentTokenValue(),
803: $tokens->currentTokenType(),
804: $tokens->currentTokenOffset(),
805: Lexer::TOKEN_IDENTIFIER,
806: null,
807: $tokens->currentTokenLine(),
808: );
809: }
810:
811: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
812: $tokens->dropSavePoint();
813:
814: return $this->enrichWithAttributes(
815: $tokens,
816: new IdentifierTypeNode($currentTokenValue),
817: $startLine,
818: $startIndex,
819: );
820: }
821:
822: $tokens->rollback();
823: $constExpr = $this->doctrineConstantExprParser->parse($tokens);
824: if (!$constExpr instanceof Ast\ConstExpr\ConstFetchNode) {
825: throw new ParserException(
826: $tokens->currentTokenValue(),
827: $tokens->currentTokenType(),
828: $tokens->currentTokenOffset(),
829: Lexer::TOKEN_IDENTIFIER,
830: null,
831: $tokens->currentTokenLine(),
832: );
833: }
834:
835: return $constExpr;
836: }
837:
838: return $this->enrichWithAttributes($tokens, $key, $startLine, $startIndex);
839: }
840:
841: /**
842: * @return Ast\PhpDoc\ParamTagValueNode|Ast\PhpDoc\TypelessParamTagValueNode
843: */
844: private function parseParamTagValue(TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
845: {
846: if (
847: $tokens->isCurrentTokenType(Lexer::TOKEN_REFERENCE, Lexer::TOKEN_VARIADIC, Lexer::TOKEN_VARIABLE)
848: ) {
849: $type = null;
850: } else {
851: $type = $this->typeParser->parse($tokens);
852: }
853:
854: $isReference = $tokens->tryConsumeTokenType(Lexer::TOKEN_REFERENCE);
855: $isVariadic = $tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC);
856: $parameterName = $this->parseRequiredVariableName($tokens);
857: $description = $this->parseOptionalDescription($tokens, false);
858:
859: if ($type !== null) {
860: return new Ast\PhpDoc\ParamTagValueNode($type, $isVariadic, $parameterName, $description, $isReference);
861: }
862:
863: return new Ast\PhpDoc\TypelessParamTagValueNode($isVariadic, $parameterName, $description, $isReference);
864: }
865:
866: private function parseParamImmediatelyInvokedCallableTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamImmediatelyInvokedCallableTagValueNode
867: {
868: $parameterName = $this->parseRequiredVariableName($tokens);
869: $description = $this->parseOptionalDescription($tokens, false);
870:
871: return new Ast\PhpDoc\ParamImmediatelyInvokedCallableTagValueNode($parameterName, $description);
872: }
873:
874: private function parseParamLaterInvokedCallableTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamLaterInvokedCallableTagValueNode
875: {
876: $parameterName = $this->parseRequiredVariableName($tokens);
877: $description = $this->parseOptionalDescription($tokens, false);
878:
879: return new Ast\PhpDoc\ParamLaterInvokedCallableTagValueNode($parameterName, $description);
880: }
881:
882: private function parseParamClosureThisTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamClosureThisTagValueNode
883: {
884: $type = $this->typeParser->parse($tokens);
885: $parameterName = $this->parseRequiredVariableName($tokens);
886: $description = $this->parseOptionalDescription($tokens, false);
887:
888: return new Ast\PhpDoc\ParamClosureThisTagValueNode($type, $parameterName, $description);
889: }
890:
891: private function parsePureUnlessCallableIsImpureTagValue(TokenIterator $tokens): Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode
892: {
893: $parameterName = $this->parseRequiredVariableName($tokens);
894: $description = $this->parseOptionalDescription($tokens, false);
895:
896: return new Ast\PhpDoc\PureUnlessCallableIsImpureTagValueNode($parameterName, $description);
897: }
898:
899: private function parsePureUnlessParameterIsPassed(TokenIterator $tokens): Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode
900: {
901: $parameterName = $this->parseRequiredVariableName($tokens);
902: $description = $this->parseOptionalDescription($tokens, false);
903:
904: return new Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode($parameterName, $description);
905: }
906:
907: private function parseVarTagValue(TokenIterator $tokens): Ast\PhpDoc\VarTagValueNode
908: {
909: $type = $this->typeParser->parse($tokens);
910: $variableName = $this->parseOptionalVariableName($tokens);
911: $description = $this->parseOptionalDescription($tokens, $variableName === '');
912: return new Ast\PhpDoc\VarTagValueNode($type, $variableName, $description);
913: }
914:
915: private function parseReturnTagValue(TokenIterator $tokens): Ast\PhpDoc\ReturnTagValueNode
916: {
917: $type = $this->typeParser->parse($tokens);
918: $description = $this->parseOptionalDescription($tokens, true);
919: return new Ast\PhpDoc\ReturnTagValueNode($type, $description);
920: }
921:
922: private function parseThrowsTagValue(TokenIterator $tokens): Ast\PhpDoc\ThrowsTagValueNode
923: {
924: $type = $this->typeParser->parse($tokens);
925: $description = $this->parseOptionalDescription($tokens, true);
926: return new Ast\PhpDoc\ThrowsTagValueNode($type, $description);
927: }
928:
929: private function parseMixinTagValue(TokenIterator $tokens): Ast\PhpDoc\MixinTagValueNode
930: {
931: $type = $this->typeParser->parse($tokens);
932: $description = $this->parseOptionalDescription($tokens, true);
933: return new Ast\PhpDoc\MixinTagValueNode($type, $description);
934: }
935:
936: private function parseRequireExtendsTagValue(TokenIterator $tokens): Ast\PhpDoc\RequireExtendsTagValueNode
937: {
938: $type = $this->typeParser->parse($tokens);
939: $description = $this->parseOptionalDescription($tokens, true);
940: return new Ast\PhpDoc\RequireExtendsTagValueNode($type, $description);
941: }
942:
943: private function parseRequireImplementsTagValue(TokenIterator $tokens): Ast\PhpDoc\RequireImplementsTagValueNode
944: {
945: $type = $this->typeParser->parse($tokens);
946: $description = $this->parseOptionalDescription($tokens, true);
947: return new Ast\PhpDoc\RequireImplementsTagValueNode($type, $description);
948: }
949:
950: private function parseSealedTagValue(TokenIterator $tokens): Ast\PhpDoc\SealedTagValueNode
951: {
952: $type = $this->typeParser->parse($tokens);
953: $description = $this->parseOptionalDescription($tokens, true);
954: return new Ast\PhpDoc\SealedTagValueNode($type, $description);
955: }
956:
957: private function parseDeprecatedTagValue(TokenIterator $tokens): Ast\PhpDoc\DeprecatedTagValueNode
958: {
959: $description = $this->parseOptionalDescription($tokens, false);
960: return new Ast\PhpDoc\DeprecatedTagValueNode($description);
961: }
962:
963: private function parsePropertyTagValue(TokenIterator $tokens): Ast\PhpDoc\PropertyTagValueNode
964: {
965: $type = $this->typeParser->parse($tokens);
966: $parameterName = $this->parseRequiredVariableName($tokens);
967: $description = $this->parseOptionalDescription($tokens, false);
968: return new Ast\PhpDoc\PropertyTagValueNode($type, $parameterName, $description);
969: }
970:
971: private function parseMethodTagValue(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueNode
972: {
973: $staticKeywordOrReturnTypeOrMethodName = $this->typeParser->parse($tokens);
974:
975: if ($staticKeywordOrReturnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode && $staticKeywordOrReturnTypeOrMethodName->name === 'static') {
976: $isStatic = true;
977: $returnTypeOrMethodName = $this->typeParser->parse($tokens);
978:
979: } else {
980: $isStatic = false;
981: $returnTypeOrMethodName = $staticKeywordOrReturnTypeOrMethodName;
982: }
983:
984: if ($tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
985: $returnType = $returnTypeOrMethodName;
986: $methodName = $tokens->currentTokenValue();
987: $tokens->next();
988:
989: } elseif ($returnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode) {
990: $returnType = $isStatic ? $staticKeywordOrReturnTypeOrMethodName : null;
991: $methodName = $returnTypeOrMethodName->name;
992: $isStatic = false;
993:
994: } else {
995: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); // will throw exception
996: exit;
997: }
998:
999: $templateTypes = [];
1000:
1001: if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
1002: do {
1003: $startLine = $tokens->currentTokenLine();
1004: $startIndex = $tokens->currentTokenIndex();
1005: $templateTypes[] = $this->enrichWithAttributes(
1006: $tokens,
1007: $this->typeParser->parseTemplateTagValue($tokens),
1008: $startLine,
1009: $startIndex,
1010: );
1011: } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
1012: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
1013: }
1014:
1015: $parameters = [];
1016: $tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
1017: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
1018: $parameters[] = $this->parseMethodTagValueParameter($tokens);
1019: while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
1020: $parameters[] = $this->parseMethodTagValueParameter($tokens);
1021: }
1022: }
1023: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
1024:
1025: $description = $this->parseOptionalDescription($tokens, false);
1026: return new Ast\PhpDoc\MethodTagValueNode($isStatic, $returnType, $methodName, $parameters, $description, $templateTypes);
1027: }
1028:
1029: private function parseMethodTagValueParameter(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueParameterNode
1030: {
1031: $startLine = $tokens->currentTokenLine();
1032: $startIndex = $tokens->currentTokenIndex();
1033:
1034: switch ($tokens->currentTokenType()) {
1035: case Lexer::TOKEN_IDENTIFIER:
1036: case Lexer::TOKEN_OPEN_PARENTHESES:
1037: case Lexer::TOKEN_NULLABLE:
1038: $parameterType = $this->typeParser->parse($tokens);
1039: break;
1040:
1041: default:
1042: $parameterType = null;
1043: }
1044:
1045: $isReference = $tokens->tryConsumeTokenType(Lexer::TOKEN_REFERENCE);
1046: $isVariadic = $tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC);
1047:
1048: $parameterName = $tokens->currentTokenValue();
1049: $tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
1050:
1051: if ($tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL)) {
1052: $defaultValue = $this->constantExprParser->parse($tokens);
1053:
1054: } else {
1055: $defaultValue = null;
1056: }
1057:
1058: return $this->enrichWithAttributes(
1059: $tokens,
1060: new Ast\PhpDoc\MethodTagValueParameterNode($parameterType, $isReference, $isVariadic, $parameterName, $defaultValue),
1061: $startLine,
1062: $startIndex,
1063: );
1064: }
1065:
1066: private function parseExtendsTagValue(string $tagName, TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
1067: {
1068: $startLine = $tokens->currentTokenLine();
1069: $startIndex = $tokens->currentTokenIndex();
1070: $baseType = new IdentifierTypeNode($tokens->currentTokenValue());
1071: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1072:
1073: $type = $this->typeParser->parseGeneric(
1074: $tokens,
1075: $this->typeParser->enrichWithAttributes($tokens, $baseType, $startLine, $startIndex),
1076: );
1077:
1078: $description = $this->parseOptionalDescription($tokens, true);
1079:
1080: switch ($tagName) {
1081: case '@extends':
1082: return new Ast\PhpDoc\ExtendsTagValueNode($type, $description);
1083: case '@implements':
1084: return new Ast\PhpDoc\ImplementsTagValueNode($type, $description);
1085: case '@use':
1086: return new Ast\PhpDoc\UsesTagValueNode($type, $description);
1087: }
1088:
1089: throw new ShouldNotHappenException();
1090: }
1091:
1092: private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeAliasTagValueNode
1093: {
1094: $alias = $tokens->currentTokenValue();
1095: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1096:
1097: $templateTypes = $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)
1098: ? $this->typeParser->parseTemplateTypes($tokens)
1099: : [];
1100:
1101: // support phan-type/psalm-type syntax
1102: $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
1103:
1104: $startLine = $tokens->currentTokenLine();
1105: $startIndex = $tokens->currentTokenIndex();
1106: try {
1107: $type = $this->typeParser->parse($tokens);
1108: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
1109: if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
1110: throw new ParserException(
1111: $tokens->currentTokenValue(),
1112: $tokens->currentTokenType(),
1113: $tokens->currentTokenOffset(),
1114: Lexer::TOKEN_PHPDOC_EOL,
1115: null,
1116: $tokens->currentTokenLine(),
1117: );
1118: }
1119: }
1120:
1121: return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $templateTypes);
1122: } catch (ParserException $e) {
1123: $this->parseOptionalDescription($tokens, false);
1124: return new Ast\PhpDoc\TypeAliasTagValueNode(
1125: $alias,
1126: $this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex),
1127: $templateTypes,
1128: );
1129: }
1130: }
1131:
1132: private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeAliasImportTagValueNode
1133: {
1134: $importedAlias = $tokens->currentTokenValue();
1135: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1136:
1137: $tokens->consumeTokenValue(Lexer::TOKEN_IDENTIFIER, 'from');
1138:
1139: $identifierStartLine = $tokens->currentTokenLine();
1140: $identifierStartIndex = $tokens->currentTokenIndex();
1141: $importedFrom = $tokens->currentTokenValue();
1142: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1143: $importedFromType = $this->enrichWithAttributes(
1144: $tokens,
1145: new IdentifierTypeNode($importedFrom),
1146: $identifierStartLine,
1147: $identifierStartIndex,
1148: );
1149:
1150: $importedAs = null;
1151: if ($tokens->tryConsumeTokenValue('as')) {
1152: $importedAs = $tokens->currentTokenValue();
1153: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1154: }
1155:
1156: return new Ast\PhpDoc\TypeAliasImportTagValueNode($importedAlias, $importedFromType, $importedAs);
1157: }
1158:
1159: /**
1160: * @return Ast\PhpDoc\AssertTagValueNode|Ast\PhpDoc\AssertTagPropertyValueNode|Ast\PhpDoc\AssertTagMethodValueNode
1161: */
1162: private function parseAssertTagValue(TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
1163: {
1164: $isNegated = $tokens->tryConsumeTokenType(Lexer::TOKEN_NEGATED);
1165: $isEquality = $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
1166: $type = $this->typeParser->parse($tokens);
1167: $parameter = $this->parseAssertParameter($tokens);
1168: $description = $this->parseOptionalDescription($tokens, false);
1169:
1170: if (array_key_exists('method', $parameter)) {
1171: return new Ast\PhpDoc\AssertTagMethodValueNode($type, $parameter['parameter'], $parameter['method'], $isNegated, $description, $isEquality);
1172: } elseif (array_key_exists('property', $parameter)) {
1173: return new Ast\PhpDoc\AssertTagPropertyValueNode($type, $parameter['parameter'], $parameter['property'], $isNegated, $description, $isEquality);
1174: }
1175:
1176: return new Ast\PhpDoc\AssertTagValueNode($type, $parameter['parameter'], $isNegated, $description, $isEquality);
1177: }
1178:
1179: /**
1180: * @return array{parameter: string}|array{parameter: string, property: string}|array{parameter: string, method: string}
1181: */
1182: private function parseAssertParameter(TokenIterator $tokens): array
1183: {
1184: if ($tokens->isCurrentTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
1185: $parameter = '$this';
1186: $tokens->next();
1187: } else {
1188: $parameter = $tokens->currentTokenValue();
1189: $tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
1190: }
1191:
1192: if ($tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
1193: $tokens->consumeTokenType(Lexer::TOKEN_ARROW);
1194:
1195: $propertyOrMethod = $tokens->currentTokenValue();
1196: $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
1197:
1198: if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
1199: $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
1200:
1201: return ['parameter' => $parameter, 'method' => $propertyOrMethod];
1202: }
1203:
1204: return ['parameter' => $parameter, 'property' => $propertyOrMethod];
1205: }
1206:
1207: return ['parameter' => $parameter];
1208: }
1209:
1210: private function parseSelfOutTagValue(TokenIterator $tokens): Ast\PhpDoc\SelfOutTagValueNode
1211: {
1212: $type = $this->typeParser->parse($tokens);
1213: $description = $this->parseOptionalDescription($tokens, true);
1214:
1215: return new Ast\PhpDoc\SelfOutTagValueNode($type, $description);
1216: }
1217:
1218: private function parseParamOutTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamOutTagValueNode
1219: {
1220: $type = $this->typeParser->parse($tokens);
1221: $parameterName = $this->parseRequiredVariableName($tokens);
1222: $description = $this->parseOptionalDescription($tokens, false);
1223:
1224: return new Ast\PhpDoc\ParamOutTagValueNode($type, $parameterName, $description);
1225: }
1226:
1227: private function parseOptionalVariableName(TokenIterator $tokens): string
1228: {
1229: if ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {
1230: $parameterName = $tokens->currentTokenValue();
1231: $tokens->next();
1232: } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
1233: $parameterName = '$this';
1234: $tokens->next();
1235:
1236: } else {
1237: $parameterName = '';
1238: }
1239:
1240: return $parameterName;
1241: }
1242:
1243: private function parseRequiredVariableName(TokenIterator $tokens): string
1244: {
1245: $parameterName = $tokens->currentTokenValue();
1246: $tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
1247:
1248: return $parameterName;
1249: }
1250:
1251: /**
1252: * @param bool $limitStartToken true should be used when the description immediately follows a parsed type
1253: */
1254: private function parseOptionalDescription(TokenIterator $tokens, bool $limitStartToken): string
1255: {
1256: if ($limitStartToken) {
1257: foreach (self::DISALLOWED_DESCRIPTION_START_TOKENS as $disallowedStartToken) {
1258: if (!$tokens->isCurrentTokenType($disallowedStartToken)) {
1259: continue;
1260: }
1261:
1262: $tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception
1263: }
1264:
1265: if (
1266: !$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)
1267: && !$tokens->isPrecededByHorizontalWhitespace()
1268: ) {
1269: $tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception
1270: }
1271: }
1272:
1273: return $this->parseText($tokens)->text;
1274: }
1275:
1276: }
1277: