Chris@13: rawMessage = $message; Chris@0: if (is_array($attributes)) { Chris@0: $this->attributes = $attributes; Chris@0: } else { Chris@13: $this->attributes = ['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@13: public function getRawMessage() : string { 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@13: public function getStartLine() : int { Chris@13: return $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@13: public function getEndLine() : int { Chris@13: return $this->attributes['endLine'] ?? -1; 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@13: public function getAttributes() : array { Chris@0: return $this->attributes; Chris@0: } Chris@0: Chris@0: /** Chris@13: * Sets the attributes of the node/token the error occurred 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@13: public function setRawMessage(string $message) { Chris@16: $this->rawMessage = $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@13: public function setStartLine(int $line) { Chris@16: $this->attributes['startLine'] = $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@13: public function hasColumnInfo() : bool { Chris@13: return isset($this->attributes['startFilePos'], $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@13: public function getStartColumn(string $code) : int { 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@13: public function getEndColumn(string $code) : int { 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@13: /** Chris@13: * Formats message including line and column information. Chris@13: * Chris@13: * @param string $code Source code associated with the error, for calculation of the columns Chris@13: * Chris@13: * @return string Formatted message Chris@13: */ Chris@13: public function getMessageWithColumnInfo(string $code) : string { 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@13: /** Chris@13: * Converts a file offset into a column. Chris@13: * Chris@13: * @param string $code Source code that $pos indexes into Chris@13: * @param int $pos 0-based position in $code Chris@13: * Chris@13: * @return int 1-based column (relative to start of line) Chris@13: */ Chris@13: private function toColumn(string $code, int $pos) : int { 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: }