Add Error::getMessageWithColumnInfo()

This commit is contained in:
Nikita Popov 2016-09-30 18:24:43 +02:00
parent c5e0c3d7e2
commit f3c7dc9d89
3 changed files with 10 additions and 7 deletions

View File

@ -90,11 +90,7 @@ foreach ($files as $file) {
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
if ($withColumnInfo && $e->hasColumnInfo()) {
$startLine = $e->getStartLine();
$endLine = $e->getEndLine();
$startColumn = $e->getStartColumn($code);
$endColumn = $e->getEndColumn($code);
return $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
return $e->getMessageWithColumnInfo($code);
} else {
return $e->getMessage();
}

View File

@ -120,6 +120,14 @@ class Error extends \RuntimeException
return $this->toColumn($code, $this->attributes['endFilePos']);
}
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)
);
}
private function toColumn($code, $pos) {
if ($pos > strlen($code)) {
throw new \RuntimeException('Invalid position information');

View File

@ -60,8 +60,7 @@ class CodeParsingTest extends CodeTestAbstract
private function formatErrorMessage(Error $e, $code) {
if ($e->hasColumnInfo()) {
return $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
return $e->getMessageWithColumnInfo($code);
} else {
return $e->getMessage();
}