NodeAbstract.php 5.1 KB

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