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