diff --git a/bin/php-parse b/bin/php-parse index 4041338..2d54747 100755 --- a/bin/php-parse +++ b/bin/php-parse @@ -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(); } diff --git a/lib/PhpParser/Error.php b/lib/PhpParser/Error.php index 58d1dd6..1eac054 100644 --- a/lib/PhpParser/Error.php +++ b/lib/PhpParser/Error.php @@ -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'); diff --git a/test/PhpParser/CodeParsingTest.php b/test/PhpParser/CodeParsingTest.php index 5eb695e..d17507b 100644 --- a/test/PhpParser/CodeParsingTest.php +++ b/test/PhpParser/CodeParsingTest.php @@ -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(); }