1 <?php declare(strict_types=1);
7 abstract class NodeAbstract implements Node, \JsonSerializable
14 * @param array $attributes Array of attributes
16 public function __construct(array $attributes = []) {
17 $this->attributes = $attributes;
21 * Gets line the node started in (alias of getStartLine).
23 * @return int Start line (or -1 if not available)
25 public function getLine() : int {
26 return $this->attributes['startLine'] ?? -1;
30 * Gets line the node started in.
32 * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
34 * @return int Start line (or -1 if not available)
36 public function getStartLine() : int {
37 return $this->attributes['startLine'] ?? -1;
41 * Gets the line the node ended in.
43 * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
45 * @return int End line (or -1 if not available)
47 public function getEndLine() : int {
48 return $this->attributes['endLine'] ?? -1;
52 * Gets the token offset of the first token that is part of this node.
54 * The offset is an index into the array returned by Lexer::getTokens().
56 * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
58 * @return int Token start position (or -1 if not available)
60 public function getStartTokenPos() : int {
61 return $this->attributes['startTokenPos'] ?? -1;
65 * Gets the token offset of the last token that is part of this node.
67 * The offset is an index into the array returned by Lexer::getTokens().
69 * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
71 * @return int Token end position (or -1 if not available)
73 public function getEndTokenPos() : int {
74 return $this->attributes['endTokenPos'] ?? -1;
78 * Gets the file offset of the first character that is part of this node.
80 * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
82 * @return int File start position (or -1 if not available)
84 public function getStartFilePos() : int {
85 return $this->attributes['startFilePos'] ?? -1;
89 * Gets the file offset of the last character that is part of this node.
91 * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
93 * @return int File end position (or -1 if not available)
95 public function getEndFilePos() : int {
96 return $this->attributes['endFilePos'] ?? -1;
100 * Gets all comments directly preceding this node.
102 * The comments are also available through the "comments" attribute.
106 public function getComments() : array {
107 return $this->attributes['comments'] ?? [];
111 * Gets the doc comment of the node.
113 * The doc comment has to be the last comment associated with the node.
115 * @return null|Comment\Doc Doc comment object or null
117 public function getDocComment() {
118 $comments = $this->getComments();
123 $lastComment = $comments[count($comments) - 1];
124 if (!$lastComment instanceof Comment\Doc) {
132 * Sets the doc comment of the node.
134 * This will either replace an existing doc comment or add it to the comments array.
136 * @param Comment\Doc $docComment Doc comment to set
138 public function setDocComment(Comment\Doc $docComment) {
139 $comments = $this->getComments();
141 $numComments = count($comments);
142 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
143 // Replace existing doc comment
144 $comments[$numComments - 1] = $docComment;
146 // Append new comment
147 $comments[] = $docComment;
150 $this->setAttribute('comments', $comments);
153 public function setAttribute(string $key, $value) {
154 $this->attributes[$key] = $value;
157 public function hasAttribute(string $key) : bool {
158 return array_key_exists($key, $this->attributes);
161 public function getAttribute(string $key, $default = null) {
162 if (!array_key_exists($key, $this->attributes)) {
165 return $this->attributes[$key];
169 public function getAttributes() : array {
170 return $this->attributes;
173 public function setAttributes(array $attributes) {
174 $this->attributes = $attributes;
180 public function jsonSerialize() : array {
181 return ['nodeType' => $this->getType()] + get_object_vars($this);