Skip to content

Commit

Permalink
Merge pull request #8401 from michalsn/fix/highlightFile
Browse files Browse the repository at this point in the history
fix: `highlightFile()` in `BaseExceptionHandler` for PHP 8.3
  • Loading branch information
kenjis authored Jan 7, 2024
2 parents 766f76b + d558f09 commit fc62f96
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 3 deletions.
16 changes: 13 additions & 3 deletions system/Debug/BaseExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ 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));
} else {
// We have to remove these tags since we're preparing the result
// ourselves and these tags are added manually at the end.
$source = str_replace(['<pre><code>', '</code></pre>'], '', $source);
}

// Get just the part to show
$start = max($lineNumber - (int) round($lines / 2), 0);
Expand All @@ -199,7 +206,7 @@ protected static function highlightFile(string $file, int $lineNumber, int $line
// of open and close span tags on one line, we need
// to ensure we can close them all to get the lines
// showing correctly.
$spans = 1;
$spans = 0;

foreach ($source as $n => $row) {
$spans += substr_count($row, '<span') - substr_count($row, '</span');
Expand All @@ -216,6 +223,9 @@ protected static function highlightFile(string $file, int $lineNumber, int $line
);
} else {
$out .= sprintf('<span class="line"><span class="number">' . $format . '</span> %s', $n + $start + 1, $row) . "\n";
// We're closing only one span tag we added manually line before,
// so we have to increment $spans count to close this tag later.
$spans++;
}
}

Expand Down
33 changes: 33 additions & 0 deletions system/Test/IniTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Test;

trait IniTestTrait
{
private array $iniSettings = [];

private function backupIniValues(array $keys): void
{
foreach ($keys as $key) {
$this->iniSettings[$key] = ini_get($key);
}
}

private function restoreIniValues(): void
{
foreach ($this->iniSettings as $key => $value) {
ini_set($key, $value);
}

$this->iniSettings = [];
}
}
16 changes: 16 additions & 0 deletions tests/_support/Debug/highlightFile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre><code><span class="line"><span class="number"> 9</span> * the LICENSE file that was distributed with this source code.
<span class="line"><span class="number">10</span> */
<span class="line"><span class="number">11</span>
<span class="line"><span class="number">12</span> </span><span style="color: #f1ce61;">namespace </span><span style="color: #c7c7c7">Tests\Support\Controllers</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">13</span>
<span class="line"><span class="number">14</span> use </span><span style="color: #c7c7c7">CodeIgniter\Controller</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">15</span>
<span class='line highlight'><span class='number'>16</span> class Hello extends Controller
</span></span><span style="color: #c7c7c7"></span><span style="color: #f1ce61;"></span><span style="color: #c7c7c7"><span class="line"><span class="number">17</span> </span><span style="color: #f1ce61;">{
<span class="line"><span class="number">18</span> public function </span><span style="color: #c7c7c7">index</span><span style="color: #f1ce61;">()
<span class="line"><span class="number">19</span> {
<span class="line"><span class="number">20</span> return </span><span style="color: #869d6a">'Hello'</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">21</span> }
<span class="line"><span class="number">22</span> }
<span class="line"><span class="number">23</span> </span>
</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
16 changes: 16 additions & 0 deletions tests/_support/Debug/highlightFile_pre_80000.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre><code><span class="line"><span class="number"> 9</span> &nbsp;*&nbsp;the&nbsp;LICENSE&nbsp;file&nbsp;that&nbsp;was&nbsp;distributed&nbsp;with&nbsp;this&nbsp;source&nbsp;code.
<span class="line"><span class="number">10</span> &nbsp;*/
<span class="line"><span class="number">11</span>
<span class="line"><span class="number">12</span> </span><span style="color: #f1ce61;">namespace&nbsp;</span><span style="color: #c7c7c7">Tests</span><span style="color: #f1ce61;">\</span><span style="color: #c7c7c7">Support</span><span style="color: #f1ce61;">\</span><span style="color: #c7c7c7">Controllers</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">13</span>
<span class="line"><span class="number">14</span> use&nbsp;</span><span style="color: #c7c7c7">CodeIgniter</span><span style="color: #f1ce61;">\</span><span style="color: #c7c7c7">Controller</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">15</span>
<span class='line highlight'><span class='number'>16</span> class&nbsp;Hello&nbsp;extends&nbsp;Controller
</span></span><span style="color: #c7c7c7"></span><span style="color: #f1ce61;"></span><span style="color: #c7c7c7"><span class="line"><span class="number">17</span> </span><span style="color: #f1ce61;">{
<span class="line"><span class="number">18</span> &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #c7c7c7">index</span><span style="color: #f1ce61;">()
<span class="line"><span class="number">19</span> &nbsp;&nbsp;&nbsp;&nbsp;{
<span class="line"><span class="number">20</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #869d6a">'Hello'</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">21</span> &nbsp;&nbsp;&nbsp;&nbsp;}
<span class="line"><span class="number">22</span> }
<span class="line"><span class="number">23</span> </span>
</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
16 changes: 16 additions & 0 deletions tests/_support/Debug/highlightFile_pre_80300.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<pre><code><span class="line"><span class="number"> 9</span> &nbsp;*&nbsp;the&nbsp;LICENSE&nbsp;file&nbsp;that&nbsp;was&nbsp;distributed&nbsp;with&nbsp;this&nbsp;source&nbsp;code.
<span class="line"><span class="number">10</span> &nbsp;*/
<span class="line"><span class="number">11</span>
<span class="line"><span class="number">12</span> </span><span style="color: #f1ce61;">namespace&nbsp;</span><span style="color: #c7c7c7">Tests\Support\Controllers</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">13</span>
<span class="line"><span class="number">14</span> use&nbsp;</span><span style="color: #c7c7c7">CodeIgniter\Controller</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">15</span>
<span class='line highlight'><span class='number'>16</span> class&nbsp;Hello&nbsp;extends&nbsp;Controller
</span></span><span style="color: #c7c7c7"></span><span style="color: #f1ce61;"></span><span style="color: #c7c7c7"><span class="line"><span class="number">17</span> </span><span style="color: #f1ce61;">{
<span class="line"><span class="number">18</span> &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #c7c7c7">index</span><span style="color: #f1ce61;">()
<span class="line"><span class="number">19</span> &nbsp;&nbsp;&nbsp;&nbsp;{
<span class="line"><span class="number">20</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #869d6a">'Hello'</span><span style="color: #f1ce61;">;
<span class="line"><span class="number">21</span> &nbsp;&nbsp;&nbsp;&nbsp;}
<span class="line"><span class="number">22</span> }
<span class="line"><span class="number">23</span> </span>
</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
31 changes: 31 additions & 0 deletions tests/system/Debug/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Controllers\Home;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\IniTestTrait;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Exceptions as ExceptionsConfig;
use Config\Services;
Expand All @@ -27,6 +28,7 @@
final class ExceptionHandlerTest extends CIUnitTestCase
{
use StreamFilterTrait;
use IniTestTrait;

private ExceptionHandler $handler;

Expand Down Expand Up @@ -237,4 +239,33 @@ public function testMaskSensitiveDataTraceDataKey(): void

$this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']);
}

public function testHighlightFile(): void
{
$this->backupIniValues([
'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string',
]);

$highlightFile = $this->getPrivateMethodInvoker($this->handler, 'highlightFile');
$result = $highlightFile(SUPPORTPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Hello.php', 16);

switch (true) {
case PHP_VERSION_ID < 80000:
$resultFile = 'highlightFile_pre_80000.html';
break;

case PHP_VERSION_ID < 80300:
$resultFile = 'highlightFile_pre_80300.html';
break;

default:
$resultFile = 'highlightFile.html';
}

$expected = file_get_contents(SUPPORTPATH . 'Debug' . DIRECTORY_SEPARATOR . $resultFile);

$this->assertSame($expected, $result);

$this->restoreIniValues();
}
}
43 changes: 43 additions & 0 deletions tests/system/Test/IniTestTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Test;

use ReflectionException;

/**
* @internal
*
* @group Others
*/
final class IniTestTraitTest extends CIUnitTestCase
{
use IniTestTrait;

/**
* @throws ReflectionException
*/
public function testBackupAndRestoreIniValues(): void
{
$this->backupIniValues(['highlight.default']);
$backup = $this->getPrivateProperty($this, 'iniSettings');
$this->assertSame('#0000BB', $backup['highlight.default']);

ini_set('highlight.default', '#FFFFFF');
$this->assertSame('#FFFFFF', ini_get('highlight.default'));

$this->restoreIniValues();
$this->assertSame('#0000BB', ini_get('highlight.default'));

$backup = $this->getPrivateProperty($this, 'iniSettings');
$this->assertSame([], $backup);
}
}

0 comments on commit fc62f96

Please sign in to comment.