Skip to content

Commit 02118c7

Browse files
committed
bug #4598 Sync Error file and line (fabpot)
This PR was merged into the 3.x branch. Discussion ---------- Sync Error file and line The PHP exception file and name go together. So, we should only update the line number only if we have a file. Commits ------- 4a121d9 Sync Error file and line
2 parents 7cde13f + 4a121d9 commit 02118c7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Error/Error.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ public function appendMessage($rawMessage): void
106106

107107
private function updateRepr(): void
108108
{
109-
if ($this->lineno > 0) {
110-
$this->line = $this->lineno;
111-
}
112109
if ($this->source && $this->source->getPath()) {
110+
// we only update the file and the line together
113111
$this->file = $this->source->getPath();
112+
if ($this->lineno > 0) {
113+
$this->line = $this->lineno;
114+
}
114115
}
115116

116117
$this->message = $this->rawMessage;

tests/ErrorTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Twig\Environment;
1616
use Twig\Error\Error;
1717
use Twig\Error\RuntimeError;
18+
use Twig\Error\SyntaxError;
1819
use Twig\Loader\ArrayLoader;
1920
use Twig\Loader\FilesystemLoader;
2021
use Twig\Source;
@@ -234,6 +235,24 @@ public function testTwigArrayReduceThrowsRuntimeExceptions()
234235
}
235236
}
236237

238+
public function testTwigExceptionUpdateFileAndLineTogether()
239+
{
240+
$twig = new Environment(new ArrayLoader([
241+
'index' => "\n\n\n\n{{ foo() }}",
242+
]), ['debug' => true, 'cache' => false]);
243+
244+
try {
245+
$twig->load('index')->render([]);
246+
} catch (SyntaxError $e) {
247+
$this->assertSame('Unknown "foo" function in "index" at line 5.', $e->getMessage());
248+
$this->assertSame(5, $e->getTemplateLine());
249+
// as we are using an ArrayLoader, we don't have a file, so the line should not be the template line,
250+
// but the line of the error in the Parser.php file
251+
$this->assertStringContainsString('Parser.php', $e->getFile());
252+
$this->assertNotSame(5, $e->getLine());
253+
}
254+
}
255+
237256
public static function getErroredTemplates()
238257
{
239258
return [

0 commit comments

Comments
 (0)