Skip to content

Commit 4276667

Browse files
dvdougsebastianbergmann
authored andcommitted
Use path coverage as baseline for CRAP where available, and branch coverage for marking whether a method/class/function is covered
1 parent 96b9564 commit 4276667

File tree

3 files changed

+39
-44
lines changed

3 files changed

+39
-44
lines changed

src/Node/File.php

+34-39
Original file line numberDiff line numberDiff line change
@@ -441,95 +441,90 @@ private function calculateStatistics(): void
441441

442442
foreach ($this->traits as &$trait) {
443443
foreach ($trait['methods'] as &$method) {
444-
if ($method['executableLines'] > 0) {
445-
$method['coverage'] = ($method['executedLines'] /
446-
$method['executableLines']) * 100;
447-
} else {
448-
$method['coverage'] = 100;
449-
}
444+
$methodLineCoverage = $method['executableLines'] ? ($method['executedLines'] / $method['executableLines']) * 100 : 100;
445+
$methodBranchCoverage = $method['executableBranches'] ? ($method['executedBranches'] / $method['executableBranches']) * 100 : 0;
446+
$methodPathCoverage = $method['executablePaths'] ? ($method['executedPaths'] / $method['executablePaths']) * 100 : 0;
447+
448+
$method['coverage'] = $methodBranchCoverage ?: $methodLineCoverage;
450449

451450
$method['crap'] = $this->crap(
452451
$method['ccn'],
453-
$method['coverage']
452+
$methodPathCoverage ?: $methodLineCoverage
454453
);
455454

456455
$trait['ccn'] += $method['ccn'];
457456
}
458457

459458
unset($method);
460459

461-
if ($trait['executableLines'] > 0) {
462-
$trait['coverage'] = ($trait['executedLines'] /
463-
$trait['executableLines']) * 100;
460+
$traitLineCoverage = $trait['executableLines'] ? ($trait['executedLines'] / $trait['executableLines']) * 100 : 100;
461+
$traitBranchCoverage = $trait['executableBranches'] ? ($trait['executedBranches'] / $trait['executableBranches']) * 100 : 0;
462+
$traitPathCoverage = $trait['executablePaths'] ? ($trait['executedPaths'] / $trait['executablePaths']) * 100 : 0;
464463

465-
if ($trait['coverage'] === 100) {
466-
$this->numTestedClasses++;
467-
}
468-
} else {
469-
$trait['coverage'] = 100;
464+
$trait['coverage'] = $traitBranchCoverage ?: $traitLineCoverage;
465+
466+
if ($trait['executableLines'] > 0 && $trait['coverage'] === 100) {
467+
$this->numTestedClasses++;
470468
}
471469

472470
$trait['crap'] = $this->crap(
473471
$trait['ccn'],
474-
$trait['coverage']
472+
$traitPathCoverage ?: $traitLineCoverage
475473
);
476474
}
477475

478476
unset($trait);
479477

480478
foreach ($this->classes as &$class) {
481479
foreach ($class['methods'] as &$method) {
482-
if ($method['executableLines'] > 0) {
483-
$method['coverage'] = ($method['executedLines'] /
484-
$method['executableLines']) * 100;
485-
} else {
486-
$method['coverage'] = 100;
487-
}
480+
$methodLineCoverage = $method['executableLines'] ? ($method['executedLines'] / $method['executableLines']) * 100 : 100;
481+
$methodBranchCoverage = $method['executableBranches'] ? ($method['executedBranches'] / $method['executableBranches']) * 100 : 0;
482+
$methodPathCoverage = $method['executablePaths'] ? ($method['executedPaths'] / $method['executablePaths']) * 100 : 0;
483+
484+
$method['coverage'] = $methodBranchCoverage ?: $methodLineCoverage;
488485

489486
$method['crap'] = $this->crap(
490487
$method['ccn'],
491-
$method['coverage']
488+
$methodPathCoverage ?: $methodLineCoverage
492489
);
493490

494491
$class['ccn'] += $method['ccn'];
495492
}
496493

497494
unset($method);
498495

499-
if ($class['executableLines'] > 0) {
500-
$class['coverage'] = ($class['executedLines'] /
501-
$class['executableLines']) * 100;
496+
$classLineCoverage = $class['executableLines'] ? ($class['executedLines'] / $class['executableLines']) * 100 : 100;
497+
$classBranchCoverage = $class['executableBranches'] ? ($class['executedBranches'] / $class['executableBranches']) * 100 : 0;
498+
$classPathCoverage = $class['executablePaths'] ? ($class['executedPaths'] / $class['executablePaths']) * 100 : 0;
502499

503-
if ($class['coverage'] === 100) {
504-
$this->numTestedClasses++;
505-
}
506-
} else {
507-
$class['coverage'] = 100;
500+
$class['coverage'] = $classBranchCoverage ?: $classLineCoverage;
501+
502+
if ($class['executableLines'] > 0 && $class['coverage'] === 100) {
503+
$this->numTestedClasses++;
508504
}
509505

510506
$class['crap'] = $this->crap(
511507
$class['ccn'],
512-
$class['coverage']
508+
$classPathCoverage ?: $classLineCoverage
513509
);
514510
}
515511

516512
unset($class);
517513

518514
foreach ($this->functions as &$function) {
519-
if ($function['executableLines'] > 0) {
520-
$function['coverage'] = ($function['executedLines'] /
521-
$function['executableLines']) * 100;
522-
} else {
523-
$function['coverage'] = 100;
524-
}
515+
$functionLineCoverage = $function['executableLines'] ? ($function['executedLines'] / $function['executableLines']) * 100 : 100;
516+
$functionBranchCoverage = $function['executableBranches'] ? ($function['executedBranches'] / $function['executableBranches']) * 100 : 0;
517+
$functionPathCoverage = $function['executablePaths'] ? ($function['executedPaths'] / $function['executablePaths']) * 100 : 0;
518+
519+
$function['coverage'] = $functionBranchCoverage ?: $functionLineCoverage;
525520

526521
if ($function['coverage'] === 100) {
527522
$this->numTestedFunctions++;
528523
}
529524

530525
$function['crap'] = $this->crap(
531526
$function['ccn'],
532-
$function['coverage']
527+
$functionPathCoverage ?: $functionLineCoverage
533528
);
534529
}
535530
}

tests/_files/Report/HTML/PathCoverageForBankAccount/BankAccount.php.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
</td>
107107
<td class="warning small"><div align="right">75.00%</div></td>
108108
<td class="warning small"><div align="right">3&nbsp;/&nbsp;4</div></td>
109-
<td class="warning small">8.12</td>
109+
<td class="warning small">6.60</td>
110110
<td class="warning big"> <div class="progress">
111111
<div class="progress-bar bg-warning" role="progressbar" aria-valuenow="60.00" aria-valuemin="0" aria-valuemax="100" style="width: 60.00%">
112112
<span class="sr-only">60.00% covered (warning)</span>

tests/_files/Report/HTML/PathCoverageForBankAccount/dashboard.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ <h3>Insufficient Coverage</h3>
5757
</tr>
5858
</thead>
5959
<tbody>
60-
<tr><td><a href="BankAccount.php.html#2">BankAccount</a></td><td class="text-right">50%</td></tr>
60+
<tr><td><a href="BankAccount.php.html#2">BankAccount</a></td><td class="text-right">42%</td></tr>
6161

6262
</tbody>
6363
</table>
@@ -74,7 +74,7 @@ <h3>Project Risks</h3>
7474
</tr>
7575
</thead>
7676
<tbody>
77-
<tr><td><a href="BankAccount.php.html#2">BankAccount</a></td><td class="text-right">8</td></tr>
77+
<tr><td><a href="BankAccount.php.html#2">BankAccount</a></td><td class="text-right">6</td></tr>
7878

7979
</tbody>
8080
</table>
@@ -158,7 +158,7 @@ <h3>Project Risks</h3>
158158
.yAxis.tickFormat(d3.format('d'));
159159

160160
d3.select('#classCoverageDistribution svg')
161-
.datum(getCoverageDistributionData([0,0,0,0,0,0,1,0,0,0,0,0], "Class Coverage"))
161+
.datum(getCoverageDistributionData([0,0,0,0,0,1,0,0,0,0,0,0], "Class Coverage"))
162162
.transition().duration(500).call(chart);
163163

164164
nv.utils.windowResize(chart.update);
@@ -226,7 +226,7 @@ <h3>Project Risks</h3>
226226
chart.yAxis.axisLabel('Cyclomatic Complexity');
227227

228228
d3.select('#classComplexity svg')
229-
.datum(getComplexityData([[50,5,"<a href=\"BankAccount.php.html#2\">BankAccount<\/a>"]], 'Class Complexity'))
229+
.datum(getComplexityData([[42.857142857143,5,"<a href=\"BankAccount.php.html#2\">BankAccount<\/a>"]], 'Class Complexity'))
230230
.transition()
231231
.duration(500)
232232
.call(chart);

0 commit comments

Comments
 (0)