class NodeConnectingVisitor
Visitor that connects a child node to its parent node as well as its sibling nodes.
On the child node, the parent node can be accessed through <code>$node->getAttribute('parent')</code>, the previous node can be accessed through <code>$node->getAttribute('previous')</code>, and the next node can be accessed through <code>$node->getAttribute('next')</code>.
Hierarchy
- class \PhpParser\NodeVisitorAbstract implements \PhpParser\NodeVisitor
- class \PhpParser\NodeVisitor\NodeConnectingVisitor extends \PhpParser\NodeVisitorAbstract
Expanded class hierarchy of NodeConnectingVisitor
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ NodeVisitor/ NodeConnectingVisitor.php, line 17
Namespace
PhpParser\NodeVisitorView source
final class NodeConnectingVisitor extends NodeVisitorAbstract {
/**
* @var Node[]
*/
private array $stack = [];
/**
* @var ?Node
*/
private $previous;
public function beforeTraverse(array $nodes) {
$this->stack = [];
$this->previous = null;
}
public function enterNode(Node $node) {
if (!empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
}
if ($this->previous !== null && $this->previous
->getAttribute('parent') === $node->getAttribute('parent')) {
$node->setAttribute('previous', $this->previous);
$this->previous
->setAttribute('next', $node);
}
$this->stack[] = $node;
}
public function leaveNode(Node $node) {
$this->previous = $node;
array_pop($this->stack);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
NodeConnectingVisitor::$previous | private | property | |||
NodeConnectingVisitor::$stack | private | property | |||
NodeConnectingVisitor::beforeTraverse | public | function | Called once before traversal. | Overrides NodeVisitorAbstract::beforeTraverse | |
NodeConnectingVisitor::enterNode | public | function | Called when entering a node. | Overrides NodeVisitorAbstract::enterNode | |
NodeConnectingVisitor::leaveNode | public | function | Called when leaving a node. | Overrides NodeVisitorAbstract::leaveNode | |
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 |