NodeAbstract.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace PhpParser;
  3. abstract class NodeAbstract implements Node
  4. {
  5. private $subNodeNames;
  6. protected $attributes;
  7. /**
  8. * Creates a Node.
  9. *
  10. * If null is passed for the $subNodes parameter the node constructor must assign
  11. * all subnodes by itself and also override the getSubNodeNames() method.
  12. * DEPRECATED: If an array is passed as $subNodes instead, the properties corresponding
  13. * to the array keys will be set and getSubNodeNames() will return the keys of that
  14. * array.
  15. *
  16. * @param null|array $subNodes Null or an array of sub nodes (deprecated)
  17. * @param array $attributes Array of attributes
  18. */
  19. public function __construct($subNodes = array(), array $attributes = array()) {
  20. $this->attributes = $attributes;
  21. if (null !== $subNodes) {
  22. foreach ($subNodes as $name => $value) {
  23. $this->$name = $value;
  24. }
  25. $this->subNodeNames = array_keys($subNodes);
  26. }
  27. }
  28. /**
  29. * Gets the type of the node.
  30. *
  31. * @return string Type of the node
  32. */
  33. public function getType() {
  34. return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
  35. }
  36. /**
  37. * Gets the names of the sub nodes.
  38. *
  39. * @return array Names of sub nodes
  40. */
  41. public function getSubNodeNames() {
  42. return $this->subNodeNames;
  43. }
  44. /**
  45. * Gets line the node started in.
  46. *
  47. * @return int Line
  48. */
  49. public function getLine() {
  50. return $this->getAttribute('startLine', -1);
  51. }
  52. /**
  53. * Sets line the node started in.
  54. *
  55. * @param int $line Line
  56. */
  57. public function setLine($line) {
  58. $this->setAttribute('startLine', (int) $line);
  59. }
  60. /**
  61. * Gets the doc comment of the node.
  62. *
  63. * The doc comment has to be the last comment associated with the node.
  64. *
  65. * @return null|Comment\Doc Doc comment object or null
  66. */
  67. public function getDocComment() {
  68. $comments = $this->getAttribute('comments');
  69. if (!$comments) {
  70. return null;
  71. }
  72. $lastComment = $comments[count($comments) - 1];
  73. if (!$lastComment instanceof Comment\Doc) {
  74. return null;
  75. }
  76. return $lastComment;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function setAttribute($key, $value) {
  82. $this->attributes[$key] = $value;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function hasAttribute($key) {
  88. return array_key_exists($key, $this->attributes);
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function &getAttribute($key, $default = null) {
  94. if (!array_key_exists($key, $this->attributes)) {
  95. return $default;
  96. } else {
  97. return $this->attributes[$key];
  98. }
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function getAttributes() {
  104. return $this->attributes;
  105. }
  106. }