Skip to content

Commit 8448c89

Browse files
More experimentation with how branch and path coverage is presented
1 parent c871111 commit 8448c89

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
@@ -636,7 +636,8 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
636636
$testData = $node->testData();
637637
$codeLines = $this->loadFile($node->pathAsString());
638638

639-
$lineData = [];
639+
$lineData = [];
640+
$branchStartData = [];
640641

641642
foreach (array_keys($codeLines) as $line) {
642643
$lineData[$line + 1] = [
@@ -650,6 +651,20 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
650651
foreach ($functionCoverageData as $method) {
651652
/** @var ProcessedBranchCoverageData $branch */
652653
foreach ($method->branches as $branch) {
654+
$startLine = min($branch->line_start, $branch->line_end);
655+
656+
if (isset($lineData[$startLine])) {
657+
if (!isset($branchStartData[$startLine])) {
658+
$branchStartData[$startLine] = ['total' => 0, 'hit' => 0];
659+
}
660+
661+
$branchStartData[$startLine]['total']++;
662+
663+
if ($branch->hit !== []) {
664+
$branchStartData[$startLine]['hit']++;
665+
}
666+
}
667+
653668
foreach (range($branch->line_start, $branch->line_end) as $line) {
654669
if (!isset($lineData[$line])) { // blank line at end of file is sometimes included here
655670
continue;
@@ -673,6 +688,9 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
673688
$trClass = '';
674689
$popover = '';
675690

691+
$coverageCount = '';
692+
$coverageCountClass = 'coverage-count';
693+
676694
if ($lineData[$i]['includedInBranches'] > 0) {
677695
$lineCss = 'success';
678696

@@ -682,6 +700,10 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
682700
$lineCss = 'warning';
683701
}
684702

703+
if (isset($branchStartData[$i]) && $branchStartData[$i]['total'] > 1) {
704+
$coverageCount = $branchStartData[$i]['hit'] . '/' . $branchStartData[$i]['total'];
705+
}
706+
685707
$popoverContent = '<ul>';
686708

687709
if (count($lineData[$i]['tests']) === 1) {
@@ -705,7 +727,7 @@ private function renderSourceWithBranchCoverage(FileNode $node): string
705727
);
706728
}
707729

708-
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover);
730+
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover, $coverageCount, $coverageCountClass);
709731

710732
$i++;
711733
}
@@ -761,6 +783,8 @@ private function renderSourceWithPathCoverage(FileNode $node): string
761783
foreach ($codeLines as $line) {
762784
$trClass = '';
763785
$popover = '';
786+
$coverageCount = '';
787+
$coverageCountClass = 'coverage-count';
764788
$includedInPathsCount = count(array_unique($lineData[$i]['includedInPaths']));
765789
$includedInHitPathsCount = count(array_unique($lineData[$i]['includedInHitPaths']));
766790

@@ -796,7 +820,7 @@ private function renderSourceWithPathCoverage(FileNode $node): string
796820
);
797821
}
798822

799-
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover);
823+
$lines .= $this->renderLine($singleLineTemplate, $i, $line, $trClass, $popover, $coverageCount, $coverageCountClass);
800824

801825
$i++;
802826
}
@@ -971,14 +995,16 @@ private function renderPathStructure(FileNode $node): string
971995
return $pathsTemplate->render();
972996
}
973997

974-
private function renderLine(Template $template, int $lineNumber, string $lineContent, string $class, string $popover): string
998+
private function renderLine(Template $template, int $lineNumber, string $lineContent, string $class, string $popover, string $coverageCount = '', string $coverageCountClass = 'col-0'): string
975999
{
9761000
$template->setVar(
9771001
[
978-
'lineNumber' => (string) $lineNumber,
979-
'lineContent' => $lineContent,
980-
'class' => $class,
981-
'popover' => $popover,
1002+
'lineNumber' => (string) $lineNumber,
1003+
'lineContent' => $lineContent,
1004+
'class' => $class,
1005+
'popover' => $popover,
1006+
'coverageCount' => $coverageCount,
1007+
'coverageCountClass' => $coverageCountClass,
9821008
],
9831009
);
9841010

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)