2011-06-03 17:44:23 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
class Error extends \RuntimeException
|
2011-06-03 17:44:23 +02:00
|
|
|
{
|
|
|
|
protected $rawMessage;
|
2015-04-18 12:51:26 +02:00
|
|
|
protected $attributes;
|
2011-06-03 17:44:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2011-06-03 17:44:23 +02:00
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function __construct($message, $attributes = array()) {
|
2011-06-03 17:44:23 +02:00
|
|
|
$this->rawMessage = (string) $message;
|
2015-04-18 12:51:26 +02:00
|
|
|
if (is_array($attributes)) {
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
} else {
|
|
|
|
$this->attributes = array('startLine' => $attributes);
|
|
|
|
}
|
2011-06-03 17:44:23 +02:00
|
|
|
$this->updateMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the error message
|
|
|
|
*
|
|
|
|
* @return string Error message
|
|
|
|
*/
|
|
|
|
public function getRawMessage() {
|
2011-11-27 21:43:27 +01:00
|
|
|
return $this->rawMessage;
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-18 12:51:26 +02:00
|
|
|
* Gets the line the error starts in.
|
2011-06-03 17:44:23 +02:00
|
|
|
*
|
2015-04-18 12:51:26 +02:00
|
|
|
* @return int Error start line
|
2011-06-03 17:44:23 +02:00
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function getStartLine() {
|
|
|
|
return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1;
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-18 12:51:26 +02:00
|
|
|
* Gets the line the error ends in.
|
2011-06-03 17:44:23 +02:00
|
|
|
*
|
2015-04-18 12:51:26 +02:00
|
|
|
* @return int Error end line
|
2011-06-03 17:44:23 +02:00
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function getEndLine() {
|
|
|
|
return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1;
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:51:26 +02:00
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
/**
|
2015-04-18 12:51:26 +02:00
|
|
|
* Gets the attributes of the node/token the error occurred at.
|
2011-06-03 17:44:23 +02:00
|
|
|
*
|
2015-04-18 12:51:26 +02:00
|
|
|
* @return array
|
2011-06-03 17:44:23 +02:00
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function getAttributes() {
|
|
|
|
return $this->attributes;
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:59:44 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function setRawMessage($message) {
|
|
|
|
$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
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function setStartLine($line) {
|
|
|
|
$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
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function hasColumnInfo() {
|
|
|
|
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
|
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function getStartColumn($code) {
|
|
|
|
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
|
|
|
|
*/
|
2015-04-18 12:51:26 +02:00
|
|
|
public function getEndColumn($code) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-30 18:24:43 +02:00
|
|
|
public function getMessageWithColumnInfo($code) {
|
|
|
|
return sprintf(
|
|
|
|
'%s from %d:%d to %d:%d', $this->getRawMessage(),
|
|
|
|
$this->getStartLine(), $this->getStartColumn($code),
|
|
|
|
$this->getEndLine(), $this->getEndColumn($code)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:51:26 +02:00
|
|
|
private function toColumn($code, $pos) {
|
2015-04-27 15:33:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-06-03 17:44:23 +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()) {
|
2011-06-03 17:44:23 +02:00
|
|
|
$this->message .= ' on unknown line';
|
|
|
|
} else {
|
2015-04-18 12:51:26 +02:00
|
|
|
$this->message .= ' on line ' . $this->getStartLine();
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
2015-04-18 12:51:26 +02:00
|
|
|
}
|
2015-04-14 20:31:06 +02:00
|
|
|
}
|