<?php
namespace PhpParser;
use PhpParser\Node;
abstract class NodeAbstract implements Node, \JsonSerializable
{
protected $attributes;
public function __construct(array $attributes = array()) {
$this->attributes = $attributes;
}
public function getType() {
$className = rtrim(get_class($this), '_');
return strtr(
substr($className, strlen(Node::class) + 1),
'\\',
'_'
);
}
public function getLine() {
return $this->getAttribute('startLine', -1);
}
public function setLine($line) {
$this->setAttribute('startLine', (int) $line);
}
public function getDocComment() {
$comments = $this->getAttribute('comments');
if (!$comments) {
return null;
}
$lastComment = $comments[count($comments) - 1];
if (!$lastComment instanceof Comment\Doc) {
return null;
}
return $lastComment;
}
public function setDocComment(Comment\Doc $docComment) {
$comments = $this->getAttribute('comments', []);
$numComments = count($comments);
if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
$comments[$numComments - 1] = $docComment;
} else {
$comments[] = $docComment;
}
$this->setAttribute('comments', $comments);
}
public function setAttribute($key, $value) {
$this->attributes[$key] = $value;
}
public function hasAttribute($key) {
return array_key_exists($key, $this->attributes);
}
public function &getAttribute($key, $default = null) {
if (!array_key_exists($key, $this->attributes)) {
return $default;
} else {
return $this->attributes[$key];
}
}
public function getAttributes() {
return $this->attributes;
}
public function jsonSerialize() {
return ['nodeType' => $this->getType()] + get_object_vars($this);
}
}