@@ -89,9 +89,9 @@ final class CodeCoverage
89
89
/**
90
90
* Code coverage data.
91
91
*
92
- * @var array
92
+ * @var ProcessedCodeCoverageData
93
93
*/
94
- private $ data = [] ;
94
+ private $ data ;
95
95
96
96
/**
97
97
* @var array
@@ -151,6 +151,7 @@ public function __construct(Driver $driver = null, Filter $filter = null)
151
151
$ this ->filter = $ filter ;
152
152
153
153
$ this ->wizard = new Wizard ;
154
+ $ this ->data = new ProcessedCodeCoverageData ();
154
155
}
155
156
156
157
/**
@@ -172,7 +173,7 @@ public function clear(): void
172
173
{
173
174
$ this ->isInitialized = false ;
174
175
$ this ->currentId = null ;
175
- $ this ->data = [] ;
176
+ $ this ->data = new ProcessedCodeCoverageData () ;
176
177
$ this ->tests = [];
177
178
$ this ->report = null ;
178
179
}
@@ -188,7 +189,7 @@ public function filter(): Filter
188
189
/**
189
190
* Returns the collected code coverage data.
190
191
*/
191
- public function getData (bool $ raw = false ): array
192
+ public function getData (bool $ raw = false ): ProcessedCodeCoverageData
192
193
{
193
194
if (!$ raw && $ this ->addUncoveredFilesFromWhitelist ) {
194
195
$ this ->addUncoveredFilesFromWhitelist ();
@@ -200,7 +201,7 @@ public function getData(bool $raw = false): array
200
201
/**
201
202
* Sets the coverage data.
202
203
*/
203
- public function setData (array $ data ): void
204
+ public function setData (ProcessedCodeCoverageData $ data ): void
204
205
{
205
206
$ this ->data = $ data ;
206
207
$ this ->report = null ;
@@ -297,7 +298,7 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append =
297
298
298
299
$ this ->applyWhitelistFilter ($ rawData );
299
300
$ this ->applyIgnoredLinesFilter ($ rawData );
300
- $ this ->initializeFilesThatAreSeenTheFirstTime ($ rawData );
301
+ $ this ->data -> initializeFilesThatAreSeenTheFirstTime ($ rawData );
301
302
302
303
if (!$ append ) {
303
304
return ;
@@ -339,19 +340,7 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append =
339
340
340
341
$ this ->tests [$ id ] = ['size ' => $ size , 'status ' => $ status ];
341
342
342
- foreach ($ rawData ->getLineCoverage () as $ file => $ lines ) {
343
- if (!$ this ->filter ->isFile ($ file )) {
344
- continue ;
345
- }
346
-
347
- foreach ($ lines as $ k => $ v ) {
348
- if ($ v === Driver::LINE_EXECUTED ) {
349
- if (empty ($ this ->data [$ file ][$ k ]) || !\in_array ($ id , $ this ->data [$ file ][$ k ])) {
350
- $ this ->data [$ file ][$ k ][] = $ id ;
351
- }
352
- }
353
- }
354
- }
343
+ $ this ->data ->markCodeAsExecutedByTestCase ($ id , $ rawData );
355
344
356
345
$ this ->report = null ;
357
346
}
@@ -367,36 +356,7 @@ public function merge(self $that): void
367
356
\array_merge ($ this ->filter ->getWhitelistedFiles (), $ that ->filter ()->getWhitelistedFiles ())
368
357
);
369
358
370
- foreach ($ that ->data as $ file => $ lines ) {
371
- if (!isset ($ this ->data [$ file ])) {
372
- if (!$ this ->filter ->isFiltered ($ file )) {
373
- $ this ->data [$ file ] = $ lines ;
374
- }
375
-
376
- continue ;
377
- }
378
-
379
- // we should compare the lines if any of two contains data
380
- $ compareLineNumbers = \array_unique (
381
- \array_merge (
382
- \array_keys ($ this ->data [$ file ]),
383
- \array_keys ($ that ->data [$ file ])
384
- )
385
- );
386
-
387
- foreach ($ compareLineNumbers as $ line ) {
388
- $ thatPriority = $ this ->getLinePriority ($ that ->data [$ file ], $ line );
389
- $ thisPriority = $ this ->getLinePriority ($ this ->data [$ file ], $ line );
390
-
391
- if ($ thatPriority > $ thisPriority ) {
392
- $ this ->data [$ file ][$ line ] = $ that ->data [$ file ][$ line ];
393
- } elseif ($ thatPriority === $ thisPriority && \is_array ($ this ->data [$ file ][$ line ])) {
394
- $ this ->data [$ file ][$ line ] = \array_unique (
395
- \array_merge ($ this ->data [$ file ][$ line ], $ that ->data [$ file ][$ line ])
396
- );
397
- }
398
- }
399
- }
359
+ $ this ->data ->merge ($ that ->data );
400
360
401
361
$ this ->tests = \array_merge ($ this ->tests , $ that ->getTests ());
402
362
$ this ->report = null ;
@@ -457,38 +417,6 @@ public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist):
457
417
$ this ->unintentionallyCoveredSubclassesWhitelist = $ whitelist ;
458
418
}
459
419
460
- /**
461
- * Determine the priority for a line
462
- *
463
- * 1 = the line is not set
464
- * 2 = the line has not been tested
465
- * 3 = the line is dead code
466
- * 4 = the line has been tested
467
- *
468
- * During a merge, a higher number is better.
469
- *
470
- * @param array $data
471
- * @param int $line
472
- *
473
- * @return int
474
- */
475
- private function getLinePriority ($ data , $ line )
476
- {
477
- if (!\array_key_exists ($ line , $ data )) {
478
- return 1 ;
479
- }
480
-
481
- if (\is_array ($ data [$ line ]) && \count ($ data [$ line ]) === 0 ) {
482
- return 2 ;
483
- }
484
-
485
- if ($ data [$ line ] === null ) {
486
- return 3 ;
487
- }
488
-
489
- return 4 ;
490
- }
491
-
492
420
/**
493
421
* Applies the @covers annotation filtering.
494
422
*
@@ -559,19 +487,6 @@ private function applyIgnoredLinesFilter(RawCodeCoverageData $data): void
559
487
}
560
488
}
561
489
562
- private function initializeFilesThatAreSeenTheFirstTime (RawCodeCoverageData $ data ): void
563
- {
564
- foreach ($ data ->getLineCoverage () as $ file => $ lines ) {
565
- if (!isset ($ this ->data [$ file ]) && $ this ->filter ->isFile ($ file )) {
566
- $ this ->data [$ file ] = [];
567
-
568
- foreach ($ lines as $ k => $ v ) {
569
- $ this ->data [$ file ][$ k ] = $ v === -2 ? null : [];
570
- }
571
- }
572
- }
573
- }
574
-
575
490
/**
576
491
* @throws CoveredCodeNotExecutedException
577
492
* @throws InvalidArgumentException
@@ -585,7 +500,7 @@ private function addUncoveredFilesFromWhitelist(): void
585
500
$ data = [];
586
501
$ uncoveredFiles = \array_diff (
587
502
$ this ->filter ->getWhitelist (),
588
- \array_keys ($ this ->data )
503
+ \array_keys ($ this ->data -> getLineCoverage () )
589
504
);
590
505
591
506
foreach ($ uncoveredFiles as $ uncoveredFile ) {
0 commit comments