Skip to content

Commit 7eec2d8

Browse files
Refactor array to value object
1 parent f63f0e2 commit 7eec2d8

File tree

11 files changed

+154
-76
lines changed

11 files changed

+154
-76
lines changed

src/Node/AbstractNode.php

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

2021
/**
2122
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
2223
*
23-
* @phpstan-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2424
* @phpstan-import-type ProcessedFunctionType from \SebastianBergmann\CodeCoverage\Node\File
2525
* @phpstan-import-type ProcessedClassType from \SebastianBergmann\CodeCoverage\Node\File
2626
* @phpstan-import-type ProcessedTraitType from \SebastianBergmann\CodeCoverage\Node\File
@@ -186,10 +186,7 @@ abstract public function traits(): array;
186186
*/
187187
abstract public function functions(): array;
188188

189-
/**
190-
* @return LinesOfCodeType
191-
*/
192-
abstract public function linesOfCode(): array;
189+
abstract public function linesOfCode(): LinesOfCode;
193190

194191
abstract public function numberOfExecutableLines(): int;
195192

src/Node/Directory.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
use function count;
1515
use IteratorAggregate;
1616
use RecursiveIteratorIterator;
17+
use SebastianBergmann\CodeCoverage\StaticAnalysis\LinesOfCode;
1718

1819
/**
1920
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
20-
*
21-
* @phpstan-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2221
*/
2322
final class Directory extends AbstractNode implements IteratorAggregate
2423
{
@@ -35,15 +34,11 @@ final class Directory extends AbstractNode implements IteratorAggregate
3534
/**
3635
* @var list<File>
3736
*/
38-
private array $files = [];
39-
private ?array $classes = null;
40-
private ?array $traits = null;
41-
private ?array $functions = null;
42-
43-
/**
44-
* @var null|LinesOfCodeType
45-
*/
46-
private ?array $linesOfCode = null;
37+
private array $files = [];
38+
private ?array $classes = null;
39+
private ?array $traits = null;
40+
private ?array $functions = null;
41+
private ?LinesOfCode $linesOfCode = null;
4742
private int $numFiles = -1;
4843
private int $numExecutableLines = -1;
4944
private int $numExecutedLines = -1;
@@ -165,25 +160,22 @@ public function functions(): array
165160
return $this->functions;
166161
}
167162

168-
/**
169-
* @return LinesOfCodeType
170-
*/
171-
public function linesOfCode(): array
163+
public function linesOfCode(): LinesOfCode
172164
{
173165
if ($this->linesOfCode === null) {
174-
$this->linesOfCode = [
175-
'linesOfCode' => 0,
176-
'commentLinesOfCode' => 0,
177-
'nonCommentLinesOfCode' => 0,
178-
];
166+
$linesOfCode = 0;
167+
$commentLinesOfCode = 0;
168+
$nonCommentLinesOfCode = 0;
179169

180170
foreach ($this->children as $child) {
181171
$childLinesOfCode = $child->linesOfCode();
182172

183-
$this->linesOfCode['linesOfCode'] += $childLinesOfCode['linesOfCode'];
184-
$this->linesOfCode['commentLinesOfCode'] += $childLinesOfCode['commentLinesOfCode'];
185-
$this->linesOfCode['nonCommentLinesOfCode'] += $childLinesOfCode['nonCommentLinesOfCode'];
173+
$linesOfCode += $childLinesOfCode->linesOfCode();
174+
$commentLinesOfCode += $childLinesOfCode->commentLinesOfCode();
175+
$nonCommentLinesOfCode += $childLinesOfCode->nonCommentLinesOfCode();
186176
}
177+
178+
$this->linesOfCode = new LinesOfCode($linesOfCode, $commentLinesOfCode, $nonCommentLinesOfCode);
187179
}
188180

189181
return $this->linesOfCode;

src/Node/File.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_filter;
1313
use function count;
1414
use function range;
15+
use SebastianBergmann\CodeCoverage\StaticAnalysis\LinesOfCode;
1516

1617
/**
1718
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
@@ -20,7 +21,6 @@
2021
* @phpstan-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2122
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2223
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
23-
* @phpstan-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2424
* @phpstan-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2525
*
2626
* @phpstan-type ProcessedFunctionType = array{
@@ -119,11 +119,7 @@ final class File extends AbstractNode
119119
* @var array<string, ProcessedFunctionType>
120120
*/
121121
private array $functions = [];
122-
123-
/**
124-
* @var LinesOfCodeType
125-
*/
126-
private readonly array $linesOfCode;
122+
private readonly LinesOfCode $linesOfCode;
127123
private ?int $numClasses = null;
128124
private int $numTestedClasses = 0;
129125
private ?int $numTraits = null;
@@ -142,9 +138,8 @@ final class File extends AbstractNode
142138
* @param array<string, CodeUnitClassType> $classes
143139
* @param array<string, CodeUnitTraitType> $traits
144140
* @param array<string, CodeUnitFunctionType> $functions
145-
* @param LinesOfCodeType $linesOfCode
146141
*/
147-
public function __construct(string $name, AbstractNode $parent, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, array $linesOfCode)
142+
public function __construct(string $name, AbstractNode $parent, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, LinesOfCode $linesOfCode)
148143
{
149144
parent::__construct($name, $parent);
150145

@@ -203,7 +198,7 @@ public function functions(): array
203198
return $this->functions;
204199
}
205200

206-
public function linesOfCode(): array
201+
public function linesOfCode(): LinesOfCode
207202
{
208203
return $this->linesOfCode;
209204
}
@@ -366,15 +361,15 @@ public function numberOfTestedFunctions(): int
366361
*/
367362
private function calculateStatistics(array $classes, array $traits, array $functions): void
368363
{
369-
foreach (range(1, $this->linesOfCode['linesOfCode']) as $lineNumber) {
364+
foreach (range(1, $this->linesOfCode->linesOfCode()) as $lineNumber) {
370365
$this->codeUnitsByLine[$lineNumber] = [];
371366
}
372367

373368
$this->processClasses($classes);
374369
$this->processTraits($traits);
375370
$this->processFunctions($functions);
376371

377-
foreach (range(1, $this->linesOfCode['linesOfCode']) as $lineNumber) {
372+
foreach (range(1, $this->linesOfCode->linesOfCode()) as $lineNumber) {
378373
if (isset($this->lineCoverageData[$lineNumber])) {
379374
foreach ($this->codeUnitsByLine[$lineNumber] as &$codeUnit) {
380375
$codeUnit['executableLines']++;

src/Report/Clover.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
168168
$linesOfCode = $item->linesOfCode();
169169

170170
$xmlMetrics = $xmlDocument->createElement('metrics');
171-
$xmlMetrics->setAttribute('loc', (string) $linesOfCode['linesOfCode']);
172-
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['nonCommentLinesOfCode']);
171+
$xmlMetrics->setAttribute('loc', (string) $linesOfCode->linesOfCode());
172+
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode->nonCommentLinesOfCode());
173173
$xmlMetrics->setAttribute('classes', (string) $item->numberOfClassesAndTraits());
174174
$xmlMetrics->setAttribute('methods', (string) $item->numberOfMethods());
175175
$xmlMetrics->setAttribute('coveredmethods', (string) $item->numberOfTestedMethods());
@@ -201,8 +201,8 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
201201

202202
$xmlMetrics = $xmlDocument->createElement('metrics');
203203
$xmlMetrics->setAttribute('files', (string) count($report));
204-
$xmlMetrics->setAttribute('loc', (string) $linesOfCode['linesOfCode']);
205-
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['nonCommentLinesOfCode']);
204+
$xmlMetrics->setAttribute('loc', (string) $linesOfCode->linesOfCode());
205+
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode->nonCommentLinesOfCode());
206206
$xmlMetrics->setAttribute('classes', (string) $report->numberOfClassesAndTraits());
207207
$xmlMetrics->setAttribute('methods', (string) $report->numberOfMethods());
208208
$xmlMetrics->setAttribute('coveredmethods', (string) $report->numberOfTestedMethods());

src/Report/Xml/Facade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ private function setTotals(AbstractNode $node, Totals $totals): void
229229
$loc = $node->linesOfCode();
230230

231231
$totals->setNumLines(
232-
$loc['linesOfCode'],
233-
$loc['commentLinesOfCode'],
234-
$loc['nonCommentLinesOfCode'],
232+
$loc->linesOfCode(),
233+
$loc->commentLinesOfCode(),
234+
$loc->nonCommentLinesOfCode(),
235235
$node->numberOfExecutableLines(),
236236
$node->numberOfExecutedLines(),
237237
);

src/StaticAnalysis/CachingFileAnalyser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @phpstan-import-type CodeUnitInterfaceType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2828
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2929
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
30-
* @phpstan-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
3130
* @phpstan-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
3231
*/
3332
final class CachingFileAnalyser implements FileAnalyser
@@ -97,10 +96,7 @@ public function functionsIn(string $filename): array
9796
return $this->cache[$filename]['functionsIn'];
9897
}
9998

100-
/**
101-
* @return LinesOfCodeType
102-
*/
103-
public function linesOfCodeFor(string $filename): array
99+
public function linesOfCodeFor(string $filename): LinesOfCode
104100
{
105101
if (!isset($this->cache[$filename])) {
106102
$this->process($filename);
@@ -166,7 +162,11 @@ private function read(string $filename): array|false
166162

167163
return unserialize(
168164
file_get_contents($cacheFile),
169-
['allowed_classes' => false],
165+
[
166+
'allowed_classes' => [
167+
LinesOfCode::class,
168+
],
169+
],
170170
);
171171
}
172172

src/StaticAnalysis/FileAnalyser.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
1919
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2020
*
21-
* @phpstan-type LinesOfCodeType = array{
22-
* linesOfCode: int,
23-
* commentLinesOfCode: int,
24-
* nonCommentLinesOfCode: int
25-
* }
2621
* @phpstan-type LinesType = array<int, int>
2722
*/
2823
interface FileAnalyser
@@ -47,10 +42,7 @@ public function traitsIn(string $filename): array;
4742
*/
4843
public function functionsIn(string $filename): array;
4944

50-
/**
51-
* @return LinesOfCodeType
52-
*/
53-
public function linesOfCodeFor(string $filename): array;
45+
public function linesOfCodeFor(string $filename): LinesOfCode;
5446

5547
/**
5648
* @return LinesType

src/StaticAnalysis/ParsingFileAnalyser.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
* @phpstan-import-type CodeUnitInterfaceType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
3838
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
3939
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
40-
* @phpstan-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
4140
* @phpstan-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
4241
*/
4342
final class ParsingFileAnalyser implements FileAnalyser
@@ -63,7 +62,7 @@ final class ParsingFileAnalyser implements FileAnalyser
6362
private array $functions = [];
6463

6564
/**
66-
* @var array<string, LinesOfCodeType>
65+
* @var array<string, LinesOfCode>
6766
*/
6867
private array $linesOfCode = [];
6968

@@ -125,10 +124,7 @@ public function functionsIn(string $filename): array
125124
return $this->functions[$filename];
126125
}
127126

128-
/**
129-
* @return LinesOfCodeType
130-
*/
131-
public function linesOfCodeFor(string $filename): array
127+
public function linesOfCodeFor(string $filename): LinesOfCode
132128
{
133129
$this->analyse($filename);
134130

@@ -229,11 +225,11 @@ private function analyse(string $filename): void
229225

230226
$result = $lineCountingVisitor->result();
231227

232-
$this->linesOfCode[$filename] = [
233-
'linesOfCode' => $result->linesOfCode(),
234-
'commentLinesOfCode' => $result->commentLinesOfCode(),
235-
'nonCommentLinesOfCode' => $result->nonCommentLinesOfCode(),
236-
];
228+
$this->linesOfCode[$filename] = new LinesOfCode(
229+
$result->linesOfCode(),
230+
$result->commentLinesOfCode(),
231+
$result->nonCommentLinesOfCode(),
232+
);
237233
}
238234

239235
private function findLinesIgnoredByLineBasedAnnotations(string $filename, string $source, bool $useAnnotationsForIgnoringCode): void
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\StaticAnalysis;
11+
12+
/**
13+
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
14+
*/
15+
final readonly class LinesOfCode
16+
{
17+
/**
18+
* @var non-negative-int
19+
*/
20+
private int $linesOfCode;
21+
22+
/**
23+
* @var non-negative-int
24+
*/
25+
private int $commentLinesOfCode;
26+
27+
/**
28+
* @var non-negative-int
29+
*/
30+
private int $nonCommentLinesOfCode;
31+
32+
/**
33+
* @param non-negative-int $linesOfCode
34+
* @param non-negative-int $commentLinesOfCode
35+
* @param non-negative-int $nonCommentLinesOfCode
36+
*/
37+
public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode)
38+
{
39+
$this->linesOfCode = $linesOfCode;
40+
$this->commentLinesOfCode = $commentLinesOfCode;
41+
$this->nonCommentLinesOfCode = $nonCommentLinesOfCode;
42+
}
43+
44+
/**
45+
* @return non-negative-int
46+
*/
47+
public function linesOfCode(): int
48+
{
49+
return $this->linesOfCode;
50+
}
51+
52+
/**
53+
* @return non-negative-int
54+
*/
55+
public function commentLinesOfCode(): int
56+
{
57+
return $this->commentLinesOfCode;
58+
}
59+
60+
/**
61+
* @return non-negative-int
62+
*/
63+
public function nonCommentLinesOfCode(): int
64+
{
65+
return $this->nonCommentLinesOfCode;
66+
}
67+
}

tests/tests/StaticAnalysis/ParsingFileAnalyserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testGetLinesOfCodeForFileWithoutNewline(): void
123123
1,
124124
(new ParsingFileAnalyser(false, false))->linesOfCodeFor(
125125
TEST_FILES_PATH . 'source_without_newline.php',
126-
)['linesOfCode'],
126+
)->linesOfCode(),
127127
);
128128
}
129129

@@ -134,9 +134,9 @@ public function testGetLinesOfCodeForFileCrLineEndings(): void
134134
TEST_FILES_PATH . 'source_without_lf_only_cr.php',
135135
);
136136

137-
$this->assertSame(4, $result['linesOfCode']);
138-
$this->assertSame(2, $result['commentLinesOfCode']);
139-
$this->assertSame(2, $result['nonCommentLinesOfCode']);
137+
$this->assertSame(4, $result->linesOfCode());
138+
$this->assertSame(2, $result->commentLinesOfCode());
139+
$this->assertSame(2, $result->nonCommentLinesOfCode());
140140
}
141141

142142
public function testLinesCanBeIgnoredUsingAttribute(): void

0 commit comments

Comments
 (0)