123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php declare(strict_types=1);
- namespace PhpParser;
- abstract class NodeAbstract implements Node, \JsonSerializable
- {
- protected $attributes;
-
- public function __construct(array $attributes = []) {
- $this->attributes = $attributes;
- }
-
- public function getLine() : int {
- return $this->attributes['startLine'] ?? -1;
- }
-
- public function getStartLine() : int {
- return $this->attributes['startLine'] ?? -1;
- }
-
- public function getEndLine() : int {
- return $this->attributes['endLine'] ?? -1;
- }
-
- public function getStartTokenPos() : int {
- return $this->attributes['startTokenPos'] ?? -1;
- }
-
- public function getEndTokenPos() : int {
- return $this->attributes['endTokenPos'] ?? -1;
- }
-
- public function getStartFilePos() : int {
- return $this->attributes['startFilePos'] ?? -1;
- }
-
- public function getEndFilePos() : int {
- return $this->attributes['endFilePos'] ?? -1;
- }
-
- public function getComments() : array {
- return $this->attributes['comments'] ?? [];
- }
-
- public function getDocComment() {
- $comments = $this->getComments();
- for ($i = count($comments) - 1; $i >= 0; $i--) {
- $comment = $comments[$i];
- if ($comment instanceof Comment\Doc) {
- return $comment;
- }
- }
- return null;
- }
-
- public function setDocComment(Comment\Doc $docComment) {
- $comments = $this->getComments();
- for ($i = count($comments) - 1; $i >= 0; $i--) {
- if ($comments[$i] instanceof Comment\Doc) {
-
- $comments[$i] = $docComment;
- $this->setAttribute('comments', $comments);
- return;
- }
- }
-
- $comments[] = $docComment;
- $this->setAttribute('comments', $comments);
- }
- public function setAttribute(string $key, $value) {
- $this->attributes[$key] = $value;
- }
- public function hasAttribute(string $key) : bool {
- return array_key_exists($key, $this->attributes);
- }
- public function getAttribute(string $key, $default = null) {
- if (array_key_exists($key, $this->attributes)) {
- return $this->attributes[$key];
- }
- return $default;
- }
- public function getAttributes() : array {
- return $this->attributes;
- }
- public function setAttributes(array $attributes) {
- $this->attributes = $attributes;
- }
-
- public function jsonSerialize() : array {
- return ['nodeType' => $this->getType()] + get_object_vars($this);
- }
- }
|