NodeAbstract.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. abstract class NodeAbstract implements Node, \JsonSerializable {
  4. /** @var array<string, mixed> Attributes */
  5. protected array $attributes;
  6. /**
  7. * Creates a Node.
  8. *
  9. * @param array<string, mixed> $attributes Array of attributes
  10. */
  11. public function __construct(array $attributes = []) {
  12. $this->attributes = $attributes;
  13. }
  14. /**
  15. * Gets line the node started in (alias of getStartLine).
  16. *
  17. * @return int Start line (or -1 if not available)
  18. * @phpstan-return -1|positive-int
  19. */
  20. public function getLine(): int {
  21. return $this->attributes['startLine'] ?? -1;
  22. }
  23. /**
  24. * Gets line the node started in.
  25. *
  26. * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
  27. *
  28. * @return int Start line (or -1 if not available)
  29. * @phpstan-return -1|positive-int
  30. */
  31. public function getStartLine(): int {
  32. return $this->attributes['startLine'] ?? -1;
  33. }
  34. /**
  35. * Gets the line the node ended in.
  36. *
  37. * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
  38. *
  39. * @return int End line (or -1 if not available)
  40. * @phpstan-return -1|positive-int
  41. */
  42. public function getEndLine(): int {
  43. return $this->attributes['endLine'] ?? -1;
  44. }
  45. /**
  46. * Gets the token offset of the first token that is part of this node.
  47. *
  48. * The offset is an index into the array returned by Lexer::getTokens().
  49. *
  50. * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
  51. *
  52. * @return int Token start position (or -1 if not available)
  53. */
  54. public function getStartTokenPos(): int {
  55. return $this->attributes['startTokenPos'] ?? -1;
  56. }
  57. /**
  58. * Gets the token offset of the last token that is part of this node.
  59. *
  60. * The offset is an index into the array returned by Lexer::getTokens().
  61. *
  62. * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
  63. *
  64. * @return int Token end position (or -1 if not available)
  65. */
  66. public function getEndTokenPos(): int {
  67. return $this->attributes['endTokenPos'] ?? -1;
  68. }
  69. /**
  70. * Gets the file offset of the first character that is part of this node.
  71. *
  72. * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
  73. *
  74. * @return int File start position (or -1 if not available)
  75. */
  76. public function getStartFilePos(): int {
  77. return $this->attributes['startFilePos'] ?? -1;
  78. }
  79. /**
  80. * Gets the file offset of the last character that is part of this node.
  81. *
  82. * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
  83. *
  84. * @return int File end position (or -1 if not available)
  85. */
  86. public function getEndFilePos(): int {
  87. return $this->attributes['endFilePos'] ?? -1;
  88. }
  89. /**
  90. * Gets all comments directly preceding this node.
  91. *
  92. * The comments are also available through the "comments" attribute.
  93. *
  94. * @return Comment[]
  95. */
  96. public function getComments(): array {
  97. return $this->attributes['comments'] ?? [];
  98. }
  99. /**
  100. * Gets the doc comment of the node.
  101. *
  102. * @return null|Comment\Doc Doc comment object or null
  103. */
  104. public function getDocComment(): ?Comment\Doc {
  105. $comments = $this->getComments();
  106. for ($i = count($comments) - 1; $i >= 0; $i--) {
  107. $comment = $comments[$i];
  108. if ($comment instanceof Comment\Doc) {
  109. return $comment;
  110. }
  111. }
  112. return null;
  113. }
  114. /**
  115. * Sets the doc comment of the node.
  116. *
  117. * This will either replace an existing doc comment or add it to the comments array.
  118. *
  119. * @param Comment\Doc $docComment Doc comment to set
  120. */
  121. public function setDocComment(Comment\Doc $docComment): void {
  122. $comments = $this->getComments();
  123. for ($i = count($comments) - 1; $i >= 0; $i--) {
  124. if ($comments[$i] instanceof Comment\Doc) {
  125. // Replace existing doc comment.
  126. $comments[$i] = $docComment;
  127. $this->setAttribute('comments', $comments);
  128. return;
  129. }
  130. }
  131. // Append new doc comment.
  132. $comments[] = $docComment;
  133. $this->setAttribute('comments', $comments);
  134. }
  135. public function setAttribute(string $key, $value): void {
  136. $this->attributes[$key] = $value;
  137. }
  138. public function hasAttribute(string $key): bool {
  139. return array_key_exists($key, $this->attributes);
  140. }
  141. public function getAttribute(string $key, $default = null) {
  142. if (array_key_exists($key, $this->attributes)) {
  143. return $this->attributes[$key];
  144. }
  145. return $default;
  146. }
  147. public function getAttributes(): array {
  148. return $this->attributes;
  149. }
  150. public function setAttributes(array $attributes): void {
  151. $this->attributes = $attributes;
  152. }
  153. /**
  154. * @return array<string, mixed>
  155. */
  156. public function jsonSerialize(): array {
  157. return ['nodeType' => $this->getType()] + get_object_vars($this);
  158. }
  159. }