@@ -81,6 +81,20 @@ class PHP_CodeCoverage
81
81
*/
82
82
private $ tests = [];
83
83
84
+ /**
85
+ * Determine if the data has been initialized or not
86
+ *
87
+ * @var bool
88
+ */
89
+ private $ isInitialized = false ;
90
+
91
+ /**
92
+ * Determine whether we need to check for dead and unused code on each test
93
+ *
94
+ * @var bool
95
+ */
96
+ private $ shouldCheckForDeadAndUnused = true ;
97
+
84
98
/**
85
99
* Constructor.
86
100
*
@@ -121,6 +135,7 @@ public function getReport()
121
135
*/
122
136
public function clear ()
123
137
{
138
+ $ this ->isInitialized = false ;
124
139
$ this ->currentId = null ;
125
140
$ this ->data = [];
126
141
$ this ->tests = [];
@@ -206,9 +221,13 @@ public function start($id, $clear = false)
206
221
$ this ->clear ();
207
222
}
208
223
224
+ if ($ this ->isInitialized === false ) {
225
+ $ this ->initializeData ();
226
+ }
227
+
209
228
$ this ->currentId = $ id ;
210
229
211
- $ this ->driver ->start ();
230
+ $ this ->driver ->start ($ this -> shouldCheckForDeadAndUnused );
212
231
}
213
232
214
233
/**
@@ -580,13 +599,7 @@ private function addUncoveredFilesFromWhitelist()
580
599
continue ;
581
600
}
582
601
583
- if ($ this ->processUncoveredFilesFromWhitelist ) {
584
- $ this ->processUncoveredFileFromWhitelist (
585
- $ uncoveredFile ,
586
- $ data ,
587
- $ uncoveredFiles
588
- );
589
- } else {
602
+ if (!$ this ->processUncoveredFilesFromWhitelist ) {
590
603
$ data [$ uncoveredFile ] = [];
591
604
592
605
$ lines = count (file ($ uncoveredFile ));
@@ -600,31 +613,6 @@ private function addUncoveredFilesFromWhitelist()
600
613
$ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
601
614
}
602
615
603
- /**
604
- * @param string $uncoveredFile
605
- * @param array $data
606
- * @param array $uncoveredFiles
607
- */
608
- private function processUncoveredFileFromWhitelist ($ uncoveredFile , array &$ data , array $ uncoveredFiles )
609
- {
610
- $ this ->driver ->start ();
611
- include_once $ uncoveredFile ;
612
- $ coverage = $ this ->driver ->stop ();
613
-
614
- foreach ($ coverage as $ file => $ fileCoverage ) {
615
- if (!isset ($ data [$ file ]) &&
616
- in_array ($ file , $ uncoveredFiles )) {
617
- foreach (array_keys ($ fileCoverage ) as $ key ) {
618
- if ($ fileCoverage [$ key ] == PHP_CodeCoverage_Driver::LINE_EXECUTED ) {
619
- $ fileCoverage [$ key ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
620
- }
621
- }
622
-
623
- $ data [$ file ] = $ fileCoverage ;
624
- }
625
- }
626
- }
627
-
628
616
/**
629
617
* Returns the lines of a source file that should be ignored.
630
618
*
@@ -897,4 +885,45 @@ private function selectDriver()
897
885
return new PHP_CodeCoverage_Driver_Xdebug ;
898
886
}
899
887
}
888
+
889
+ /**
890
+ * If we are processing uncovered files from whitelist,
891
+ * we can initialize the data before we start to speed up the tests
892
+ */
893
+ protected function initializeData ()
894
+ {
895
+ $ this ->isInitialized = true ;
896
+
897
+ if ($ this ->processUncoveredFilesFromWhitelist ) {
898
+
899
+ $ this ->shouldCheckForDeadAndUnused = false ;
900
+
901
+ $ this ->driver ->start (true );
902
+
903
+ foreach ($ this ->filter ->getWhitelist () as $ file ) {
904
+ if ($ this ->filter ->isFile ($ file )) {
905
+ include_once ($ file );
906
+ }
907
+ }
908
+
909
+ $ data = [];
910
+ $ coverage = $ this ->driver ->stop ();
911
+
912
+ foreach ($ coverage as $ file => $ fileCoverage ) {
913
+ if ($ this ->filter ->isFiltered ($ file )) {
914
+ continue ;
915
+ }
916
+
917
+ foreach (array_keys ($ fileCoverage ) as $ key ) {
918
+ if ($ fileCoverage [$ key ] == PHP_CodeCoverage_Driver::LINE_EXECUTED ) {
919
+ $ fileCoverage [$ key ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
920
+ }
921
+ }
922
+
923
+ $ data [$ file ] = $ fileCoverage ;
924
+ }
925
+
926
+ $ this ->append ($ coverage , 'UNCOVERED_FILES_FROM_WHITELIST ' );
927
+ }
928
+ }
900
929
}
0 commit comments