class CommentAnnotatingVisitor
Hierarchy
- class \PhpParser\NodeVisitorAbstract implements \PhpParser\NodeVisitor
- class \PhpParser\NodeVisitor\CommentAnnotatingVisitor extends \PhpParser\NodeVisitorAbstract
Expanded class hierarchy of CommentAnnotatingVisitor
1 file declares its use of CommentAnnotatingVisitor
- ParserAbstract.php in vendor/
nikic/ php-parser/ lib/ PhpParser/ ParserAbstract.php
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ NodeVisitor/ CommentAnnotatingVisitor.php, line 10
Namespace
PhpParser\NodeVisitorView source
class CommentAnnotatingVisitor extends NodeVisitorAbstract {
/** @var int Last seen token start position */
private int $pos = 0;
/** @var Token[] Token array */
private array $tokens;
/** @var list<int> Token positions of comments */
private array $commentPositions = [];
/**
* Create a comment annotation visitor.
*
* @param Token[] $tokens Token array
*/
public function __construct(array $tokens) {
$this->tokens = $tokens;
// Collect positions of comments. We use this to avoid traversing parts of the AST where
// there are no comments.
foreach ($tokens as $i => $token) {
if ($token->id === \T_COMMENT || $token->id === \T_DOC_COMMENT) {
$this->commentPositions[] = $i;
}
}
}
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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
CommentAnnotatingVisitor::$commentPositions | private | property | @var list<int> Token positions of comments | ||
CommentAnnotatingVisitor::$pos | private | property | @var int Last seen token start position | ||
CommentAnnotatingVisitor::$tokens | private | property | @var Token[] Token array | ||
CommentAnnotatingVisitor::enterNode | public | function | Called when entering a node. | Overrides NodeVisitorAbstract::enterNode | |
CommentAnnotatingVisitor::__construct | public | function | Create a comment annotation visitor. | ||
NodeVisitor::DONT_TRAVERSE_CHILDREN | public | constant | If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes of the current node will not be traversed for any visitors. |
||
NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN | public | constant | If NodeVisitor::enterNode() returns DONT_TRAVERSE_CURRENT_AND_CHILDREN, child nodes of the current node will not be traversed for any visitors. |
||
NodeVisitor::REMOVE_NODE | public | constant | If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs in an array, it will be removed from the array. |
||
NodeVisitor::REPLACE_WITH_NULL | public | constant | If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns REPLACE_WITH_NULL, the node will be replaced with null. This is not a legal return value if the node is part of an array, rather than another node. |
||
NodeVisitor::STOP_TRAVERSAL | public | constant | If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns STOP_TRAVERSAL, traversal is aborted. |
||
NodeVisitorAbstract::afterTraverse | public | function | Called once after traversal. | Overrides NodeVisitor::afterTraverse | 1 |
NodeVisitorAbstract::beforeTraverse | public | function | Called once before traversal. | Overrides NodeVisitor::beforeTraverse | 5 |
NodeVisitorAbstract::leaveNode | public | function | Called when leaving a node. | Overrides NodeVisitor::leaveNode | 2 |