Error.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  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($message, $attributes = array()) {
  15. $this->rawMessage = (string) $message;
  16. if (is_array($attributes)) {
  17. $this->attributes = $attributes;
  18. } else {
  19. $this->attributes = array('startLine' => $attributes);
  20. }
  21. $this->updateMessage();
  22. }
  23. /**
  24. * Gets the error message
  25. *
  26. * @return string Error message
  27. */
  28. public function getRawMessage() {
  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() {
  37. return isset($this->attributes['startLine']) ? $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() {
  45. return isset($this->attributes['endLine']) ? $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() {
  53. return $this->attributes;
  54. }
  55. /**
  56. * Sets the line of the PHP file the error occurred in.
  57. *
  58. * @param string $message Error message
  59. */
  60. public function setRawMessage($message) {
  61. $this->rawMessage = (string) $message;
  62. $this->updateMessage();
  63. }
  64. /**
  65. * Sets the line the error starts in.
  66. *
  67. * @param int $line Error start line
  68. */
  69. public function setStartLine($line) {
  70. $this->attributes['startLine'] = (int) $line;
  71. $this->updateMessage();
  72. }
  73. /**
  74. * Returns whether the error has start and end column information.
  75. *
  76. * For column information enable the startFilePos and endFilePos in the lexer options.
  77. *
  78. * @return bool
  79. */
  80. public function hasColumnInfo() {
  81. return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
  82. }
  83. /**
  84. * Gets the start column (1-based) into the line where the error started.
  85. *
  86. * @param string $code Source code of the file
  87. * @return int
  88. */
  89. public function getStartColumn($code) {
  90. if (!$this->hasColumnInfo()) {
  91. throw new \RuntimeException('Error does not have column information');
  92. }
  93. return $this->toColumn($code, $this->attributes['startFilePos']);
  94. }
  95. /**
  96. * Gets the end column (1-based) into the line where the error ended.
  97. *
  98. * @param string $code Source code of the file
  99. * @return int
  100. */
  101. public function getEndColumn($code) {
  102. if (!$this->hasColumnInfo()) {
  103. throw new \RuntimeException('Error does not have column information');
  104. }
  105. return $this->toColumn($code, $this->attributes['endFilePos']);
  106. }
  107. private function toColumn($code, $pos) {
  108. if ($pos > strlen($code)) {
  109. throw new \RuntimeException('Invalid position information');
  110. }
  111. $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
  112. if (false === $lineStartPos) {
  113. $lineStartPos = -1;
  114. }
  115. return $pos - $lineStartPos;
  116. }
  117. /**
  118. * Updates the exception message after a change to rawMessage or rawLine.
  119. */
  120. protected function updateMessage() {
  121. $this->message = $this->rawMessage;
  122. if (-1 === $this->getStartLine()) {
  123. $this->message .= ' on unknown line';
  124. } else {
  125. $this->message .= ' on line ' . $this->getStartLine();
  126. }
  127. }
  128. /** @deprecated Use getStartLine() instead */
  129. public function getRawLine() {
  130. return $this->getStartLine();
  131. }
  132. /** @deprecated Use setStartLine() instead */
  133. public function setRawLine($line) {
  134. $this->setStartLine($line);
  135. }
  136. }