From df543f61cb00969cc41b55e3a4e39df2c90c864a Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Jan 2024 16:50:21 +0900 Subject: [PATCH 1/2] test: add test for ExceptionHandler::highlightFile() --- tests/system/Debug/ExceptionHandlerTest.php | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/system/Debug/ExceptionHandlerTest.php b/tests/system/Debug/ExceptionHandlerTest.php index 1321f67195f3..8276c76c7ffb 100644 --- a/tests/system/Debug/ExceptionHandlerTest.php +++ b/tests/system/Debug/ExceptionHandlerTest.php @@ -237,4 +237,29 @@ public function testMaskSensitiveDataTraceDataKey(): void $this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']); } + + public function testHighlightFile(): void + { + $highlightFile = $this->getPrivateMethodInvoker($this->handler, 'highlightFile'); + + $output = $highlightFile(APPPATH . 'Controllers/Home.php', 9, 3); + + if (PHP_VERSION_ID >= 80300) { + $expect = <<< 'HTML' +
 8     {
+                 9         return view('welcome_message');
+                10     }
+                
+ HTML; + } else { + $expect = <<< 'HTML' +
 8     {
+                 9         return view('welcome_message');
+                10     }
+                
+ HTML; + } + + $this->assertSame($expect, $output); + } } From 4547ef3ec51f9d807005ed7c5081bf9fa8a10fa2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 5 Jan 2024 16:51:23 +0900 Subject: [PATCH 2/2] fix: error page does not show source files on PHP 8.3 --- system/Debug/BaseExceptionHandler.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/system/Debug/BaseExceptionHandler.php b/system/Debug/BaseExceptionHandler.php index 33dd126ff1ec..e90f95408c24 100644 --- a/system/Debug/BaseExceptionHandler.php +++ b/system/Debug/BaseExceptionHandler.php @@ -182,8 +182,11 @@ protected static function highlightFile(string $file, int $lineNumber, int $line $source = str_replace(["\r\n", "\r"], "\n", $source); $source = explode("\n", highlight_string($source, true)); - $source = str_replace('
', "\n", $source[1]); - $source = explode("\n", str_replace("\r\n", "\n", $source)); + + if (PHP_VERSION_ID < 80300) { + $source = str_replace('
', "\n", $source[1]); + $source = explode("\n", str_replace("\r\n", "\n", $source)); + } // Get just the part to show $start = max($lineNumber - (int) round($lines / 2), 0);