Chris@0: rawMessage = (string) $message; Chris@0: if (is_array($attributes)) { Chris@0: $this->attributes = $attributes; Chris@0: } else { Chris@0: $this->attributes = array('startLine' => $attributes); Chris@0: } Chris@0: $this->updateMessage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the error message Chris@0: * Chris@0: * @return string Error message Chris@0: */ Chris@0: public function getRawMessage() { Chris@0: return $this->rawMessage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the line the error starts in. Chris@0: * Chris@0: * @return int Error start line Chris@0: */ Chris@0: public function getStartLine() { Chris@0: return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the line the error ends in. Chris@0: * Chris@0: * @return int Error end line Chris@0: */ Chris@0: public function getEndLine() { Chris@0: return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1; Chris@0: } Chris@0: Chris@0: Chris@0: /** Chris@0: * Gets the attributes of the node/token the error occurred at. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getAttributes() { Chris@0: return $this->attributes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the attributes of the node/token the error occured at. Chris@0: * Chris@0: * @param array $attributes Chris@0: */ Chris@0: public function setAttributes(array $attributes) { Chris@0: $this->attributes = $attributes; Chris@0: $this->updateMessage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the line of the PHP file the error occurred in. Chris@0: * Chris@0: * @param string $message Error message Chris@0: */ Chris@0: public function setRawMessage($message) { Chris@0: $this->rawMessage = (string) $message; Chris@0: $this->updateMessage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the line the error starts in. Chris@0: * Chris@0: * @param int $line Error start line Chris@0: */ Chris@0: public function setStartLine($line) { Chris@0: $this->attributes['startLine'] = (int) $line; Chris@0: $this->updateMessage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether the error has start and end column information. Chris@0: * Chris@0: * For column information enable the startFilePos and endFilePos in the lexer options. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasColumnInfo() { Chris@0: return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the start column (1-based) into the line where the error started. Chris@0: * Chris@0: * @param string $code Source code of the file Chris@0: * @return int Chris@0: */ Chris@0: public function getStartColumn($code) { Chris@0: if (!$this->hasColumnInfo()) { Chris@0: throw new \RuntimeException('Error does not have column information'); Chris@0: } Chris@0: Chris@0: return $this->toColumn($code, $this->attributes['startFilePos']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the end column (1-based) into the line where the error ended. Chris@0: * Chris@0: * @param string $code Source code of the file Chris@0: * @return int Chris@0: */ Chris@0: public function getEndColumn($code) { Chris@0: if (!$this->hasColumnInfo()) { Chris@0: throw new \RuntimeException('Error does not have column information'); Chris@0: } Chris@0: Chris@0: return $this->toColumn($code, $this->attributes['endFilePos']); Chris@0: } Chris@0: Chris@0: public function getMessageWithColumnInfo($code) { Chris@0: return sprintf( Chris@0: '%s from %d:%d to %d:%d', $this->getRawMessage(), Chris@0: $this->getStartLine(), $this->getStartColumn($code), Chris@0: $this->getEndLine(), $this->getEndColumn($code) Chris@0: ); Chris@0: } Chris@0: Chris@0: private function toColumn($code, $pos) { Chris@0: if ($pos > strlen($code)) { Chris@0: throw new \RuntimeException('Invalid position information'); Chris@0: } Chris@0: Chris@0: $lineStartPos = strrpos($code, "\n", $pos - strlen($code)); Chris@0: if (false === $lineStartPos) { Chris@0: $lineStartPos = -1; Chris@0: } Chris@0: Chris@0: return $pos - $lineStartPos; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates the exception message after a change to rawMessage or rawLine. Chris@0: */ Chris@0: protected function updateMessage() { Chris@0: $this->message = $this->rawMessage; Chris@0: Chris@0: if (-1 === $this->getStartLine()) { Chris@0: $this->message .= ' on unknown line'; Chris@0: } else { Chris@0: $this->message .= ' on line ' . $this->getStartLine(); Chris@0: } Chris@0: } Chris@0: }