Skip to content

Commit

Permalink
code coverage: fix compatibility with PHP 8.3 [Closes #446]
Browse files Browse the repository at this point in the history
PHP 8.3 introduced new HTML output of highlight_string(). The main change is that output is wrapped by <pre>, so new lines are preserved (not converted to <br />) and spaces remains spaces (not converted to &nbsp;)
  • Loading branch information
milo committed Apr 17, 2024
1 parent 309dc19 commit c02fdda
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/CodeCoverage/Generators/template.phtml
Expand Up @@ -77,9 +77,8 @@
text-decoration: none;
}

code {
display: block;
white-space: nowrap;
pre {
margin: 0;
position: relative;
}

Expand Down Expand Up @@ -247,14 +246,33 @@ foreach ($files as $id => $info) {
$currentId = "F{$id}";
assignArrayByPath($directories, $info, $currentId);

$html = highlight_string($code, true);
if (PHP_VERSION_ID < 80300) { // Normalize to HTML introduced by PHP 8.3
$html = preg_replace(
[
'#^<code><span style="color: (.+?)">\n<span#',
'#<br />#',
'#&nbsp;#',
'#</span>\n</span>\n</code>$#'
],
[
'<code style="color: $1"><span',
"\n",
' ',
'</span></code>',
],
$html,
);
$html = "<pre>$html</pre>";
}

$data = (array) $info;
$data['digits'] = $digits;
$data['lineCount'] = $lineCount;
$data['content'] = strtr(highlight_string($code, true), [
'<code>' => "<code style='margin-left: {$digits}em'>",
$data['content'] = strtr($html, [
'<pre>' => "<pre style='margin-left: {$digits}em'>",
'<span style="color: ' => '<i class="',
'</span>' => '</i>',
'<br />' => '<br>',
]);
$jsonData[$currentId] = $data;
} ?>
Expand Down

0 comments on commit c02fdda

Please sign in to comment.