NodeAbstract.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggregate
  3. {
  4. protected $subNodes;
  5. protected $attributes;
  6. /**
  7. * Creates a Node.
  8. *
  9. * @param array $subNodes Array of sub nodes
  10. * @param array $attributes Array of attributes
  11. */
  12. public function __construct(array $subNodes = array(), array $attributes = array()) {
  13. $this->subNodes = $subNodes;
  14. $this->attributes = $attributes;
  15. }
  16. /**
  17. * Gets the type of the node.
  18. *
  19. * @return string Type of the node
  20. */
  21. public function getType() {
  22. return substr(get_class($this), 15);
  23. }
  24. /**
  25. * Gets the names of the sub nodes.
  26. *
  27. * @return array Names of sub nodes
  28. */
  29. public function getSubNodeNames() {
  30. return array_keys($this->subNodes);
  31. }
  32. /**
  33. * Gets line the node started in.
  34. *
  35. * @return int Line
  36. */
  37. public function getLine() {
  38. return $this->getAttribute('startLine', -1);
  39. }
  40. /**
  41. * Sets line the node started in.
  42. *
  43. * @param int $line Line
  44. */
  45. public function setLine($line) {
  46. $this->setAttribute('startLine', (int) $line);
  47. }
  48. /**
  49. * Gets the doc comment of the node.
  50. *
  51. * The doc comment has to be the last comment associated with the node.
  52. *
  53. * @return null|PHPParser_Comment_Doc Doc comment object or null
  54. */
  55. public function getDocComment() {
  56. $comments = $this->getAttribute('comments');
  57. if (!$comments) {
  58. return null;
  59. }
  60. $lastComment = $comments[count($comments) - 1];
  61. if (!$lastComment instanceof PHPParser_Comment_Doc) {
  62. return null;
  63. }
  64. return $lastComment;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function setAttribute($key, $value) {
  70. $this->attributes[$key] = $value;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function hasAttribute($key) {
  76. return array_key_exists($key, $this->attributes);
  77. }
  78. /**
  79. * {@inheritDoc}
  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. /**
  89. * {@inheritDoc}
  90. */
  91. public function getAttributes() {
  92. return $this->attributes;
  93. }
  94. /* Magic interfaces */
  95. public function &__get($name) {
  96. return $this->subNodes[$name];
  97. }
  98. public function __set($name, $value) {
  99. $this->subNodes[$name] = $value;
  100. }
  101. public function __isset($name) {
  102. return isset($this->subNodes[$name]);
  103. }
  104. public function __unset($name) {
  105. unset($this->subNodes[$name]);
  106. }
  107. public function getIterator() {
  108. return new ArrayIterator($this->subNodes);
  109. }
  110. }