From c4c0db8810e3e2414f34fb1dfe6bd463c0ffe5cd Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 26 Jun 2025 19:11:57 +0100 Subject: [PATCH 1/2] [BUGFIX] Exclude absent line number from exception message The bug was introduced by #1288, so has not been included in any release; thus a changelog entry is not justified. --- tests/Unit/Parsing/SourceExceptionTest.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Parsing/SourceExceptionTest.php b/tests/Unit/Parsing/SourceExceptionTest.php index 6e22bc96f..923f6c490 100644 --- a/tests/Unit/Parsing/SourceExceptionTest.php +++ b/tests/Unit/Parsing/SourceExceptionTest.php @@ -20,7 +20,7 @@ public function getMessageReturnsMessageProvidedToConstructor(): void $message = 'The cake is a lie.'; $exception = new SourceException($message); - self::assertStringContainsString($message, $exception->getMessage()); + self::assertSame($message, $exception->getMessage()); } /** @@ -55,6 +55,17 @@ public function getMessageWithLineNumberProvidedIncludesLineNumber(): void self::assertStringContainsString(' [line no: ' . $lineNumber . ']', $exception->getMessage()); } + /** + * @test + */ + public function getMessageWithLineNumberProvidedIncludesMessage(): void + { + $message = 'There is no flatware.'; + $exception = new SourceException($message, 17); + + self::assertStringContainsString($message, $exception->getMessage()); + } + /** * @test */ From 6cc6b3da555e3f65ea2c20334d55cd16a0235ced Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 26 Jun 2025 19:33:53 +0100 Subject: [PATCH 2/2] Include the fix as well as the test changes --- src/Parsing/SourceException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parsing/SourceException.php b/src/Parsing/SourceException.php index b96c1878c..43b3faf05 100644 --- a/src/Parsing/SourceException.php +++ b/src/Parsing/SourceException.php @@ -17,7 +17,7 @@ class SourceException extends \Exception implements Positionable public function __construct(string $message, ?int $lineNumber = null) { $this->setPosition($lineNumber); - if ($lineNumber !== 0) { + if ($lineNumber !== null) { $message .= " [line no: $lineNumber]"; } parent::__construct($message);