@@ -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 ;
@@ -463,6 +451,22 @@ public function setAddUncoveredFilesFromWhitelist($flag)
463
451
$ this ->addUncoveredFilesFromWhitelist = $ flag ;
464
452
}
465
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
+
466
470
/**
467
471
* @param bool $flag
468
472
* @throws PHP_CodeCoverage_InvalidArgumentException
@@ -553,20 +557,6 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
553
557
}
554
558
}
555
559
556
- /**
557
- * Processes whitelisted files that are not covered.
558
- */
559
- private function addUncoveredFilesFromWhitelist ()
560
- {
561
- $ data = [];
562
-
563
- foreach (array_keys ($ this ->uncoveredFiles ) as $ file ) {
564
- $ data [$ file ] = $ this ->data [$ file ];
565
- }
566
-
567
- $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
568
- }
569
-
570
560
/**
571
561
* Returns the lines of a source file that should be ignored.
572
562
*
@@ -841,24 +831,59 @@ private function selectDriver()
841
831
}
842
832
843
833
/**
844
- * Initialise the data with the executable/dead lines from each file in the whitelist
834
+ * Determine uncovered lines
845
835
*/
846
- private function initData ( )
836
+ private function processUncoveredLines ( $ raw = false )
847
837
{
848
- foreach ($ this ->filter ()->getWhitelist () as $ file ) {
849
- $ this ->driver ->start ();
850
- include_once ($ file );
851
- $ data = $ this ->driver ->stop ();
838
+ if (!$ raw && $ this ->addUncoveredFilesFromWhitelist ) {
839
+ $ filesToProcess = $ this ->filter ()->getWhitelist ();
840
+ } else {
841
+ $ filesToProcess = array_keys ($ this ->data );
842
+ }
852
843
853
- foreach ($ this ->getLinesToBeIgnored ($ file ) as $ line ) {
854
- unset($ data [$ file ][$ line ]);
844
+ foreach ($ filesToProcess as $ file ) {
845
+
846
+ if (!file_exists ($ file )) {
847
+ continue ;
855
848
}
856
849
857
- $ this ->initializeFilesThatAreSeenTheFirstTime ($ data );
858
- }
850
+ $ isCoveredFile = isset ($ this ->data [$ file ]);
851
+ $ data = [];
852
+
853
+ // If we have coverage data, or we are allowed to process uncovered files
854
+ if ($ isCoveredFile || $ this ->processUncoveredFilesFromWhitelist ) {
855
+ $ this ->driver ->start ();
856
+ include_once ($ file );
857
+ $ data = $ this ->driver ->stop ();
858
+ } else {
859
+ $ lines = count (file ($ file ));
859
860
860
- foreach (array_keys ($ this ->data ) as $ file ) {
861
- $ this ->uncoveredFiles [$ file ] = 1 ;
861
+ for ($ i = 1 ; $ i <= $ lines ; $ i ++) {
862
+ $ data [$ file ][$ i ] = [];
863
+ }
864
+ }
865
+
866
+ if (empty ($ data [$ file ])) {
867
+ continue ;
868
+ }
869
+
870
+ if (!$ isCoveredFile ) {
871
+ // If it is an uncovered file, just append the data
872
+ $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
873
+ } else {
874
+
875
+ // Otherwise, we need to merge the uncovered/dead code with the coverage data
876
+
877
+ foreach ($ data [$ file ] as $ k => $ v ) {
878
+ if (!isset ($ this ->data [$ file ][$ k ])) {
879
+ $ this ->data [$ file ][$ k ] = $ v == -2 ? null : [];
880
+ }
881
+ }
882
+ }
883
+
884
+ foreach ($ this ->getLinesToBeIgnored ($ file ) as $ line ) {
885
+ unset($ this ->data [$ file ][$ line ]);
886
+ }
862
887
}
863
888
}
864
889
}
0 commit comments