From 8fba6af90c92c11e287963f2cc9bbc66f4debb8c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 25 Jun 2024 18:46:18 +0200 Subject: [PATCH] Conditionally assemble file path string --- .../framework/src/Framework/Exceptions/ParseException.php | 4 +++- packages/framework/tests/Unit/CustomExceptionsTest.php | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Exceptions/ParseException.php b/packages/framework/src/Framework/Exceptions/ParseException.php index 753cb870780..aaf4724ae0a 100644 --- a/packages/framework/src/Framework/Exceptions/ParseException.php +++ b/packages/framework/src/Framework/Exceptions/ParseException.php @@ -25,7 +25,9 @@ public function __construct(string $file = '', ?Throwable $previous = null) protected function formatMessage(string $file, ?Throwable $previous): string { - return rtrim(sprintf("Invalid %s in file: '%s' %s", $this->getTypeLabel($file), $file, $this->getContext($previous))); + $fileLabel = $file ? sprintf(": '%s'", $file) : ''; + + return rtrim(sprintf('Invalid %s in file%s %s', $this->getTypeLabel($file), $fileLabel, $this->getContext($previous))); } protected function getTypeLabel(string $file): string diff --git a/packages/framework/tests/Unit/CustomExceptionsTest.php b/packages/framework/tests/Unit/CustomExceptionsTest.php index fcaeb77b1f3..e3894eec0a0 100644 --- a/packages/framework/tests/Unit/CustomExceptionsTest.php +++ b/packages/framework/tests/Unit/CustomExceptionsTest.php @@ -121,7 +121,7 @@ public function testParseExceptionWithDefaultMessage() { $exception = new ParseException(); - $this->assertSame("Invalid data in file: ''", $exception->getMessage()); + $this->assertSame('Invalid data in file', $exception->getMessage()); } public function testParseExceptionWithFileName() @@ -158,7 +158,7 @@ public function testParseExceptionWithEmptyFileNameAndCustomMessage() $previous = new RuntimeException('Custom error message.'); $exception = new ParseException('', $previous); - $this->assertSame("Invalid data in file: '' (Custom error message)", $exception->getMessage()); + $this->assertSame('Invalid data in file (Custom error message)', $exception->getMessage()); } public function testParseExceptionWithEmptyFileNameAndEmptyPreviousMessage() @@ -166,7 +166,7 @@ public function testParseExceptionWithEmptyFileNameAndEmptyPreviousMessage() $previous = new RuntimeException(''); $exception = new ParseException('', $previous); - $this->assertSame("Invalid data in file: ''", $exception->getMessage()); + $this->assertSame('Invalid data in file', $exception->getMessage()); } public function testParseExceptionWithNoPrevious()