Skip to content

Commit 034866b

Browse files
committed
Represent line-coverage data in objects
1 parent 37047b5 commit 034866b

File tree

13 files changed

+285
-249
lines changed

13 files changed

+285
-249
lines changed

src/Data/ProcessedFunctionType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Data;
11+
12+
final class ProcessedFunctionType
13+
{
14+
public function __construct(
15+
public string $functionName,
16+
public string $namespace,
17+
public string $signature,
18+
public int $startLine,
19+
public int $endLine,
20+
public int $executableLines,
21+
public int $executedLines,
22+
public int $executableBranches,
23+
public int $executedBranches,
24+
public int $executablePaths,
25+
public int $executedPaths,
26+
public int $ccn,
27+
public float|int $coverage,
28+
public int|string $crap,
29+
public string $link,
30+
) {
31+
}
32+
}

src/Data/ProcessedMethodType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Data;
11+
12+
final class ProcessedMethodType
13+
{
14+
public function __construct(
15+
public string $methodName,
16+
public string $visibility,
17+
public string $signature,
18+
public int $startLine,
19+
public int $endLine,
20+
public int $executableLines,
21+
public int $executedLines,
22+
public int $executableBranches,
23+
public int $executedBranches,
24+
public int $executablePaths,
25+
public int $executedPaths,
26+
public int $ccn,
27+
public float|int $coverage,
28+
public int|string $crap,
29+
public string $link,
30+
) {
31+
}
32+
}

src/Node/AbstractNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
use function str_replace;
1616
use function substr;
1717
use Countable;
18+
use SebastianBergmann\CodeCoverage\Data\ProcessedFunctionType;
1819
use SebastianBergmann\CodeCoverage\StaticAnalysis\LinesOfCode;
1920
use SebastianBergmann\CodeCoverage\Util\Percentage;
2021

2122
/**
2223
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
2324
*
24-
* @phpstan-import-type ProcessedFunctionType from File
2525
* @phpstan-import-type ProcessedClassType from File
2626
* @phpstan-import-type ProcessedTraitType from File
2727
*/
@@ -190,7 +190,7 @@ public function cyclomaticComplexity(): int
190190
}
191191

192192
foreach ($this->functions() as $function) {
193-
$ccn += $function['ccn'];
193+
$ccn += $function->ccn;
194194
}
195195

196196
return $ccn;

src/Node/Directory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use function count;
1515
use IteratorAggregate;
1616
use RecursiveIteratorIterator;
17+
use SebastianBergmann\CodeCoverage\Data\ProcessedFunctionType;
1718
use SebastianBergmann\CodeCoverage\StaticAnalysis\LinesOfCode;
1819

1920
/**
2021
* @template-implements IteratorAggregate<int, AbstractNode>
2122
*
22-
* @phpstan-import-type ProcessedFunctionType from File
2323
* @phpstan-import-type ProcessedClassType from File
2424
* @phpstan-import-type ProcessedTraitType from File
2525
*

src/Node/File.php

Lines changed: 110 additions & 133 deletions
Large diffs are not rendered by default.

src/Report/Clover.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,33 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
8080

8181
foreach ($class['methods'] as $methodName => $method) {
8282
/** @phpstan-ignore equal.notAllowed */
83-
if ($method['executableLines'] == 0) {
83+
if ($method->executableLines == 0) {
8484
continue;
8585
}
8686

8787
$classMethods++;
88-
$classStatements += $method['executableLines'];
89-
$coveredClassStatements += $method['executedLines'];
88+
$classStatements += $method->executableLines;
89+
$coveredClassStatements += $method->executedLines;
9090

9191
/** @phpstan-ignore equal.notAllowed */
92-
if ($method['coverage'] == 100) {
92+
if ($method->coverage == 100) {
9393
$coveredMethods++;
9494
}
9595

9696
$methodCount = 0;
9797

98-
foreach (range($method['startLine'], $method['endLine']) as $line) {
98+
foreach (range($method->startLine, $method->endLine) as $line) {
9999
if (isset($coverageData[$line])) {
100100
$methodCount = max($methodCount, count($coverageData[$line]));
101101
}
102102
}
103103

104-
$lines[$method['startLine']] = [
105-
'ccn' => $method['ccn'],
104+
$lines[$method->startLine] = [
105+
'ccn' => $method->ccn,
106106
'count' => $methodCount,
107-
'crap' => $method['crap'],
107+
'crap' => $method->crap,
108108
'type' => 'method',
109-
'visibility' => $method['visibility'],
109+
'visibility' => $method->visibility,
110110
'name' => $methodName,
111111
];
112112
}

src/Report/Cobertura.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
144144
$classElement->appendChild($classLinesElement);
145145

146146
foreach ($class['methods'] as $methodName => $method) {
147-
if ($method['executableLines'] === 0) {
147+
if ($method->executableLines === 0) {
148148
continue;
149149
}
150150

151-
preg_match("/\((.*?)\)/", $method['signature'], $signature);
151+
preg_match("/\((.*?)\)/", $method->signature, $signature);
152152

153-
$linesValid = $method['executableLines'];
154-
$linesCovered = $method['executedLines'];
153+
$linesValid = $method->executableLines;
154+
$linesCovered = $method->executedLines;
155155
$lineRate = $linesCovered / $linesValid;
156156

157-
$branchesValid = $method['executableBranches'];
158-
$branchesCovered = $method['executedBranches'];
157+
$branchesValid = $method->executableBranches;
158+
$branchesCovered = $method->executedBranches;
159159
$branchRate = $branchesValid === 0 ? 0 : ($branchesCovered / $branchesValid);
160160

161161
$methodElement = $document->createElement('method');
@@ -164,13 +164,13 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
164164
$methodElement->setAttribute('signature', $signature[1]);
165165
$methodElement->setAttribute('line-rate', (string) $lineRate);
166166
$methodElement->setAttribute('branch-rate', (string) $branchRate);
167-
$methodElement->setAttribute('complexity', (string) $method['ccn']);
167+
$methodElement->setAttribute('complexity', (string) $method->ccn);
168168

169169
$methodLinesElement = $document->createElement('lines');
170170

171171
$methodElement->appendChild($methodLinesElement);
172172

173-
foreach (range($method['startLine'], $method['endLine']) as $line) {
173+
foreach (range($method->startLine, $method->endLine) as $line) {
174174
if (!isset($coverageData[$line])) {
175175
continue;
176176
}
@@ -217,23 +217,23 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
217217
$functions = $item->functions();
218218

219219
foreach ($functions as $functionName => $function) {
220-
if ($function['executableLines'] === 0) {
220+
if ($function->executableLines === 0) {
221221
continue;
222222
}
223223

224-
$complexity += $function['ccn'];
225-
$packageComplexity += $function['ccn'];
226-
$functionsComplexity += $function['ccn'];
224+
$complexity += $function->ccn;
225+
$packageComplexity += $function->ccn;
226+
$functionsComplexity += $function->ccn;
227227

228-
$linesValid = $function['executableLines'];
229-
$linesCovered = $function['executedLines'];
228+
$linesValid = $function->executableLines;
229+
$linesCovered = $function->executedLines;
230230
$lineRate = $linesCovered / $linesValid;
231231

232232
$functionsLinesValid += $linesValid;
233233
$functionsLinesCovered += $linesCovered;
234234

235-
$branchesValid = $function['executableBranches'];
236-
$branchesCovered = $function['executedBranches'];
235+
$branchesValid = $function->executableBranches;
236+
$branchesCovered = $function->executedBranches;
237237
$branchRate = $branchesValid === 0 ? 0 : ($branchesCovered / $branchesValid);
238238

239239
$functionsBranchesValid += $branchesValid;
@@ -242,16 +242,16 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
242242
$methodElement = $document->createElement('method');
243243

244244
$methodElement->setAttribute('name', $functionName);
245-
$methodElement->setAttribute('signature', $function['signature']);
245+
$methodElement->setAttribute('signature', $function->signature);
246246
$methodElement->setAttribute('line-rate', (string) $lineRate);
247247
$methodElement->setAttribute('branch-rate', (string) $branchRate);
248-
$methodElement->setAttribute('complexity', (string) $function['ccn']);
248+
$methodElement->setAttribute('complexity', (string) $function->ccn);
249249

250250
$methodLinesElement = $document->createElement('lines');
251251

252252
$methodElement->appendChild($methodLinesElement);
253253

254-
foreach (range($function['startLine'], $function['endLine']) as $line) {
254+
foreach (range($function->startLine, $function->endLine) as $line) {
255255
if (!isset($coverageData[$line])) {
256256
continue;
257257
}

src/Report/Crap4j.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
7171

7272
foreach ($classes as $className => $class) {
7373
foreach ($class['methods'] as $methodName => $method) {
74-
$crapLoad = $this->crapLoad((float) $method['crap'], $method['ccn'], $method['coverage']);
74+
$crapLoad = $this->crapLoad((float) $method->crap, $method->ccn, $method->coverage);
7575

76-
$fullCrap += $method['crap'];
76+
$fullCrap += $method->crap;
7777
$fullCrapLoad += $crapLoad;
7878
$fullMethodCount++;
7979

80-
if ($method['crap'] >= $this->threshold) {
80+
if ($method->crap >= $this->threshold) {
8181
$fullCrapMethodCount++;
8282
}
8383

@@ -90,11 +90,11 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
9090
$methodNode->appendChild($document->createElement('package', $namespace));
9191
$methodNode->appendChild($document->createElement('className', $className));
9292
$methodNode->appendChild($document->createElement('methodName', $methodName));
93-
$methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature'])));
94-
$methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature'])));
95-
$methodNode->appendChild($document->createElement('crap', (string) $this->roundValue((float) $method['crap'])));
96-
$methodNode->appendChild($document->createElement('complexity', (string) $method['ccn']));
97-
$methodNode->appendChild($document->createElement('coverage', (string) $this->roundValue($method['coverage'])));
93+
$methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method->signature)));
94+
$methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method->signature)));
95+
$methodNode->appendChild($document->createElement('crap', (string) $this->roundValue((float) $method->crap)));
96+
$methodNode->appendChild($document->createElement('complexity', (string) $method->ccn));
97+
$methodNode->appendChild($document->createElement('coverage', (string) $this->roundValue($method->coverage)));
9898
$methodNode->appendChild($document->createElement('crapLoad', (string) round($crapLoad)));
9999

100100
$methodsNode->appendChild($methodNode);

src/Report/Html/Renderer/Dashboard.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function str_replace;
2121
use function uasort;
2222
use function usort;
23+
use SebastianBergmann\CodeCoverage\Data\ProcessedMethodType;
2324
use SebastianBergmann\CodeCoverage\FileCouldNotBeWrittenException;
2425
use SebastianBergmann\CodeCoverage\Node\AbstractNode;
2526
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
@@ -102,11 +103,11 @@ private function complexity(array $classes, string $baseLink): array
102103
}
103104

104105
$result['method'][] = [
105-
$method['coverage'],
106-
$method['ccn'],
107-
str_replace($baseLink, '', $method['link']),
106+
$method->coverage,
107+
$method->ccn,
108+
str_replace($baseLink, '', $method->link),
108109
$methodName,
109-
$method['crap'],
110+
$method->crap,
110111
];
111112
}
112113

@@ -173,12 +174,12 @@ private function coverageDistribution(array $classes): array
173174

174175
foreach ($classes as $class) {
175176
foreach ($class['methods'] as $methodName => $method) {
176-
if ($method['coverage'] === 0) {
177+
if ($method->coverage === 0) {
177178
$result['method']['0%']++;
178-
} elseif ($method['coverage'] === 100) {
179+
} elseif ($method->coverage === 100) {
179180
$result['method']['100%']++;
180181
} else {
181-
$key = floor($method['coverage'] / 10) * 10;
182+
$key = floor($method->coverage / 10) * 10;
182183
$key = $key . '-' . ($key + 10) . '%';
183184
$result['method'][$key]++;
184185
}
@@ -219,14 +220,14 @@ private function insufficientCoverage(array $classes, string $baseLink): array
219220

220221
foreach ($classes as $className => $class) {
221222
foreach ($class['methods'] as $methodName => $method) {
222-
if ($method['coverage'] < $this->thresholds->highLowerBound()) {
223+
if ($method->coverage < $this->thresholds->highLowerBound()) {
223224
$key = $methodName;
224225

225226
if ($className !== '*') {
226227
$key = $className . '::' . $methodName;
227228
}
228229

229-
$leastTestedMethods[$key] = $method['coverage'];
230+
$leastTestedMethods[$key] = $method->coverage;
230231
}
231232
}
232233

@@ -252,7 +253,7 @@ private function insufficientCoverage(array $classes, string $baseLink): array
252253

253254
$result['method'] .= sprintf(
254255
' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%d%%</td></tr>' . "\n",
255-
str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']),
256+
str_replace($baseLink, '', $classes[$class]['methods'][$method]->link),
256257
$methodName,
257258
$method,
258259
$coverage,
@@ -275,7 +276,7 @@ private function projectRisks(array $classes, string $baseLink): array
275276

276277
foreach ($classes as $className => $class) {
277278
foreach ($class['methods'] as $methodName => $method) {
278-
if ($method['coverage'] < $this->thresholds->highLowerBound() && $method['ccn'] > 1) {
279+
if ($method->coverage < $this->thresholds->highLowerBound() && $method->ccn > 1) {
279280
$key = $methodName;
280281

281282
if ($className !== '*') {
@@ -296,9 +297,9 @@ private function projectRisks(array $classes, string $baseLink): array
296297
{
297298
return ((int) ($a['crap']) <=> (int) ($b['crap'])) * -1;
298299
});
299-
uasort($methodRisks, static function (array $a, array $b)
300+
uasort($methodRisks, static function (ProcessedMethodType $a, ProcessedMethodType $b)
300301
{
301-
return ((int) ($a['crap']) <=> (int) ($b['crap'])) * -1;
302+
return ((int) ($a->crap) <=> (int) ($b->crap)) * -1;
302303
});
303304

304305
foreach ($classRisks as $className => $class) {
@@ -317,12 +318,12 @@ private function projectRisks(array $classes, string $baseLink): array
317318

318319
$result['method'] .= sprintf(
319320
' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%.1f%%</td><td class="text-right">%d</td><td class="text-right">%d</td></tr>' . "\n",
320-
str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']),
321+
str_replace($baseLink, '', $classes[$class]['methods'][$method]->link),
321322
$methodName,
322323
$method,
323-
$methodVals['coverage'],
324-
$methodVals['ccn'],
325-
$methodVals['crap'],
324+
$methodVals->coverage,
325+
$methodVals->ccn,
326+
$methodVals->crap,
326327
);
327328
}
328329

0 commit comments

Comments
 (0)