Skip to content

Commit 0c55c5f

Browse files
Refactor arrays to value objects
1 parent 7eec2d8 commit 0c55c5f

19 files changed

+1272
-317
lines changed

src/Node/File.php

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
use function array_filter;
1313
use function count;
1414
use function range;
15+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Class_;
16+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Function_;
1517
use SebastianBergmann\CodeCoverage\StaticAnalysis\LinesOfCode;
18+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Method;
19+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_;
1620

1721
/**
1822
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
1923
*
20-
* @phpstan-import-type CodeUnitFunctionType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
21-
* @phpstan-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
22-
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
23-
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
2424
* @phpstan-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2525
*
2626
* @phpstan-type ProcessedFunctionType = array{
@@ -129,15 +129,15 @@ final class File extends AbstractNode
129129
private ?int $numTestedFunctions = null;
130130

131131
/**
132-
* @var array<int, array|array{0: CodeUnitClassType, 1: string}|array{0: CodeUnitFunctionType}|array{0: CodeUnitTraitType, 1: string}>
132+
* @var array<int, array|array{0: Class_, 1: string}|array{0: Function_}|array{0: Trait_, 1: string}>
133133
*/
134134
private array $codeUnitsByLine = [];
135135

136136
/**
137137
* @param array<int, ?list<non-empty-string>> $lineCoverageData
138-
* @param array<string, CodeUnitClassType> $classes
139-
* @param array<string, CodeUnitTraitType> $traits
140-
* @param array<string, CodeUnitFunctionType> $functions
138+
* @param array<string, Class_> $classes
139+
* @param array<string, Trait_> $traits
140+
* @param array<string, Function_> $functions
141141
*/
142142
public function __construct(string $name, AbstractNode $parent, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, LinesOfCode $linesOfCode)
143143
{
@@ -355,9 +355,9 @@ public function numberOfTestedFunctions(): int
355355
}
356356

357357
/**
358-
* @param array<string, CodeUnitClassType> $classes
359-
* @param array<string, CodeUnitTraitType> $traits
360-
* @param array<string, CodeUnitFunctionType> $functions
358+
* @param array<string, Class_> $classes
359+
* @param array<string, Trait_> $traits
360+
* @param array<string, Function_> $functions
361361
*/
362362
private function calculateStatistics(array $classes, array $traits, array $functions): void
363363
{
@@ -462,7 +462,7 @@ private function calculateStatistics(array $classes, array $traits, array $funct
462462
}
463463

464464
/**
465-
* @param array<string, CodeUnitClassType> $classes
465+
* @param array<string, Class_> $classes
466466
*/
467467
private function processClasses(array $classes): void
468468
{
@@ -471,9 +471,9 @@ private function processClasses(array $classes): void
471471
foreach ($classes as $className => $class) {
472472
$this->classes[$className] = [
473473
'className' => $className,
474-
'namespace' => $class['namespace'],
474+
'namespace' => $class->namespace(),
475475
'methods' => [],
476-
'startLine' => $class['startLine'],
476+
'startLine' => $class->startLine(),
477477
'executableLines' => 0,
478478
'executedLines' => 0,
479479
'executableBranches' => 0,
@@ -483,11 +483,11 @@ private function processClasses(array $classes): void
483483
'ccn' => 0,
484484
'coverage' => 0,
485485
'crap' => 0,
486-
'link' => $link . $class['startLine'],
486+
'link' => $link . $class->startLine(),
487487
];
488488

489-
foreach ($class['methods'] as $methodName => $method) {
490-
$methodData = $this->newMethod($className, $methodName, $method, $link);
489+
foreach ($class->methods() as $methodName => $method) {
490+
$methodData = $this->newMethod($className, $method, $link);
491491
$this->classes[$className]['methods'][$methodName] = $methodData;
492492

493493
$this->classes[$className]['executableBranches'] += $methodData['executableBranches'];
@@ -500,7 +500,7 @@ private function processClasses(array $classes): void
500500
$this->numExecutablePaths += $methodData['executablePaths'];
501501
$this->numExecutedPaths += $methodData['executedPaths'];
502502

503-
foreach (range($method['startLine'], $method['endLine']) as $lineNumber) {
503+
foreach (range($method->startLine(), $method->endLine()) as $lineNumber) {
504504
$this->codeUnitsByLine[$lineNumber] = [
505505
&$this->classes[$className],
506506
&$this->classes[$className]['methods'][$methodName],
@@ -511,7 +511,7 @@ private function processClasses(array $classes): void
511511
}
512512

513513
/**
514-
* @param array<string, CodeUnitTraitType> $traits
514+
* @param array<string, Trait_> $traits
515515
*/
516516
private function processTraits(array $traits): void
517517
{
@@ -520,9 +520,9 @@ private function processTraits(array $traits): void
520520
foreach ($traits as $traitName => $trait) {
521521
$this->traits[$traitName] = [
522522
'traitName' => $traitName,
523-
'namespace' => $trait['namespace'],
523+
'namespace' => $trait->namespace(),
524524
'methods' => [],
525-
'startLine' => $trait['startLine'],
525+
'startLine' => $trait->startLine(),
526526
'executableLines' => 0,
527527
'executedLines' => 0,
528528
'executableBranches' => 0,
@@ -532,11 +532,11 @@ private function processTraits(array $traits): void
532532
'ccn' => 0,
533533
'coverage' => 0,
534534
'crap' => 0,
535-
'link' => $link . $trait['startLine'],
535+
'link' => $link . $trait->startLine(),
536536
];
537537

538-
foreach ($trait['methods'] as $methodName => $method) {
539-
$methodData = $this->newMethod($traitName, $methodName, $method, $link);
538+
foreach ($trait->methods() as $methodName => $method) {
539+
$methodData = $this->newMethod($traitName, $method, $link);
540540
$this->traits[$traitName]['methods'][$methodName] = $methodData;
541541

542542
$this->traits[$traitName]['executableBranches'] += $methodData['executableBranches'];
@@ -549,7 +549,7 @@ private function processTraits(array $traits): void
549549
$this->numExecutablePaths += $methodData['executablePaths'];
550550
$this->numExecutedPaths += $methodData['executedPaths'];
551551

552-
foreach (range($method['startLine'], $method['endLine']) as $lineNumber) {
552+
foreach (range($method->startLine(), $method->endLine()) as $lineNumber) {
553553
$this->codeUnitsByLine[$lineNumber] = [
554554
&$this->traits[$traitName],
555555
&$this->traits[$traitName]['methods'][$methodName],
@@ -560,7 +560,7 @@ private function processTraits(array $traits): void
560560
}
561561

562562
/**
563-
* @param array<string, CodeUnitFunctionType> $functions
563+
* @param array<string, Function_> $functions
564564
*/
565565
private function processFunctions(array $functions): void
566566
{
@@ -569,23 +569,23 @@ private function processFunctions(array $functions): void
569569
foreach ($functions as $functionName => $function) {
570570
$this->functions[$functionName] = [
571571
'functionName' => $functionName,
572-
'namespace' => $function['namespace'],
573-
'signature' => $function['signature'],
574-
'startLine' => $function['startLine'],
575-
'endLine' => $function['endLine'],
572+
'namespace' => $function->namespace(),
573+
'signature' => $function->signature(),
574+
'startLine' => $function->startLine(),
575+
'endLine' => $function->endLine(),
576576
'executableLines' => 0,
577577
'executedLines' => 0,
578578
'executableBranches' => 0,
579579
'executedBranches' => 0,
580580
'executablePaths' => 0,
581581
'executedPaths' => 0,
582-
'ccn' => $function['ccn'],
582+
'ccn' => $function->cyclomaticComplexity(),
583583
'coverage' => 0,
584584
'crap' => 0,
585-
'link' => $link . $function['startLine'],
585+
'link' => $link . $function->startLine(),
586586
];
587587

588-
foreach (range($function['startLine'], $function['endLine']) as $lineNumber) {
588+
foreach (range($function->startLine(), $function->endLine()) as $lineNumber) {
589589
$this->codeUnitsByLine[$lineNumber] = [&$this->functions[$functionName]];
590590
}
591591

@@ -629,31 +629,29 @@ static function (array $path)
629629
}
630630

631631
/**
632-
* @param CodeUnitMethodType $method
633-
*
634632
* @return ProcessedMethodType
635633
*/
636-
private function newMethod(string $className, string $methodName, array $method, string $link): array
634+
private function newMethod(string $className, Method $method, string $link): array
637635
{
638636
$methodData = [
639-
'methodName' => $methodName,
640-
'visibility' => $method['visibility'],
641-
'signature' => $method['signature'],
642-
'startLine' => $method['startLine'],
643-
'endLine' => $method['endLine'],
637+
'methodName' => $method->name(),
638+
'visibility' => $method->visibility(),
639+
'signature' => $method->signature(),
640+
'startLine' => $method->startLine(),
641+
'endLine' => $method->endLine(),
644642
'executableLines' => 0,
645643
'executedLines' => 0,
646644
'executableBranches' => 0,
647645
'executedBranches' => 0,
648646
'executablePaths' => 0,
649647
'executedPaths' => 0,
650-
'ccn' => $method['ccn'],
648+
'ccn' => $method->cyclomaticComplexity(),
651649
'coverage' => 0,
652650
'crap' => 0,
653-
'link' => $link . $method['startLine'],
651+
'link' => $link . $method->startLine(),
654652
];
655653

656-
$key = $className . '->' . $methodName;
654+
$key = $className . '->' . $method->name();
657655

658656
if (isset($this->functionCoverageData[$key]['branches'])) {
659657
$methodData['executableBranches'] = count(

src/StaticAnalysis/CachingFileAnalyser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
/**
2323
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
2424
*
25-
* @phpstan-import-type CodeUnitFunctionType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
26-
* @phpstan-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
27-
* @phpstan-import-type CodeUnitInterfaceType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
28-
* @phpstan-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
29-
* @phpstan-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
3025
* @phpstan-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
3126
*/
3227
final class CachingFileAnalyser implements FileAnalyser
@@ -49,7 +44,7 @@ public function __construct(string $directory, FileAnalyser $analyser, bool $use
4944
}
5045

5146
/**
52-
* @return array<string, CodeUnitInterfaceType>
47+
* @return array<string, Interface_>
5348
*/
5449
public function interfacesIn(string $filename): array
5550
{
@@ -61,7 +56,7 @@ public function interfacesIn(string $filename): array
6156
}
6257

6358
/**
64-
* @return array<string, CodeUnitClassType>
59+
* @return array<string, Class_>
6560
*/
6661
public function classesIn(string $filename): array
6762
{
@@ -73,7 +68,7 @@ public function classesIn(string $filename): array
7368
}
7469

7570
/**
76-
* @return array<string, CodeUnitTraitType>
71+
* @return array<string, Trait_>
7772
*/
7873
public function traitsIn(string $filename): array
7974
{
@@ -85,7 +80,7 @@ public function traitsIn(string $filename): array
8580
}
8681

8782
/**
88-
* @return array<string, CodeUnitFunctionType>
83+
* @return array<string, Function_>
8984
*/
9085
public function functionsIn(string $filename): array
9186
{
@@ -164,7 +159,12 @@ private function read(string $filename): array|false
164159
file_get_contents($cacheFile),
165160
[
166161
'allowed_classes' => [
162+
Class_::class,
163+
Function_::class,
164+
Interface_::class,
167165
LinesOfCode::class,
166+
Method::class,
167+
Trait_::class,
168168
],
169169
],
170170
);

0 commit comments

Comments
 (0)