1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/lib/PhpParser/Error.php

159 lines
4.2 KiB
PHP
Raw Normal View History

<?php
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)
*/
2015-04-18 12:51:26 +02:00
public function __construct($message, $attributes = array()) {
$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);
}
$this->updateMessage();
}
/**
* Gets the error message
*
* @return string Error message
*/
public function getRawMessage() {
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
*/
2015-04-18 12:51:26 +02:00
public function getStartLine() {
return isset($this->attributes['startLine']) ? $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
*/
2015-04-18 12:51:26 +02:00
public function getEndLine() {
return isset($this->attributes['endLine']) ? $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
*/
2015-04-18 12:51:26 +02:00
public function getAttributes() {
return $this->attributes;
}
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
}
2015-04-18 12:51:26 +02:00
private function toColumn($code, $pos) {
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
2015-04-18 12:51:26 +02:00
/** @deprecated Use getStartLine() instead */
public function getRawLine() {
return $this->getStartLine();
}
/** @deprecated Use setStartLine() instead */
public function setRawLine($line) {
$this->setStartLine($line);
}
2015-04-14 20:31:06 +02:00
}