1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-16 03:16:54 +01:00
PHP-Parser/lib/PhpParser/Error.php

182 lines
5.0 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
namespace PhpParser;
class Error extends \RuntimeException
{
protected $rawMessage;
2015-04-18 12:51:26 +02:00
protected $attributes;
/**
* Creates an Exception signifying a parse error.
*
2015-04-18 12:51:26 +02:00
* @param string $message Error message
* @param array|int $attributes Attributes of node/token where error occurred
* (or start line of error -- deprecated)
*/
public function __construct(string $message, $attributes = []) {
$this->rawMessage = (string) $message;
2015-04-18 12:51:26 +02:00
if (is_array($attributes)) {
$this->attributes = $attributes;
} else {
$this->attributes = ['startLine' => $attributes];
2015-04-18 12:51:26 +02:00
}
$this->updateMessage();
}
/**
* Gets the error message
*
* @return string Error message
*/
2017-04-28 21:40:59 +02:00
public function getRawMessage() : string {
2011-11-27 21:43:27 +01:00
return $this->rawMessage;
}
/**
2015-04-18 12:51:26 +02:00
* Gets the line the error starts in.
*
2015-04-18 12:51:26 +02:00
* @return int Error start line
*/
2017-04-28 21:40:59 +02:00
public function getStartLine() : int {
2017-04-24 22:32:40 +02:00
return $this->attributes['startLine'] ?? -1;
}
/**
2015-04-18 12:51:26 +02:00
* Gets the line the error ends in.
*
2015-04-18 12:51:26 +02:00
* @return int Error end line
*/
2017-04-28 21:40:59 +02:00
public function getEndLine() : int {
2017-04-24 22:32:40 +02:00
return $this->attributes['endLine'] ?? -1;
}
2015-04-18 12:51:26 +02:00
/**
2015-04-18 12:51:26 +02:00
* Gets the attributes of the node/token the error occurred at.
*
2015-04-18 12:51:26 +02:00
* @return array
*/
2017-04-28 21:40:59 +02:00
public function getAttributes() : array {
2015-04-18 12:51:26 +02:00
return $this->attributes;
}
/**
* Sets the attributes of the node/token the error occured at.
*
* @param array $attributes
*/
public function setAttributes(array $attributes) {
$this->attributes = $attributes;
$this->updateMessage();
}
2015-04-14 20:31:06 +02:00
/**
2015-04-18 12:51:26 +02:00
* Sets the line of the PHP file the error occurred in.
*
* @param string $message Error message
2015-04-14 20:31:06 +02:00
*/
2017-04-28 21:40:59 +02:00
public function setRawMessage(string $message) {
2015-04-18 12:51:26 +02:00
$this->rawMessage = (string) $message;
$this->updateMessage();
2015-04-14 20:31:06 +02:00
}
/**
2015-04-18 12:51:26 +02:00
* Sets the line the error starts in.
*
* @param int $line Error start line
2015-04-14 20:31:06 +02:00
*/
2017-04-28 21:40:59 +02:00
public function setStartLine(int $line) {
2015-04-18 12:51:26 +02:00
$this->attributes['startLine'] = (int) $line;
$this->updateMessage();
2015-04-14 20:31:06 +02:00
}
/**
2015-04-18 12:51:26 +02:00
* Returns whether the error has start and end column information.
*
* For column information enable the startFilePos and endFilePos in the lexer options.
*
* @return bool
2015-04-14 20:31:06 +02:00
*/
2017-04-28 21:40:59 +02:00
public function hasColumnInfo() : bool {
2015-04-18 12:51:26 +02:00
return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
2015-04-14 20:31:06 +02:00
}
/**
2015-04-18 21:20:45 +02:00
* Gets the start column (1-based) into the line where the error started.
2015-04-18 12:51:26 +02:00
*
* @param string $code Source code of the file
2015-04-14 20:31:06 +02:00
* @return int
*/
2017-04-28 21:40:59 +02:00
public function getStartColumn(string $code) : int {
2015-04-18 12:51:26 +02:00
if (!$this->hasColumnInfo()) {
throw new \RuntimeException('Error does not have column information');
2015-04-14 20:31:06 +02:00
}
2015-04-18 12:51:26 +02:00
return $this->toColumn($code, $this->attributes['startFilePos']);
2015-04-14 20:31:06 +02:00
}
/**
2015-04-18 21:20:45 +02:00
* Gets the end column (1-based) into the line where the error ended.
2015-04-18 12:51:26 +02:00
*
* @param string $code Source code of the file
2015-04-14 20:31:06 +02:00
* @return int
*/
2017-04-28 21:40:59 +02:00
public function getEndColumn(string $code) : int {
2015-04-18 12:51:26 +02:00
if (!$this->hasColumnInfo()) {
throw new \RuntimeException('Error does not have column information');
2015-04-14 20:31:06 +02:00
}
2015-04-18 12:51:26 +02:00
return $this->toColumn($code, $this->attributes['endFilePos']);
2015-04-14 20:31:06 +02:00
}
2017-01-24 08:38:55 +01:00
/**
* Formats message including line and column information.
*
* @param string $code Source code associated with the error, for calculation of the columns
*
* @return string Formatted message
2017-01-24 08:38:55 +01:00
*/
2017-04-28 21:40:59 +02:00
public function getMessageWithColumnInfo(string $code) : string {
2016-09-30 18:24:43 +02:00
return sprintf(
'%s from %d:%d to %d:%d', $this->getRawMessage(),
$this->getStartLine(), $this->getStartColumn($code),
$this->getEndLine(), $this->getEndColumn($code)
);
}
/**
* Converts a file offset into a column.
*
* @param string $code Source code that $pos indexes into
* @param int $pos 0-based position in $code
*
* @return int 1-based column (relative to start of line)
*/
2017-04-28 21:40:59 +02:00
private function toColumn(string $code, int $pos) : int {
if ($pos > strlen($code)) {
2015-04-18 12:51:26 +02:00
throw new \RuntimeException('Invalid position information');
}
2015-04-14 20:31:06 +02:00
2015-04-18 12:51:26 +02:00
$lineStartPos = strrpos($code, "\n", $pos - strlen($code));
if (false === $lineStartPos) {
$lineStartPos = -1;
2015-04-14 20:31:06 +02:00
}
2015-04-18 21:20:45 +02:00
return $pos - $lineStartPos;
2015-04-14 20:31:06 +02:00
}
/**
* Updates the exception message after a change to rawMessage or rawLine.
*/
protected function updateMessage() {
$this->message = $this->rawMessage;
2015-04-18 12:51:26 +02:00
if (-1 === $this->getStartLine()) {
$this->message .= ' on unknown line';
} else {
2015-04-18 12:51:26 +02:00
$this->message .= ' on line ' . $this->getStartLine();
}
2015-04-18 12:51:26 +02:00
}
2015-04-14 20:31:06 +02:00
}