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