7 abstract class NodeAbstract implements Node, \JsonSerializable
14 * @param array $attributes Array of attributes
16 public function __construct(array $attributes = array()) {
17 $this->attributes = $attributes;
21 * Gets the type of the node.
23 * @return string Type of the node
25 public function getType() {
26 $className = rtrim(get_class($this), '_');
28 substr($className, strlen(Node::class) + 1),
35 * Gets line the node started in.
39 public function getLine() {
40 return $this->getAttribute('startLine', -1);
44 * Sets line the node started in.
46 * @param int $line Line
50 public function setLine($line) {
51 $this->setAttribute('startLine', (int) $line);
55 * Gets the doc comment of the node.
57 * The doc comment has to be the last comment associated with the node.
59 * @return null|Comment\Doc Doc comment object or null
61 public function getDocComment() {
62 $comments = $this->getAttribute('comments');
67 $lastComment = $comments[count($comments) - 1];
68 if (!$lastComment instanceof Comment\Doc) {
76 * Sets the doc comment of the node.
78 * This will either replace an existing doc comment or add it to the comments array.
80 * @param Comment\Doc $docComment Doc comment to set
82 public function setDocComment(Comment\Doc $docComment) {
83 $comments = $this->getAttribute('comments', []);
85 $numComments = count($comments);
86 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
87 // Replace existing doc comment
88 $comments[$numComments - 1] = $docComment;
91 $comments[] = $docComment;
94 $this->setAttribute('comments', $comments);
97 public function setAttribute($key, $value) {
98 $this->attributes[$key] = $value;
101 public function hasAttribute($key) {
102 return array_key_exists($key, $this->attributes);
105 public function &getAttribute($key, $default = null) {
106 if (!array_key_exists($key, $this->attributes)) {
109 return $this->attributes[$key];
113 public function getAttributes() {
114 return $this->attributes;
117 public function jsonSerialize() {
118 return ['nodeType' => $this->getType()] + get_object_vars($this);