Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. PhpDocParser.php

function PhpDocParser::parse

File

vendor/phpstan/phpdoc-parser/src/Parser/PhpDocParser.php, line 84

Class

PhpDocParser
@phpstan-import-type ValueType from Doctrine\DoctrineArgument as DoctrineValueType

Namespace

PHPStan\PhpDocParser\Parser

Code

public function parse(TokenIterator $tokens) : Ast\PhpDoc\PhpDocNode {
    $tokens->consumeTokenType(Lexer::TOKEN_OPEN_PHPDOC);
    $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
    $children = [];
    if ($this->parseDoctrineAnnotations) {
        if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
            $lastChild = $this->parseChild($tokens);
            $children[] = $lastChild;
            while (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
                if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode && ($lastChild->value instanceof Doctrine\DoctrineTagValueNode || $lastChild->value instanceof Ast\PhpDoc\GenericTagValueNode)) {
                    $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
                    if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
                        break;
                    }
                    $lastChild = $this->parseChild($tokens);
                    $children[] = $lastChild;
                    continue;
                }
                if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
                    break;
                }
                if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
                    break;
                }
                $lastChild = $this->parseChild($tokens);
                $children[] = $lastChild;
            }
        }
    }
    else {
        if (!$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
            $children[] = $this->parseChild($tokens);
            while ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL) && !$tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
                $children[] = $this->parseChild($tokens);
            }
        }
    }
    try {
        $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
    } catch (ParserException $e) {
        $name = '';
        $startLine = $tokens->currentTokenLine();
        $startIndex = $tokens->currentTokenIndex();
        if (count($children) > 0) {
            $lastChild = $children[count($children) - 1];
            if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode) {
                $name = $lastChild->name;
                $startLine = $tokens->currentTokenLine();
                $startIndex = $tokens->currentTokenIndex();
            }
        }
        $tag = new Ast\PhpDoc\PhpDocTagNode($name, $this->enrichWithAttributes($tokens, new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e), $startLine, $startIndex));
        $tokens->forwardToTheEnd();
        return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode([
            $this->enrichWithAttributes($tokens, $tag, $startLine, $startIndex),
        ]), 1, 0);
    }
    return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\PhpDocNode(array_values($children)), 1, 0);
}
RSS feed
Powered by Drupal