Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3b78366

Browse files
localheinzsebastianbergmann
authored andcommittedMar 20, 2023
Enhancement: Document array shapes
1 parent ef1c71e commit 3b78366

14 files changed

+344
-35
lines changed
 

‎src/CodeCoverage.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535

3636
/**
3737
* Provides collection functionality for PHP code coverage information.
38+
*
39+
* @psalm-type TestType = array{
40+
* size: string,
41+
* status: string,
42+
* }
3843
*/
3944
final class CodeCoverage
4045
{
@@ -51,7 +56,7 @@ final class CodeCoverage
5156
private bool $useAnnotationsForIgnoringCode = true;
5257

5358
/**
54-
* @psalm-var array<string, array{size: string, status: string}>
59+
* @psalm-var array<string, TestType>
5560
*/
5661
private array $tests = [];
5762

@@ -120,15 +125,15 @@ public function setData(ProcessedCodeCoverageData $data): void
120125
}
121126

122127
/**
123-
* @psalm-return array<string, array{size: string, status: string}>
128+
* @psalm-return array<string, TestType>
124129
*/
125130
public function getTests(): array
126131
{
127132
return $this->tests;
128133
}
129134

130135
/**
131-
* @psalm-param array<string, array{size: string, status: string}> $tests
136+
* @psalm-param array<string, TestType> $tests
132137
*/
133138
public function setTests(array $tests): void
134139
{

‎src/Data/ProcessedCodeCoverageData.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,42 @@
2020

2121
/**
2222
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
23+
*
24+
* @psalm-import-type XdebugFunctionCoverageType from \SebastianBergmann\CodeCoverage\Driver\XdebugDriver
25+
*
26+
* @psalm-type TestIdType = string
2327
*/
2428
final class ProcessedCodeCoverageData
2529
{
2630
/**
2731
* Line coverage data.
2832
* An array of filenames, each having an array of linenumbers, each executable line having an array of testcase ids.
33+
*
34+
* @psalm-var array<string, array<int, null|list<TestIdType>>>
2935
*/
3036
private array $lineCoverage = [];
3137

3238
/**
3339
* Function coverage data.
3440
* Maintains base format of raw data (@see https://xdebug.org/docs/code_coverage), but each 'hit' entry is an array
3541
* of testcase ids.
42+
*
43+
* @psalm-var array<string, array<string, array{
44+
* branches: array<int, array{
45+
* op_start: int,
46+
* op_end: int,
47+
* line_start: int,
48+
* line_end: int,
49+
* hit: list<TestIdType>,
50+
* out: array<int, int>,
51+
* out_hit: array<int, int>,
52+
* }>,
53+
* paths: array<int, array{
54+
* path: array<int, int>,
55+
* hit: list<TestIdType>,
56+
* }>,
57+
* hit: list<TestIdType>
58+
* }>>
3659
*/
3760
private array $functionCoverage = [];
3861

@@ -213,6 +236,8 @@ private function priorityForLine(array $data, int $line): int
213236

214237
/**
215238
* For a function we have never seen before, copy all data over and simply init the 'hit' array.
239+
*
240+
* @psalm-param XdebugFunctionCoverageType $functionData
216241
*/
217242
private function initPreviouslyUnseenFunction(string $file, string $functionName, array $functionData): void
218243
{
@@ -231,6 +256,8 @@ private function initPreviouslyUnseenFunction(string $file, string $functionName
231256
* For a function we have seen before, only copy over and init the 'hit' array for any unseen branches and paths.
232257
* Techniques such as mocking and where the contents of a file are different vary during tests (e.g. compiling
233258
* containers) mean that the functions inside a file cannot be relied upon to be static.
259+
*
260+
* @psalm-param XdebugFunctionCoverageType $functionData
234261
*/
235262
private function initPreviouslySeenFunction(string $file, string $functionName, array $functionData): void
236263
{

‎src/Data/RawCodeCoverageData.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
/**
2828
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
29+
*
30+
* @psalm-import-type XdebugFunctionsCoverageType from \SebastianBergmann\CodeCoverage\Driver\XdebugDriver
31+
* @psalm-import-type XdebugCodeCoverageWithoutPathCoverageType from \SebastianBergmann\CodeCoverage\Driver\XdebugDriver
32+
* @psalm-import-type XdebugCodeCoverageWithPathCoverageType from \SebastianBergmann\CodeCoverage\Driver\XdebugDriver
2933
*/
3034
final class RawCodeCoverageData
3135
{
@@ -35,20 +39,26 @@ final class RawCodeCoverageData
3539
private static array $emptyLineCache = [];
3640

3741
/**
38-
* @see https://xdebug.org/docs/code_coverage for format
42+
* @psalm-var XdebugCodeCoverageWithoutPathCoverageType
3943
*/
4044
private array $lineCoverage;
4145

4246
/**
43-
* @see https://xdebug.org/docs/code_coverage for format
47+
* @psalm-var array<string, XdebugFunctionsCoverageType>
4448
*/
4549
private array $functionCoverage;
4650

51+
/**
52+
* @psalm-param XdebugCodeCoverageWithoutPathCoverageType $rawCoverage
53+
*/
4754
public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self
4855
{
4956
return new self($rawCoverage, []);
5057
}
5158

59+
/**
60+
* @psalm-param XdebugCodeCoverageWithPathCoverageType $rawCoverage
61+
*/
5262
public static function fromXdebugWithPathCoverage(array $rawCoverage): self
5363
{
5464
$lineCoverage = [];
@@ -73,6 +83,10 @@ public static function fromUncoveredFile(string $filename, FileAnalyser $analyse
7383
return new self([$filename => $lineCoverage], []);
7484
}
7585

86+
/**
87+
* @psalm-param XdebugCodeCoverageWithoutPathCoverageType $lineCoverage
88+
* @psalm-param array<string, XdebugFunctionsCoverageType> $functionCoverage
89+
*/
7690
private function __construct(array $lineCoverage, array $functionCoverage)
7791
{
7892
$this->lineCoverage = $lineCoverage;
@@ -86,11 +100,17 @@ public function clear(): void
86100
$this->lineCoverage = $this->functionCoverage = [];
87101
}
88102

103+
/**
104+
* @psalm-return XdebugCodeCoverageWithoutPathCoverageType
105+
*/
89106
public function lineCoverage(): array
90107
{
91108
return $this->lineCoverage;
92109
}
93110

111+
/**
112+
* @psalm-return array<string, XdebugFunctionsCoverageType>
113+
*/
94114
public function functionCoverage(): array
95115
{
96116
return $this->functionCoverage;

‎src/Driver/XdebugDriver.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@
3131

3232
/**
3333
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
34+
*
35+
* @see https://xdebug.org/docs/code_coverage#xdebug_get_code_coverage
36+
*
37+
* @psalm-type XdebugLinesCoverageType = array<int, int>
38+
* @psalm-type XdebugBranchCoverageType = array{
39+
* op_start: int,
40+
* op_end: int,
41+
* line_start: int,
42+
* line_end: int,
43+
* hit: int,
44+
* out: array<int, int>,
45+
* out_hit: array<int, int>,
46+
* }
47+
* @psalm-type XdebugPathCoverageType = array{
48+
* path: array<int, int>,
49+
* hit: int,
50+
* }
51+
* @psalm-type XdebugFunctionCoverageType = array{
52+
* branches: array<int, XdebugBranchCoverageType>,
53+
* paths: array<int, XdebugPathCoverageType>,
54+
* }
55+
* @psalm-type XdebugFunctionsCoverageType = array<string, XdebugFunctionCoverageType>
56+
* @psalm-type XdebugPathAndBranchesCoverageType = array{
57+
* lines: XdebugLinesCoverageType,
58+
* functions: XdebugFunctionsCoverageType,
59+
* }
60+
* @psalm-type XdebugCodeCoverageWithoutPathCoverageType = array<string, XdebugLinesCoverageType>
61+
* @psalm-type XdebugCodeCoverageWithPathCoverageType = array<string, XdebugPathAndBranchesCoverageType>
3462
*/
3563
final class XdebugDriver extends Driver
3664
{
@@ -84,9 +112,11 @@ public function stop(): RawCodeCoverageData
84112
xdebug_stop_code_coverage();
85113

86114
if ($this->collectsBranchAndPathCoverage()) {
115+
/* @var XdebugCodeCoverageWithPathCoverageType $data */
87116
return RawCodeCoverageData::fromXdebugWithPathCoverage($data);
88117
}
89118

119+
/* @var XdebugCodeCoverageWithoutPathCoverageType $data */
90120
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
91121
}
92122

‎src/Node/AbstractNode.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
/**
2121
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
22+
*
23+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
24+
* @psalm-import-type ProcessedFunctionType from \SebastianBergmann\CodeCoverage\Node\File
25+
* @psalm-import-type ProcessedClassType from \SebastianBergmann\CodeCoverage\Node\File
26+
* @psalm-import-type ProcessedTraitType from \SebastianBergmann\CodeCoverage\Node\File
2227
*/
2328
abstract class AbstractNode implements Countable
2429
{
@@ -163,14 +168,23 @@ public function numberOfTestedFunctionsAndMethods(): int
163168
return $this->numberOfTestedFunctions() + $this->numberOfTestedMethods();
164169
}
165170

171+
/**
172+
* @psalm-return array<string, ProcessedClassType>
173+
*/
166174
abstract public function classes(): array;
167175

176+
/**
177+
* @psalm-return array<string, ProcessedTraitType>
178+
*/
168179
abstract public function traits(): array;
169180

181+
/**
182+
* @psalm-return array<string, ProcessedFunctionType>
183+
*/
170184
abstract public function functions(): array;
171185

172186
/**
173-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
187+
* @psalm-return LinesOfCodeType
174188
*/
175189
abstract public function linesOfCode(): array;
176190

‎src/Node/Builder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
/**
2929
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
30+
*
31+
* @psalm-import-type TestType from \SebastianBergmann\CodeCoverage\CodeCoverage
3032
*/
3133
final class Builder
3234
{
@@ -56,7 +58,7 @@ public function build(CodeCoverage $coverage): Directory
5658
}
5759

5860
/**
59-
* @psalm-param array<string, array{size: string, status: string}> $tests
61+
* @psalm-param array<string, TestType> $tests
6062
*/
6163
private function addItems(Directory $root, array $items, array $tests): void
6264
{
@@ -129,6 +131,8 @@ private function addItems(Directory $root, array $items, array $tests): void
129131
* )
130132
* )
131133
* </code>
134+
*
135+
* @psalm-return array<string, array<string, array{lineCoverage: array<int, int>, functionCoverage: array<string, array<int, int>>}>>
132136
*/
133137
private function buildDirectoryStructure(ProcessedCodeCoverageData $data): array
134138
{

‎src/Node/Directory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
/**
1818
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
19+
*
20+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
1921
*/
2022
final class Directory extends AbstractNode implements IteratorAggregate
2123
{
@@ -38,7 +40,7 @@ final class Directory extends AbstractNode implements IteratorAggregate
3840
private ?array $functions = null;
3941

4042
/**
41-
* @psalm-var null|array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
43+
* @psalm-var null|LinesOfCodeType
4244
*/
4345
private ?array $linesOfCode = null;
4446
private int $numFiles = -1;
@@ -161,7 +163,7 @@ public function functions(): array
161163
}
162164

163165
/**
164-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
166+
* @psalm-return LinesOfCodeType
165167
*/
166168
public function linesOfCode(): array
167169
{

‎src/Node/File.php

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,80 @@
1515

1616
/**
1717
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
18+
*
19+
* @psalm-import-type CodeUnitFunctionType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
20+
* @psalm-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
21+
* @psalm-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
22+
* @psalm-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
23+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
24+
* @psalm-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
25+
*
26+
* @psalm-type ProcessedFunctionType = array{
27+
* functionName: string,
28+
* namespace: string,
29+
* signature: string,
30+
* startLine: int,
31+
* endLine: int,
32+
* executableLines: int,
33+
* executedLines: int,
34+
* executableBranches: int,
35+
* executedBranches: int,
36+
* executablePaths: int,
37+
* executedPaths: int,
38+
* ccn: int,
39+
* coverage: int|float,
40+
* crap: int|string,
41+
* link: string
42+
* }
43+
* @psalm-type ProcessedMethodType = array{
44+
* methodName: string,
45+
* visibility: string,
46+
* signature: string,
47+
* startLine: int,
48+
* endLine: int,
49+
* executableLines: int,
50+
* executedLines: int,
51+
* executableBranches: int,
52+
* executedBranches: int,
53+
* executablePaths: int,
54+
* executedPaths: int,
55+
* ccn: int,
56+
* coverage: float|int,
57+
* crap: int|string,
58+
* link: string
59+
* }
60+
* @psalm-type ProcessedClassType = array{
61+
* className: string,
62+
* namespace: string,
63+
* methods: array<string, ProcessedMethodType>,
64+
* startLine: int,
65+
* executableLines: int,
66+
* executedLines: int,
67+
* executableBranches: int,
68+
* executedBranches: int,
69+
* executablePaths: int,
70+
* executedPaths: int,
71+
* ccn: int,
72+
* coverage: int|float,
73+
* crap: int|string,
74+
* link: string
75+
* }
76+
* @psalm-type ProcessedTraitType = array{
77+
* traitName: string,
78+
* namespace: string,
79+
* methods: array<string, ProcessedMethodType>,
80+
* startLine: int,
81+
* executableLines: int,
82+
* executedLines: int,
83+
* executableBranches: int,
84+
* executedBranches: int,
85+
* executablePaths: int,
86+
* executedPaths: int,
87+
* ccn: int,
88+
* coverage: float|int,
89+
* crap: int|string,
90+
* link: string
91+
* }
1892
*/
1993
final class File extends AbstractNode
2094
{
@@ -27,12 +101,24 @@ final class File extends AbstractNode
27101
private int $numExecutedBranches = 0;
28102
private int $numExecutablePaths = 0;
29103
private int $numExecutedPaths = 0;
30-
private array $classes = [];
31-
private array $traits = [];
32-
private array $functions = [];
33104

34105
/**
35-
* @psalm-var array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
106+
* @psalm-var array<string, ProcessedClassType>
107+
*/
108+
private array $classes = [];
109+
110+
/**
111+
* @psalm-var array<string, ProcessedTraitType>
112+
*/
113+
private array $traits = [];
114+
115+
/**
116+
* @psalm-var array<string, ProcessedFunctionType>
117+
*/
118+
private array $functions = [];
119+
120+
/**
121+
* @psalm-var LinesOfCodeType
36122
*/
37123
private readonly array $linesOfCode;
38124
private ?int $numClasses = null;
@@ -42,10 +128,17 @@ final class File extends AbstractNode
42128
private ?int $numMethods = null;
43129
private ?int $numTestedMethods = null;
44130
private ?int $numTestedFunctions = null;
45-
private array $codeUnitsByLine = [];
46131

47132
/**
48-
* @psalm-param array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int} $linesOfCode
133+
* @var array<int, array|array{0: CodeUnitClassType, 1: string}|array{0: CodeUnitFunctionType}|array{0: CodeUnitTraitType, 1: string}>
134+
*/
135+
private array $codeUnitsByLine = [];
136+
137+
/**
138+
* @psalm-param LinesOfCodeType $linesOfCode
139+
* @psalm-param array<string, CodeUnitClassType> $classes
140+
* @psalm-param array<string, CodeUnitTraitType> $traits
141+
* @psalm-param array<string, CodeUnitFunctionType> $functions
49142
*/
50143
public function __construct(string $name, AbstractNode $parent, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, array $linesOfCode)
51144
{
@@ -94,9 +187,6 @@ public function functions(): array
94187
return $this->functions;
95188
}
96189

97-
/**
98-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
99-
*/
100190
public function linesOfCode(): array
101191
{
102192
return $this->linesOfCode;
@@ -253,6 +343,11 @@ public function numberOfTestedFunctions(): int
253343
return $this->numTestedFunctions;
254344
}
255345

346+
/**
347+
* @psalm-param array<string, CodeUnitClassType> $classes
348+
* @psalm-param array<string, CodeUnitTraitType> $traits
349+
* @psalm-param array<string, CodeUnitFunctionType> $functions
350+
*/
256351
private function calculateStatistics(array $classes, array $traits, array $functions): void
257352
{
258353
foreach (range(1, $this->linesOfCode['linesOfCode']) as $lineNumber) {
@@ -355,6 +450,9 @@ private function calculateStatistics(array $classes, array $traits, array $funct
355450
}
356451
}
357452

453+
/**
454+
* @psalm-param array<string, CodeUnitClassType> $classes
455+
*/
358456
private function processClasses(array $classes): void
359457
{
360458
$link = $this->id() . '.html#';
@@ -401,6 +499,9 @@ private function processClasses(array $classes): void
401499
}
402500
}
403501

502+
/**
503+
* @psalm-param array<string, CodeUnitTraitType> $traits
504+
*/
404505
private function processTraits(array $traits): void
405506
{
406507
$link = $this->id() . '.html#';
@@ -447,6 +548,9 @@ private function processTraits(array $traits): void
447548
}
448549
}
449550

551+
/**
552+
* @psalm-param array<string, CodeUnitFunctionType> $functions
553+
*/
450554
private function processFunctions(array $functions): void
451555
{
452556
$link = $this->id() . '.html#';
@@ -513,6 +617,11 @@ static function (array $path)
513617
}
514618
}
515619

620+
/**
621+
* @psalm-param CodeUnitMethodType $method
622+
*
623+
* @psalm-return ProcessedMethodType
624+
*/
516625
private function newMethod(string $className, string $methodName, array $method, string $link): array
517626
{
518627
$methodData = [

‎src/Report/Xml/Tests.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
16+
*
17+
* @psalm-import-type TestType from \SebastianBergmann\CodeCoverage\CodeCoverage
1618
*/
1719
final class Tests
1820
{
@@ -23,6 +25,9 @@ public function __construct(DOMElement $context)
2325
$this->contextNode = $context;
2426
}
2527

28+
/**
29+
* @param TestType $result
30+
*/
2631
public function addTest(string $test, array $result): void
2732
{
2833
$node = $this->contextNode->appendChild(

‎src/StaticAnalysis/CachingFileAnalyser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
24+
*
25+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2426
*/
2527
final class CachingFileAnalyser implements FileAnalyser
2628
{
@@ -65,7 +67,7 @@ public function functionsIn(string $filename): array
6567
}
6668

6769
/**
68-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
70+
* @psalm-return LinesOfCodeType
6971
*/
7072
public function linesOfCodeFor(string $filename): array
7173
{

‎src/StaticAnalysis/CodeUnitFindingVisitor.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,55 @@
3232

3333
/**
3434
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
35+
*
36+
* @psalm-type CodeUnitFunctionType = array{
37+
* name: string,
38+
* namespacedName: string,
39+
* namespace: string,
40+
* signature: string,
41+
* startLine: int,
42+
* endLine: int,
43+
* ccn: int
44+
* }
45+
* @psalm-type CodeUnitMethodType = array{
46+
* methodName: string,
47+
* signature: string,
48+
* visibility: string,
49+
* startLine: int,
50+
* endLine: int,
51+
* ccn: int
52+
* }
53+
* @psalm-type CodeUnitClassType = array{
54+
* name: string,
55+
* namespacedName: string,
56+
* namespace: string,
57+
* startLine: int,
58+
* endLine: int,
59+
* methods: array<string, CodeUnitMethodType>
60+
* }
61+
* @psalm-type CodeUnitTraitType = array{
62+
* name: string,
63+
* namespacedName: string,
64+
* namespace: string,
65+
* startLine: int,
66+
* endLine: int,
67+
* methods: array<string, CodeUnitMethodType>
68+
* }
3569
*/
3670
final class CodeUnitFindingVisitor extends NodeVisitorAbstract
3771
{
3872
/**
39-
* @psalm-var array<string,array{name: string, namespacedName: string, namespace: string, startLine: int, endLine: int, methods: array<string,array{methodName: string, signature: string, visibility: string, startLine: int, endLine: int, ccn: int}>}>
73+
* @psalm-var array<string, CodeUnitClassType>
4074
*/
4175
private array $classes = [];
4276

4377
/**
44-
* @psalm-var array<string,array{name: string, namespacedName: string, namespace: string, startLine: int, endLine: int, methods: array<string,array{methodName: string, signature: string, visibility: string, startLine: int, endLine: int, ccn: int}>}>
78+
* @psalm-var array<string, CodeUnitTraitType>
4579
*/
4680
private array $traits = [];
4781

4882
/**
49-
* @psalm-var array<string,array{name: string, namespacedName: string, namespace: string, signature: string, startLine: int, endLine: int, ccn: int}>
83+
* @psalm-var array<string, CodeUnitFunctionType>
5084
*/
5185
private array $functions = [];
5286

@@ -84,23 +118,23 @@ public function enterNode(Node $node): void
84118
}
85119

86120
/**
87-
* @psalm-return array<string,array{name: string, namespacedName: string, namespace: string, startLine: int, endLine: int, methods: array<string,array{methodName: string, signature: string, visibility: string, startLine: int, endLine: int, ccn: int}>}>
121+
* @psalm-return array<string, CodeUnitClassType>
88122
*/
89123
public function classes(): array
90124
{
91125
return $this->classes;
92126
}
93127

94128
/**
95-
* @psalm-return array<string,array{name: string, namespacedName: string, namespace: string, startLine: int, endLine: int, methods: array<string,array{methodName: string, signature: string, visibility: string, startLine: int, endLine: int, ccn: int}>}>
129+
* @psalm-return array<string, CodeUnitTraitType>
96130
*/
97131
public function traits(): array
98132
{
99133
return $this->traits;
100134
}
101135

102136
/**
103-
* @psalm-return array<string,array{name: string, namespacedName: string, namespace: string, signature: string, startLine: int, endLine: int, ccn: int}>
137+
* @psalm-return array<string, CodeUnitFunctionType>
104138
*/
105139
public function functions(): array
106140
{

‎src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626

2727
/**
2828
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
29+
*
30+
* @psalm-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
2931
*/
3032
final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
3133
{
3234
private int $nextBranch = 0;
3335
private readonly string $source;
3436

3537
/**
36-
* @psalm-var array<int, int>
38+
* @psalm-var LinesType
3739
*/
3840
private array $executableLinesGroupedByBranch = [];
3941

@@ -352,6 +354,9 @@ public function afterTraverse(array $nodes): void
352354
);
353355
}
354356

357+
/**
358+
* @psalm-return LinesType
359+
*/
355360
public function executableLinesGroupedByBranch(): array
356361
{
357362
return $this->executableLinesGroupedByBranch;

‎src/StaticAnalysis/FileAnalyser.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,50 @@
1111

1212
/**
1313
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
14+
*
15+
* @psalm-import-type CodeUnitFunctionType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
16+
* @psalm-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
17+
* @psalm-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
18+
* @psalm-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
19+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
20+
* @psalm-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
21+
*
22+
* @psalm-type LinesOfCodeType = array{
23+
* linesOfCode: int,
24+
* commentLinesOfCode: int,
25+
* nonCommentLinesOfCode: int
26+
* }
27+
* @psalm-type LinesType = array<int, int>
1428
*/
1529
interface FileAnalyser
1630
{
31+
/**
32+
* @psalm-return array<string, CodeUnitClassType>
33+
*/
1734
public function classesIn(string $filename): array;
1835

36+
/**
37+
* @psalm-return array<string, CodeUnitTraitType>
38+
*/
1939
public function traitsIn(string $filename): array;
2040

41+
/**
42+
* @psalm-return array<string, CodeUnitFunctionType>
43+
*/
2144
public function functionsIn(string $filename): array;
2245

2346
/**
24-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
47+
* @psalm-return LinesOfCodeType
2548
*/
2649
public function linesOfCodeFor(string $filename): array;
2750

51+
/**
52+
* @psalm-return LinesType
53+
*/
2854
public function executableLinesIn(string $filename): array;
2955

56+
/**
57+
* @psalm-return LinesType
58+
*/
3059
public function ignoredLinesFor(string $filename): array;
3160
}

‎src/StaticAnalysis/ParsingFileAnalyser.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,44 @@
3232

3333
/**
3434
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
35+
*
36+
* @psalm-import-type CodeUnitFunctionType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
37+
* @psalm-import-type CodeUnitMethodType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
38+
* @psalm-import-type CodeUnitClassType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
39+
* @psalm-import-type CodeUnitTraitType from \SebastianBergmann\CodeCoverage\StaticAnalysis\CodeUnitFindingVisitor
40+
* @psalm-import-type LinesOfCodeType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
41+
* @psalm-import-type LinesType from \SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser
3542
*/
3643
final class ParsingFileAnalyser implements FileAnalyser
3744
{
38-
private array $classes = [];
39-
private array $traits = [];
45+
/**
46+
* @psalm-var array<string, array<string, CodeUnitClassType>>
47+
*/
48+
private array $classes = [];
49+
50+
/**
51+
* @psalm-var array<string, array<string, CodeUnitTraitType>>
52+
*/
53+
private array $traits = [];
54+
55+
/**
56+
* @psalm-var array<string, array<string, CodeUnitFunctionType>>
57+
*/
4058
private array $functions = [];
4159

4260
/**
43-
* @var array<string,array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}>
61+
* @var array<string, LinesOfCodeType>
62+
*/
63+
private array $linesOfCode = [];
64+
65+
/**
66+
* @var array<string, LinesType>
67+
*/
68+
private array $ignoredLines = [];
69+
70+
/**
71+
* @var array<string, LinesType>
4472
*/
45-
private array $linesOfCode = [];
46-
private array $ignoredLines = [];
4773
private array $executableLines = [];
4874
private readonly bool $useAnnotationsForIgnoringCode;
4975
private readonly bool $ignoreDeprecatedCode;
@@ -75,9 +101,6 @@ public function functionsIn(string $filename): array
75101
return $this->functions[$filename];
76102
}
77103

78-
/**
79-
* @psalm-return array{linesOfCode: int, commentLinesOfCode: int, nonCommentLinesOfCode: int}
80-
*/
81104
public function linesOfCodeFor(string $filename): array
82105
{
83106
$this->analyse($filename);

0 commit comments

Comments
 (0)
Please sign in to comment.