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 */
4531final class ProcessedCodeCoverageData
@@ -98,13 +84,13 @@ public function markCodeAsExecutedByTestCase(string $testCaseId, RawCodeCoverage
9884 foreach ($ functions as $ functionName => $ functionData ) {
9985 foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
10086 if ($ branchData ['hit ' ] === Driver::BRANCH_HIT ) {
101- $ this ->functionCoverage [$ file ][$ functionName ][ ' branches ' ][ $ branchId][ ' hit ' ][] = $ testCaseId ;
87+ $ this ->functionCoverage [$ file ][$ functionName ]-> recordBranchHit ( $ branchId, $ testCaseId) ;
10288 }
10389 }
10490
10591 foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
10692 if ($ pathData ['hit ' ] === Driver::BRANCH_HIT ) {
107- $ this ->functionCoverage [$ file ][$ functionName ][ ' paths ' ][ $ pathId][ ' hit ' ][] = $ testCaseId ;
93+ $ this ->functionCoverage [$ file ][$ functionName ]-> recordPathHit ( $ pathId, $ testCaseId) ;
10894 }
10995 }
11096 }
@@ -197,14 +183,6 @@ public function merge(self $newData): void
197183 } else {
198184 $ this ->initPreviouslyUnseenFunction ($ file , $ functionName , $ functionData );
199185 }
200-
201- foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
202- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = array_unique (array_merge ($ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ], $ branchData ['hit ' ]));
203- }
204-
205- foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
206- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = array_unique (array_merge ($ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ], $ pathData ['hit ' ]));
207- }
208186 }
209187 }
210188 }
@@ -243,17 +221,13 @@ private function priorityForLine(array $data, int $line): int
243221 *
244222 * @param FunctionCoverageDataType|XdebugFunctionCoverageType $functionData
245223 */
246- private function initPreviouslyUnseenFunction (string $ file , string $ functionName , array $ functionData ): void
224+ private function initPreviouslyUnseenFunction (string $ file , string $ functionName , ProcessedFunctionCoverageData | array $ functionData ): void
247225 {
248- $ this ->functionCoverage [$ file ][$ functionName ] = $ functionData ;
249-
250- foreach (array_keys ($ functionData ['branches ' ]) as $ branchId ) {
251- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = [];
226+ if (is_array ($ functionData )) {
227+ $ functionData = ProcessedFunctionCoverageData::fromXdebugCoverage ($ functionData );
252228 }
253229
254- foreach (array_keys ($ functionData ['paths ' ]) as $ pathId ) {
255- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = [];
256- }
230+ $ this ->functionCoverage [$ file ][$ functionName ] = $ functionData ;
257231 }
258232
259233 /**
@@ -263,20 +237,14 @@ private function initPreviouslyUnseenFunction(string $file, string $functionName
263237 *
264238 * @param FunctionCoverageDataType|XdebugFunctionCoverageType $functionData
265239 */
266- private function initPreviouslySeenFunction (string $ file , string $ functionName , array $ functionData ): void
240+ private function initPreviouslySeenFunction (string $ file , string $ functionName , ProcessedFunctionCoverageData | array $ functionData ): void
267241 {
268- foreach ($ functionData ['branches ' ] as $ branchId => $ branchData ) {
269- if (!isset ($ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ])) {
270- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ] = $ branchData ;
271- $ this ->functionCoverage [$ file ][$ functionName ]['branches ' ][$ branchId ]['hit ' ] = [];
272- }
242+ if (is_array ($ functionData )) {
243+ $ functionData = ProcessedFunctionCoverageData::fromXdebugCoverage ($ functionData );
273244 }
274245
275- foreach ($ functionData ['paths ' ] as $ pathId => $ pathData ) {
276- if (!isset ($ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ])) {
277- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ] = $ pathData ;
278- $ this ->functionCoverage [$ file ][$ functionName ]['paths ' ][$ pathId ]['hit ' ] = [];
279- }
280- }
246+ $ this ->functionCoverage [$ file ][$ functionName ] = $ this ->functionCoverage [$ file ][$ functionName ]->merge (
247+ $ functionData
248+ );
281249 }
282250}
0 commit comments