2525 * @phpstan-import-type XdebugFunctionCoverageType from XdebugDriver
2626 *
2727 * @phpstan-type TestIdType string
28- * @phpstan-type FunctionCoverageDataType array{
29- * branches: array<int, array{
30- * op_start: int,
31- * op_end: int,
32- * line_start: int,
33- * line_end: int,
34- * hit: list<TestIdType>,
35- * out: array<int, int>,
36- * out_hit: array<int, int>,
37- * }>,
38- * paths: array<int, array{
39- * path: array<int, int>,
40- * hit: list<TestIdType>,
41- * }>
42- * }
28+ * @phpstan-type FunctionCoverageDataType ProcessedFunctionCoverageData
4329 * @phpstan-type FunctionCoverageType array<string, array<string, FunctionCoverageDataType>>
4430 * @phpstan-type LineCoverageType array<string, array<int, null|list<TestIdType>>>
4531 */
@@ -99,13 +85,13 @@ public function markCodeAsExecutedByTestCase(string $testCaseId, RawCodeCoverage
9985 foreach ($ functions as $ functionName => $ functionData ) {
10086 foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
10187 if ($ branchData ['hit ' ] === Driver::BRANCH_HIT ) {
102- $ this ->functionCoverage [$ file ][$ functionName ][ ' branches ' ][ $ branchId][ ' hit ' ][] = $ testCaseId ;
88+ $ this ->functionCoverage [$ file ][$ functionName ]-> recordBranchHit ( $ branchId, $ testCaseId) ;
10389 }
10490 }
10591
10692 foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
10793 if ($ pathData ['hit ' ] === Driver::BRANCH_HIT ) {
108- $ this ->functionCoverage [$ file ][$ functionName ][ ' paths ' ][ $ pathId][ ' hit ' ][] = $ testCaseId ;
94+ $ this ->functionCoverage [$ file ][$ functionName ]-> recordPathHit ( $ pathId, $ testCaseId) ;
10995 }
11096 }
11197 }
@@ -213,14 +199,6 @@ public function merge(self $newData): void
213199 } else {
214200 $ this ->initPreviouslyUnseenFunction ($ file , $ functionName , $ functionData );
215201 }
216-
217- foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
218- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = array_unique (array_merge ($ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ], $ branchData ['hit ' ]));
219- }
220-
221- foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
222- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = array_unique (array_merge ($ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ], $ pathData ['hit ' ]));
223- }
224202 }
225203 }
226204 }
@@ -259,17 +237,13 @@ private function priorityForLine(array $data, int $line): int
259237 *
260238 * @param FunctionCoverageDataType|XdebugFunctionCoverageType $functionData
261239 */
262- private function initPreviouslyUnseenFunction (string $ file , string $ functionName , array $ functionData ): void
240+ private function initPreviouslyUnseenFunction (string $ file , string $ functionName , ProcessedFunctionCoverageData | array $ functionData ): void
263241 {
264- $ this ->functionCoverage [$ file ][$ functionName ] = $ functionData ;
265-
266- foreach (array_keys ($ functionData ['branches ' ]) as $ branchId ) {
267- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = [];
242+ if (is_array ($ functionData )) {
243+ $ functionData = ProcessedFunctionCoverageData::fromXdebugCoverage ($ functionData );
268244 }
269245
270- foreach (array_keys ($ functionData ['paths ' ]) as $ pathId ) {
271- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = [];
272- }
246+ $ this ->functionCoverage [$ file ][$ functionName ] = $ functionData ;
273247 }
274248
275249 /**
@@ -279,20 +253,14 @@ private function initPreviouslyUnseenFunction(string $file, string $functionName
279253 *
280254 * @param FunctionCoverageDataType|XdebugFunctionCoverageType $functionData
281255 */
282- private function initPreviouslySeenFunction (string $ file , string $ functionName , array $ functionData ): void
256+ private function initPreviouslySeenFunction (string $ file , string $ functionName , ProcessedFunctionCoverageData | array $ functionData ): void
283257 {
284- foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
285- if (!isset ($ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ])) {
286- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ] = $ branchData ;
287- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = [];
288- }
258+ if (is_array ($ functionData )) {
259+ $ functionData = ProcessedFunctionCoverageData::fromXdebugCoverage ($ functionData );
289260 }
290261
291- foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
292- if (!isset ($ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ])) {
293- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ] = $ pathData ;
294- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = [];
295- }
296- }
262+ $ this ->functionCoverage [$ file ][$ functionName ] = $ this ->functionCoverage [$ file ][$ functionName ]->merge (
263+ $ functionData
264+ );
297265 }
298266}
0 commit comments