Нема описа

Error.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class Error extends \RuntimeException
  4. {
  5. protected $rawMessage;
  6. protected $attributes;
  7. /**
  8. * Creates an Exception signifying a parse error.
  9. *
  10. * @param string $message Error message
  11. * @param array|int $attributes Attributes of node/token where error occurred
  12. * (or start line of error -- deprecated)
  13. */
  14. public function __construct(string $message, $attributes = []) {
  15. $this->rawMessage = $message;
  16. if (is_array($attributes)) {
  17. $this->attributes = $attributes;
  18. } else {
  19. $this->attributes = ['startLine' => $attributes];
  20. }
  21. $this->updateMessage();
  22. }
  23. /**
  24. * Gets the error message
  25. *
  26. * @return string Error message
  27. */
  28. public function getRawMessage() : string {
  29. return $this->rawMessage;
  30. }
  31. /**
  32. * Gets the line the error starts in.
  33. *
  34. * @return int Error start line
  35. */
  36. public function getStartLine() : int {
  37. return $this->attributes['startLine'] ?? -1;
  38. }
  39. /**
  40. * Gets the line the error ends in.
  41. *
  42. * @return int Error end line
  43. */
  44. public function getEndLine() : int {
  45. return $this->attributes['endLine'] ?? -1;
  46. }
  47. /**
  48. * Gets the attributes of the node/token the error occurred at.
  49. *
  50. * @return array
  51. */
  52. public function getAttributes() : array {
  53. return $this->attributes;
  54. }
  55. /**
  56. * Sets the attributes of the node/token the error occurred at.
  57. *
  58. * @param array $attributes
  59. */
  60. public function setAttributes(array $attributes) {
  61. $this->attributes = $attributes;
  62. $this->updateMessage();
  63. }
  64. /**
  65. * Sets the line of the PHP file the error occurred in.
  66. *
  67. * @param string $message Error message
  68. */
  69. public function setRawMessage(string $message) {
  70. $this->rawMessage = $message;
  71. $this->updateMessage();
  72. }
  73. /**
  74. * Sets the line the error starts in.
  75. *
  76. * @param int $line Error start line
  77. */
  78. public function setStartLine(int $line) {
  79. $this->attributes['startLine'] = $line;
  80. $this->updateMessage();
  81. }
  82. /**
  83. * Returns whether the error has start and end column information.
  84. *
  85. * For column information enable the startFilePos and endFilePos in the lexer options.
  86. *
  87. * @return bool
  88. */
  89. public function hasColumnInfo() : bool {
  90. return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
  91. }
  92. /**
  93. * Gets the start column (1-based) into the line where the error started.
  94. *
  95. * @param string $code Source code of the file
  96. * @return int
  97. */
  98. public function getStartColumn(string $code) : int {
  99. if (!$this->hasColumnInfo()) {
  100. throw new \RuntimeException('Error does not have column information');
  101. }
  102. return $this->toColumn($code, $this->attributes['startFilePos']);
  103. }
  104. /**
  105. * Gets the end column (1-based) into the line where the error ended.
  106. *
  107. * @param string $code Source code of the file
  108. * @return int
  109. */
  110. public function getEndColumn(string $code) : int {
  111. if (!$this->hasColumnInfo()) {
  112. throw new \RuntimeException('Error does not have column information');
  113. }
  114. return $this->toColumn($code, $this->attributes['endFilePos']);
  115. }
  116. /**
  117. * Formats message including line and column information.
  118. *
  119. * @param string $code Source code associated with the error, for calculation of the columns
  120. *
  121. * @return string Formatted message
  122. */
  123. public function getMessageWithColumnInfo(string $code) : string {
  124. return sprintf(
  125. '%s from %d:%d to %d:%d', $this->getRawMessage(),
  126. $this->getStartLine(), $this->getStartColumn($code),
  127. $this->getEndLine(), $this->getEndColumn($code)
  128. );
  129. }
  130. /**
  131. * Converts a file offset into a column.
  132. *
  133. * @param string $code Source code that $pos indexes into
  134. * @param int $pos 0-based position in $code
  135. *
  136. * @return int 1-based column (relative to start of line)
  137. */
  138. private function toColumn(string $code, int $pos) : int {
  139. if ($pos > strlen($code)) {
  140. throw new \RuntimeException('Invalid position information');
  141. }
  142. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  143. if (false === $lineStartPos) {
  144. $lineStartPos = -1;
  145. }
  146. return $pos - $lineStartPos;
  147. }
  148. /**
  149. * Updates the exception message after a change to rawMessage or rawLine.
  150. */
  151. protected function updateMessage() {
  152. $this->message = $this->rawMessage;
  153. if (-1 === $this->getStartLine()) {
  154. $this->message .= ' on unknown line';
  155. } else {
  156. $this->message .= ' on line ' . $this->getStartLine();
  157. }
  158. }
  159. }