Skip to content

Commit 9fa169f

Browse files
More experimentation with how branch and path coverage is presented
1 parent 4f71d5d commit 9fa169f

9 files changed

Lines changed: 203 additions & 163 deletions

File tree

src/Report/Html/Renderer/File.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
638638
$testData = $node->testData();
639639
$codeLines = $this->loadFile($node->pathAsString());
640640

641-
$lineData = [];
641+
$lineData = [];
642+
$branchStartData = [];
642643

643644
foreach (array_keys($codeLines) as $line) {
644645
$lineData[$line + 1] = [
@@ -652,6 +653,20 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
652653
foreach ($functionCoverageData as $method) {
653654
/** @var ProcessedBranchCoverageData $branch */
654655
foreach ($method->branches as $branch) {
656+
$startLine = min($branch->line_start, $branch->line_end);
657+
658+
if (isset($lineData[$startLine])) {
659+
if (!isset($branchStartData[$startLine])) {
660+
$branchStartData[$startLine] = ['total' => 0, 'hit' => 0];
661+
}
662+
663+
$branchStartData[$startLine]['total']++;
664+
665+
if ($branch->hit !== []) {
666+
$branchStartData[$startLine]['hit']++;
667+
}
668+
}
669+
655670
foreach (range($branch->line_start, $branch->line_end) as $line) {
656671
if (!isset($lineData[$line])) { // blank line at end of file is sometimes included here
657672
continue;
@@ -675,6 +690,9 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
675690
$trClass = '';
676691
$popover = '';
677692

693+
$coverageCount = '';
694+
$coverageCountClass = 'coverage-count';
695+
678696
if ($lineData[$i]['includedInBranches'] > 0) {
679697
$lineCss = 'success';
680698

@@ -684,6 +702,10 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
684702
$lineCss = 'warning';
685703
}
686704

705+
if (isset($branchStartData[$i]) && $branchStartData[$i]['total'] > 1) {
706+
$coverageCount = $branchStartData[$i]['hit'] . '/' . $branchStartData[$i]['total'];
707+
}
708+
687709
$popoverContent = '<ul>';
688710

689711
if (count($lineData[$i]['tests']) === 1) {
@@ -707,7 +729,7 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
707729
);
708730
}
709731

710-
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover);
732+
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover, $coverageCount, $coverageCountClass);
711733

712734
$i++;
713735
}
@@ -763,6 +785,8 @@ private function renderSourceWithPathCoverage(FileNode $node): string
763785
foreach ($codeLines as $line) {
764786
$trClass = '';
765787
$popover = '';
788+
$coverageCount = '';
789+
$coverageCountClass = 'coverage-count';
766790
$includedInPathsCount = count(array_unique($lineData[$i]['includedInPaths']));
767791
$includedInHitPathsCount = count(array_unique($lineData[$i]['includedInHitPaths']));
768792

@@ -798,7 +822,7 @@ private function renderSourceWithPathCoverage(FileNode $node): string
798822
);
799823
}
800824

801-
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover);
825+
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover, $coverageCount, $coverageCountClass);
802826

803827
$i++;
804828
}
@@ -973,14 +997,16 @@ private function renderPathStructure(FileNode $node): string
973997
return $pathsTemplate->render();
974998
}
975999

976-
private function renderLine(Template $template, int $lineNumber, string $lineContent, string $class, string $popover): string
1000+
private function renderLine(Template $template, int $lineNumber, string $lineContent, string $class, string $popover, string $coverageCount = '', string $coverageCountClass = 'col-0'): string
9771001
{
9781002
$template->setVar(
9791003
[
980-
'lineNumber' => (string) $lineNumber,
981-
'lineContent' => $lineContent,
982-
'class' => $class,
983-
'popover' => $popover,
1004+
'lineNumber' => (string) $lineNumber,
1005+
'lineContent' => $lineContent,
1006+
'class' => $class,
1007+
'popover' => $popover,
1008+
'coverageCount' => $coverageCount,
1009+
'coverageCountClass' => $coverageCountClass,
9841010
],
9851011
);
9861012

src/Report/Html/Renderer/Template/css/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ td.codeLine {
171171
white-space: pre-wrap;
172172
}
173173

174+
td.coverage-count {
175+
font-family: var(--bs-font-monospace);
176+
font-size: 0.75em;
177+
text-align: center;
178+
vertical-align: middle;
179+
white-space: nowrap;
180+
width: 3.5em;
181+
flex: 0 0 3.5em;
182+
}
183+
184+
td.col-0 {
185+
display: none;
186+
}
187+
174188
td span.comment {
175189
color: var(--bs-secondary-color);
176190
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<tr class="{{class}} d-flex"><td {{popover}} class="col-1 text-end"><a id="{{lineNumber}}" href="#{{lineNumber}}">{{lineNumber}}</a></td><td class="col-11 codeLine">{{lineContent}}</td></tr>
1+
<tr class="{{class}} d-flex"><td {{popover}} class="col-1 text-end"><a id="{{lineNumber}}" href="#{{lineNumber}}">{{lineNumber}}</a></td><td class="{{coverageCountClass}}">{{coverageCount}}</td><td class="col codeLine">{{lineContent}}</td></tr>

0 commit comments

Comments
 (0)