5 class Error extends \RuntimeException
11 * Creates an Exception signifying a parse error.
13 * @param string $message Error message
14 * @param array|int $attributes Attributes of node/token where error occurred
15 * (or start line of error -- deprecated)
17 public function __construct($message, $attributes = array()) {
18 $this->rawMessage = (string) $message;
19 if (is_array($attributes)) {
20 $this->attributes = $attributes;
22 $this->attributes = array('startLine' => $attributes);
24 $this->updateMessage();
28 * Gets the error message
30 * @return string Error message
32 public function getRawMessage() {
33 return $this->rawMessage;
37 * Gets the line the error starts in.
39 * @return int Error start line
41 public function getStartLine() {
42 return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1;
46 * Gets the line the error ends in.
48 * @return int Error end line
50 public function getEndLine() {
51 return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1;
56 * Gets the attributes of the node/token the error occurred at.
60 public function getAttributes() {
61 return $this->attributes;
65 * Sets the attributes of the node/token the error occured at.
67 * @param array $attributes
69 public function setAttributes(array $attributes) {
70 $this->attributes = $attributes;
71 $this->updateMessage();
75 * Sets the line of the PHP file the error occurred in.
77 * @param string $message Error message
79 public function setRawMessage($message) {
80 $this->rawMessage = (string) $message;
81 $this->updateMessage();
85 * Sets the line the error starts in.
87 * @param int $line Error start line
89 public function setStartLine($line) {
90 $this->attributes['startLine'] = (int) $line;
91 $this->updateMessage();
95 * Returns whether the error has start and end column information.
97 * For column information enable the startFilePos and endFilePos in the lexer options.
101 public function hasColumnInfo() {
102 return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
106 * Gets the start column (1-based) into the line where the error started.
108 * @param string $code Source code of the file
111 public function getStartColumn($code) {
112 if (!$this->hasColumnInfo()) {
113 throw new \RuntimeException('Error does not have column information');
116 return $this->toColumn($code, $this->attributes['startFilePos']);
120 * Gets the end column (1-based) into the line where the error ended.
122 * @param string $code Source code of the file
125 public function getEndColumn($code) {
126 if (!$this->hasColumnInfo()) {
127 throw new \RuntimeException('Error does not have column information');
130 return $this->toColumn($code, $this->attributes['endFilePos']);
133 public function getMessageWithColumnInfo($code) {
135 '%s from %d:%d to %d:%d', $this->getRawMessage(),
136 $this->getStartLine(), $this->getStartColumn($code),
137 $this->getEndLine(), $this->getEndColumn($code)
141 private function toColumn($code, $pos) {
142 if ($pos > strlen($code)) {
143 throw new \RuntimeException('Invalid position information');
146 $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
147 if (false === $lineStartPos) {
151 return $pos - $lineStartPos;
155 * Updates the exception message after a change to rawMessage or rawLine.
157 protected function updateMessage() {
158 $this->message = $this->rawMessage;
160 if (-1 === $this->getStartLine()) {
161 $this->message .= ' on unknown line';
163 $this->message .= ' on line ' . $this->getStartLine();