1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | class Error extends \RuntimeException |
6: | { |
7: | protected $rawMessage; |
8: | protected $attributes; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | public function __construct(string $message, $attributes = []) { |
18: | $this->rawMessage = $message; |
19: | if (is_array($attributes)) { |
20: | $this->attributes = $attributes; |
21: | } else { |
22: | $this->attributes = ['startLine' => $attributes]; |
23: | } |
24: | $this->updateMessage(); |
25: | } |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | public function getRawMessage() : string { |
33: | return $this->rawMessage; |
34: | } |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | public function getStartLine() : int { |
42: | return $this->attributes['startLine'] ?? -1; |
43: | } |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | public function getEndLine() : int { |
51: | return $this->attributes['endLine'] ?? -1; |
52: | } |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | public function getAttributes() : array { |
60: | return $this->attributes; |
61: | } |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | public function setAttributes(array $attributes) { |
69: | $this->attributes = $attributes; |
70: | $this->updateMessage(); |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | public function setRawMessage(string $message) { |
79: | $this->rawMessage = $message; |
80: | $this->updateMessage(); |
81: | } |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | public function setStartLine(int $line) { |
89: | $this->attributes['startLine'] = $line; |
90: | $this->updateMessage(); |
91: | } |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | public function hasColumnInfo() : bool { |
101: | return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']); |
102: | } |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | public function getStartColumn(string $code) : int { |
111: | if (!$this->hasColumnInfo()) { |
112: | throw new \RuntimeException('Error does not have column information'); |
113: | } |
114: | |
115: | return $this->toColumn($code, $this->attributes['startFilePos']); |
116: | } |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function getEndColumn(string $code) : int { |
125: | if (!$this->hasColumnInfo()) { |
126: | throw new \RuntimeException('Error does not have column information'); |
127: | } |
128: | |
129: | return $this->toColumn($code, $this->attributes['endFilePos']); |
130: | } |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | public function getMessageWithColumnInfo(string $code) : string { |
140: | return sprintf( |
141: | '%s from %d:%d to %d:%d', $this->getRawMessage(), |
142: | $this->getStartLine(), $this->getStartColumn($code), |
143: | $this->getEndLine(), $this->getEndColumn($code) |
144: | ); |
145: | } |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | private function toColumn(string $code, int $pos) : int { |
156: | if ($pos > strlen($code)) { |
157: | throw new \RuntimeException('Invalid position information'); |
158: | } |
159: | |
160: | $lineStartPos = strrpos($code, "\n", $pos - strlen($code)); |
161: | if (false === $lineStartPos) { |
162: | $lineStartPos = -1; |
163: | } |
164: | |
165: | return $pos - $lineStartPos; |
166: | } |
167: | |
168: | |
169: | |
170: | |
171: | protected function updateMessage() { |
172: | $this->message = $this->rawMessage; |
173: | |
174: | if (-1 === $this->getStartLine()) { |
175: | $this->message .= ' on unknown line'; |
176: | } else { |
177: | $this->message .= ' on line ' . $this->getStartLine(); |
178: | } |
179: | } |
180: | } |
181: | |