NodeAbstract.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace PhpParser;
  3. abstract class NodeAbstract implements Node
  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. public function setAttribute($key, $value) {
  57. $this->attributes[$key] = $value;
  58. }
  59. public function hasAttribute($key) {
  60. return array_key_exists($key, $this->attributes);
  61. }
  62. public function &getAttribute($key, $default = null) {
  63. if (!array_key_exists($key, $this->attributes)) {
  64. return $default;
  65. } else {
  66. return $this->attributes[$key];
  67. }
  68. }
  69. public function getAttributes() {
  70. return $this->attributes;
  71. }
  72. }