Node.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. interface Node {
  4. /**
  5. * Gets the type of the node.
  6. *
  7. * @psalm-return non-empty-string
  8. * @return string Type of the node
  9. */
  10. public function getType(): string;
  11. /**
  12. * Gets the names of the sub nodes.
  13. *
  14. * @return string[] Names of sub nodes
  15. */
  16. public function getSubNodeNames(): array;
  17. /**
  18. * Gets line the node started in (alias of getStartLine).
  19. *
  20. * @return int Start line (or -1 if not available)
  21. * @phpstan-return -1|positive-int
  22. *
  23. * @deprecated Use getStartLine() instead
  24. */
  25. public function getLine(): int;
  26. /**
  27. * Gets line the node started in.
  28. *
  29. * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
  30. *
  31. * @return int Start line (or -1 if not available)
  32. * @phpstan-return -1|positive-int
  33. */
  34. public function getStartLine(): int;
  35. /**
  36. * Gets the line the node ended in.
  37. *
  38. * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
  39. *
  40. * @return int End line (or -1 if not available)
  41. * @phpstan-return -1|positive-int
  42. */
  43. public function getEndLine(): int;
  44. /**
  45. * Gets the token offset of the first token that is part of this node.
  46. *
  47. * The offset is an index into the array returned by Lexer::getTokens().
  48. *
  49. * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
  50. *
  51. * @return int Token start position (or -1 if not available)
  52. */
  53. public function getStartTokenPos(): int;
  54. /**
  55. * Gets the token offset of the last token that is part of this node.
  56. *
  57. * The offset is an index into the array returned by Lexer::getTokens().
  58. *
  59. * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
  60. *
  61. * @return int Token end position (or -1 if not available)
  62. */
  63. public function getEndTokenPos(): int;
  64. /**
  65. * Gets the file offset of the first character that is part of this node.
  66. *
  67. * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
  68. *
  69. * @return int File start position (or -1 if not available)
  70. */
  71. public function getStartFilePos(): int;
  72. /**
  73. * Gets the file offset of the last character that is part of this node.
  74. *
  75. * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
  76. *
  77. * @return int File end position (or -1 if not available)
  78. */
  79. public function getEndFilePos(): int;
  80. /**
  81. * Gets all comments directly preceding this node.
  82. *
  83. * The comments are also available through the "comments" attribute.
  84. *
  85. * @return Comment[]
  86. */
  87. public function getComments(): array;
  88. /**
  89. * Gets the doc comment of the node.
  90. *
  91. * @return null|Comment\Doc Doc comment object or null
  92. */
  93. public function getDocComment(): ?Comment\Doc;
  94. /**
  95. * Sets the doc comment of the node.
  96. *
  97. * This will either replace an existing doc comment or add it to the comments array.
  98. *
  99. * @param Comment\Doc $docComment Doc comment to set
  100. */
  101. public function setDocComment(Comment\Doc $docComment): void;
  102. /**
  103. * Sets an attribute on a node.
  104. *
  105. * @param mixed $value
  106. */
  107. public function setAttribute(string $key, $value): void;
  108. /**
  109. * Returns whether an attribute exists.
  110. */
  111. public function hasAttribute(string $key): bool;
  112. /**
  113. * Returns the value of an attribute.
  114. *
  115. * @param mixed $default
  116. *
  117. * @return mixed
  118. */
  119. public function getAttribute(string $key, $default = null);
  120. /**
  121. * Returns all the attributes of this node.
  122. *
  123. * @return array<string, mixed>
  124. */
  125. public function getAttributes(): array;
  126. /**
  127. * Replaces all the attributes of this node.
  128. *
  129. * @param array<string, mixed> $attributes
  130. */
  131. public function setAttributes(array $attributes): void;
  132. }