NodeAbstract.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace PhpParser;
  3. abstract class NodeAbstract implements Node, \JsonSerializable
  4. {
  5. protected $attributes;
  6. /**
  7. * Creates a Node.
  8. *
  9. * @param array $attributes Array of attributes
  10. */
  11. public function __construct(array $attributes = array()) {
  12. $this->attributes = $attributes;
  13. }
  14. /**
  15. * Gets the type of the node.
  16. *
  17. * @return string Type of the node
  18. */
  19. public function getType() {
  20. return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
  21. }
  22. /**
  23. * Gets line the node started in.
  24. *
  25. * @return int Line
  26. */
  27. public function getLine() {
  28. return $this->getAttribute('startLine', -1);
  29. }
  30. /**
  31. * Sets line the node started in.
  32. *
  33. * @param int $line Line
  34. */
  35. public function setLine($line) {
  36. $this->setAttribute('startLine', (int) $line);
  37. }
  38. /**
  39. * Gets the doc comment of the node.
  40. *
  41. * The doc comment has to be the last comment associated with the node.
  42. *
  43. * @return null|Comment\Doc Doc comment object or null
  44. */
  45. public function getDocComment() {
  46. $comments = $this->getAttribute('comments');
  47. if (!$comments) {
  48. return null;
  49. }
  50. $lastComment = $comments[count($comments) - 1];
  51. if (!$lastComment instanceof Comment\Doc) {
  52. return null;
  53. }
  54. return $lastComment;
  55. }
  56. /**
  57. * Sets the doc comment of the node.
  58. *
  59. * This will either replace an existing doc comment or add it to the comments array.
  60. *
  61. * @param Comment\Doc $docComment Doc comment to set
  62. */
  63. public function setDocComment(Comment\Doc $docComment) {
  64. $comments = $this->getAttribute('comments', []);
  65. $numComments = count($comments);
  66. if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
  67. // Replace existing doc comment
  68. $comments[$numComments - 1] = $docComment;
  69. } else {
  70. // Append new comment
  71. $comments[] = $docComment;
  72. }
  73. $this->setAttribute('comments', $comments);
  74. }
  75. public function setAttribute($key, $value) {
  76. $this->attributes[$key] = $value;
  77. }
  78. public function hasAttribute($key) {
  79. return array_key_exists($key, $this->attributes);
  80. }
  81. public function &getAttribute($key, $default = null) {
  82. if (!array_key_exists($key, $this->attributes)) {
  83. return $default;
  84. } else {
  85. return $this->attributes[$key];
  86. }
  87. }
  88. public function getAttributes() {
  89. return $this->attributes;
  90. }
  91. public function jsonSerialize() {
  92. return ['nodeType' => $this->getType()] + get_object_vars($this);
  93. }
  94. }