Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions system/Debug/BaseExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<br />', "\n", $source[1]);
$source = explode("\n", str_replace("\r\n", "\n", $source));

if (PHP_VERSION_ID < 80300) {
$source = str_replace('<br />', "\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);
Expand Down
25 changes: 25 additions & 0 deletions tests/system/Debug/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<pre><code><span class="line"><span class="number"> 8</span> </span><span style="color: #f1ce61;">{
<span class='line highlight'><span class='number'> 9</span> return view('welcome_message');
</span></span><span style="color: #c7c7c7"></span><span style="color: #f1ce61;"></span><span style="color: #869d6a"></span><span style="color: #f1ce61;"><span class="line"><span class="number">10</span> }
</span></code></pre>
HTML;
} else {
$expect = <<< 'HTML'
<pre><code><span class="line"><span class="number"> 8</span> &nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #f1ce61;">{
<span class='line highlight'><span class='number'> 9</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;view('welcome_message');
</span></span><span style="color: #c7c7c7"></span><span style="color: #f1ce61;"></span><span style="color: #869d6a"></span><span style="color: #f1ce61;"><span class="line"><span class="number">10</span> &nbsp;&nbsp;&nbsp;&nbsp;}
</span></code></pre>
HTML;
}

$this->assertSame($expect, $output);
}
}