Skip to content

Commit f3c7dc9

Browse files
committed
Add Error::getMessageWithColumnInfo()
1 parent c5e0c3d commit f3c7dc9

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

bin/php-parse

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ foreach ($files as $file) {
9090

9191
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
9292
if ($withColumnInfo && $e->hasColumnInfo()) {
93-
$startLine = $e->getStartLine();
94-
$endLine = $e->getEndLine();
95-
$startColumn = $e->getStartColumn($code);
96-
$endColumn = $e->getEndColumn($code);
97-
return $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
93+
return $e->getMessageWithColumnInfo($code);
9894
} else {
9995
return $e->getMessage();
10096
}

lib/PhpParser/Error.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ public function getEndColumn($code) {
120120
return $this->toColumn($code, $this->attributes['endFilePos']);
121121
}
122122

123+
public function getMessageWithColumnInfo($code) {
124+
return sprintf(
125+
'%s from %d:%d to %d:%d', $this->getRawMessage(),
126+
$this->getStartLine(), $this->getStartColumn($code),
127+
$this->getEndLine(), $this->getEndColumn($code)
128+
);
129+
}
130+
123131
private function toColumn($code, $pos) {
124132
if ($pos > strlen($code)) {
125133
throw new \RuntimeException('Invalid position information');

test/PhpParser/CodeParsingTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public function provideTestParse() {
6060

6161
private function formatErrorMessage(Error $e, $code) {
6262
if ($e->hasColumnInfo()) {
63-
return $e->getRawMessage() . ' from ' . $e->getStartLine() . ':' . $e->getStartColumn($code)
64-
. ' to ' . $e->getEndLine() . ':' . $e->getEndColumn($code);
63+
return $e->getMessageWithColumnInfo($code);
6564
} else {
6665
return $e->getMessage();
6766
}

0 commit comments

Comments
 (0)