菜谱项目

NodeAbstract.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @deprecated
  36. */
  37. public function setLine($line) {
  38. $this->setAttribute('startLine', (int) $line);
  39. }
  40. /**
  41. * Gets the doc comment of the node.
  42. *
  43. * The doc comment has to be the last comment associated with the node.
  44. *
  45. * @return null|Comment\Doc Doc comment object or null
  46. */
  47. public function getDocComment() {
  48. $comments = $this->getAttribute('comments');
  49. if (!$comments) {
  50. return null;
  51. }
  52. $lastComment = $comments[count($comments) - 1];
  53. if (!$lastComment instanceof Comment\Doc) {
  54. return null;
  55. }
  56. return $lastComment;
  57. }
  58. /**
  59. * Sets the doc comment of the node.
  60. *
  61. * This will either replace an existing doc comment or add it to the comments array.
  62. *
  63. * @param Comment\Doc $docComment Doc comment to set
  64. */
  65. public function setDocComment(Comment\Doc $docComment) {
  66. $comments = $this->getAttribute('comments', []);
  67. $numComments = count($comments);
  68. if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
  69. // Replace existing doc comment
  70. $comments[$numComments - 1] = $docComment;
  71. } else {
  72. // Append new comment
  73. $comments[] = $docComment;
  74. }
  75. $this->setAttribute('comments', $comments);
  76. }
  77. public function setAttribute($key, $value) {
  78. $this->attributes[$key] = $value;
  79. }
  80. public function hasAttribute($key) {
  81. return array_key_exists($key, $this->attributes);
  82. }
  83. public function &getAttribute($key, $default = null) {
  84. if (!array_key_exists($key, $this->attributes)) {
  85. return $default;
  86. } else {
  87. return $this->attributes[$key];
  88. }
  89. }
  90. public function getAttributes() {
  91. return $this->attributes;
  92. }
  93. public function jsonSerialize() {
  94. return ['nodeType' => $this->getType()] + get_object_vars($this);
  95. }
  96. }