@@ -52,6 +52,11 @@ class PHP_CodeCoverage
52
52
*/
53
53
private $ addUncoveredFilesFromWhitelist = true ;
54
54
55
+ /**
56
+ * @var bool
57
+ */
58
+ private $ processUncoveredFilesFromWhitelist = false ;
59
+
55
60
/**
56
61
* @var mixed
57
62
*/
@@ -81,13 +86,6 @@ class PHP_CodeCoverage
81
86
*/
82
87
private $ tests = [];
83
88
84
- /**
85
- * Store all uncovered files
86
- *
87
- * @var array
88
- */
89
- private $ uncoveredFiles = [];
90
-
91
89
/**
92
90
* Constructor.
93
91
*
@@ -107,8 +105,6 @@ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCove
107
105
108
106
$ this ->driver = $ driver ;
109
107
$ this ->filter = $ filter ;
110
-
111
- $ this ->initData ();
112
108
}
113
109
114
110
/**
@@ -155,9 +151,7 @@ public function filter()
155
151
*/
156
152
public function getData ($ raw = false )
157
153
{
158
- if (!$ raw && $ this ->addUncoveredFilesFromWhitelist ) {
159
- $ this ->addUncoveredFilesFromWhitelist ();
160
- }
154
+ $ this ->processUncoveredLines ($ raw );
161
155
162
156
return $ this ->data ;
163
157
}
@@ -248,12 +242,6 @@ public function stop($append = true, $linesToBeCovered = [], array $linesToBeUse
248
242
$ data = $ this ->driver ->stop ();
249
243
$ this ->append ($ data , null , $ append , $ linesToBeCovered , $ linesToBeUsed );
250
244
251
- foreach (array_keys ($ data ) as $ file ) {
252
- if (isset ($ this ->uncoveredFiles [$ file ])) {
253
- unset($ this ->uncoveredFiles [$ file ]);
254
- }
255
- }
256
-
257
245
$ this ->currentId = null ;
258
246
259
247
return $ data ;
@@ -280,7 +268,6 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
280
268
}
281
269
282
270
$ this ->applyListsFilter ($ data );
283
- $ this ->applyIgnoredLinesFilter ($ data );
284
271
$ this ->initializeFilesThatAreSeenTheFirstTime ($ data );
285
272
286
273
if (!$ append ) {
@@ -464,6 +451,22 @@ public function setAddUncoveredFilesFromWhitelist($flag)
464
451
$ this ->addUncoveredFilesFromWhitelist = $ flag ;
465
452
}
466
453
454
+ /**
455
+ * @param bool $flag
456
+ * @throws PHP_CodeCoverage_InvalidArgumentException
457
+ */
458
+ public function setProcessUncoveredFilesFromWhitelist ($ flag )
459
+ {
460
+ if (!is_bool ($ flag )) {
461
+ throw PHP_CodeCoverage_InvalidArgumentException::create (
462
+ 1 ,
463
+ 'boolean '
464
+ );
465
+ }
466
+
467
+ $ this ->processUncoveredFilesFromWhitelist = $ flag ;
468
+ }
469
+
467
470
/**
468
471
* @param bool $flag
469
472
* @throws PHP_CodeCoverage_InvalidArgumentException
@@ -535,24 +538,6 @@ private function applyListsFilter(array &$data)
535
538
}
536
539
}
537
540
538
- /**
539
- * Applies the "ignored lines" filtering.
540
- *
541
- * @param array $data
542
- */
543
- private function applyIgnoredLinesFilter (array &$ data )
544
- {
545
- foreach (array_keys ($ data ) as $ filename ) {
546
- if (!$ this ->filter ->isFile ($ filename )) {
547
- continue ;
548
- }
549
-
550
- foreach ($ this ->getLinesToBeIgnored ($ filename ) as $ line ) {
551
- unset($ data [$ filename ][$ line ]);
552
- }
553
- }
554
- }
555
-
556
541
/**
557
542
* @param array $data
558
543
* @since Method available since Release 1.1.0
@@ -572,20 +557,6 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
572
557
}
573
558
}
574
559
575
- /**
576
- * Processes whitelisted files that are not covered.
577
- */
578
- private function addUncoveredFilesFromWhitelist ()
579
- {
580
- $ data = [];
581
-
582
- foreach (array_keys ($ this ->uncoveredFiles ) as $ file ) {
583
- $ data [$ file ] = $ this ->data [$ file ];
584
- }
585
-
586
- $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
587
- }
588
-
589
560
/**
590
561
* Returns the lines of a source file that should be ignored.
591
562
*
@@ -860,20 +831,60 @@ private function selectDriver()
860
831
}
861
832
862
833
/**
863
- * Initialise the data with the executable/dead lines from each file in the whitelist
834
+ * Determine uncovered lines
864
835
*/
865
- private function initData ( )
836
+ private function processUncoveredLines ( $ raw = false )
866
837
{
867
- foreach ($ this ->filter ()->getWhitelist () as $ file ) {
868
- $ this ->driver ->start ();
869
- include_once ($ file );
870
- $ data = $ this ->driver ->stop ();
838
+ if (!$ raw && $ this ->addUncoveredFilesFromWhitelist ) {
839
+ $ filesToProcess = $ this ->filter ()->getWhitelist ();
840
+ } else {
841
+ $ filesToProcess = array_keys ($ this ->data );
842
+ }
843
+
844
+ $ coverageData = $ this ->data ;
845
+ $ this ->data = [];
846
+
847
+ foreach ($ filesToProcess as $ file ) {
848
+
849
+ if (!file_exists ($ file )) {
850
+ continue ;
851
+ }
852
+
853
+ $ isCoveredFile = isset ($ this ->data [$ file ]);
854
+ $ data = [];
855
+
856
+ // If we have coverage data, or we are allowed to process uncovered files
857
+ if ($ isCoveredFile || $ this ->processUncoveredFilesFromWhitelist ) {
858
+ $ this ->driver ->start ();
859
+ include_once ($ file );
860
+ $ data = $ this ->driver ->stop ();
861
+
862
+ } else {
863
+ $ lines = count (file ($ file ));
864
+
865
+ for ($ i = 1 ; $ i <= $ lines ; $ i ++) {
866
+ $ data [$ file ][$ i ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
867
+ }
868
+ }
871
869
872
870
$ this ->initializeFilesThatAreSeenTheFirstTime ($ data );
871
+
872
+ if (!$ isCoveredFile ) {
873
+ // If it is an uncovered file, just append the data
874
+ $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
875
+ }
873
876
}
874
877
875
- foreach (array_keys ($ this ->data ) as $ file ) {
876
- $ this ->uncoveredFiles [$ file ] = 1 ;
878
+ foreach ($ coverageData as $ file => $ lines ) {
879
+ foreach ($ lines as $ k => $ v ) {
880
+ $ this ->data [$ file ][$ k ] = $ v ;
881
+ }
882
+ }
883
+
884
+ foreach ($ this ->data as $ file => $ lines ) {
885
+ foreach ($ this ->getLinesToBeIgnored ($ file ) as $ line ) {
886
+ unset($ this ->data [$ file ][$ line ]);
887
+ }
877
888
}
878
889
}
879
890
}
0 commit comments