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

Breadcrumb

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

function CommentAnnotatingVisitor::enterNode

Overrides NodeVisitorAbstract::enterNode

File

vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php, line 35

Class

CommentAnnotatingVisitor

Namespace

PhpParser\NodeVisitor

Code

public function enterNode(Node $node) {
    $nextCommentPos = current($this->commentPositions);
    if ($nextCommentPos === false) {
        // No more comments.
        return self::STOP_TRAVERSAL;
    }
    $oldPos = $this->pos;
    $this->pos = $pos = $node->getStartTokenPos();
    if ($nextCommentPos > $oldPos && $nextCommentPos < $pos) {
        $comments = [];
        while (--$pos >= $oldPos) {
            $token = $this->tokens[$pos];
            if ($token->id === \T_DOC_COMMENT) {
                $comments[] = new Comment\Doc($token->text, $token->line, $token->pos, $pos, $token->getEndLine(), $token->getEndPos() - 1, $pos);
                continue;
            }
            if ($token->id === \T_COMMENT) {
                $comments[] = new Comment($token->text, $token->line, $token->pos, $pos, $token->getEndLine(), $token->getEndPos() - 1, $pos);
                continue;
            }
            if ($token->id !== \T_WHITESPACE) {
                break;
            }
        }
        if (!empty($comments)) {
            $node->setAttribute('comments', array_reverse($comments));
        }
        do {
            $nextCommentPos = next($this->commentPositions);
        } while ($nextCommentPos !== false && $nextCommentPos < $this->pos);
    }
    $endPos = $node->getEndTokenPos();
    if ($nextCommentPos > $endPos) {
        // Skip children if there are no comments located inside this node.
        $this->pos = $endPos;
        return self::DONT_TRAVERSE_CHILDREN;
    }
    return null;
}
RSS feed
Powered by Drupal