@@ -86,6 +86,20 @@ class PHP_CodeCoverage
86
86
*/
87
87
private $ tests = [];
88
88
89
+ /**
90
+ * Determine if the data has been initialized or not
91
+ *
92
+ * @var bool
93
+ */
94
+ private $ isInitialized = false ;
95
+
96
+ /**
97
+ * Determine whether we need to check for dead and unused code on each test
98
+ *
99
+ * @var bool
100
+ */
101
+ private $ shouldCheckForDeadAndUnused = true ;
102
+
89
103
/**
90
104
* Constructor.
91
105
*
@@ -126,6 +140,7 @@ public function getReport()
126
140
*/
127
141
public function clear ()
128
142
{
143
+ $ this ->isInitialized = false ;
129
144
$ this ->currentId = null ;
130
145
$ this ->data = [];
131
146
$ this ->tests = [];
@@ -211,9 +226,13 @@ public function start($id, $clear = false)
211
226
$ this ->clear ();
212
227
}
213
228
229
+ if ($ this ->isInitialized === false ) {
230
+ $ this ->initializeData ();
231
+ }
232
+
214
233
$ this ->currentId = $ id ;
215
234
216
- $ this ->driver ->start ();
235
+ $ this ->driver ->start ($ this -> shouldCheckForDeadAndUnused );
217
236
}
218
237
219
238
/**
@@ -592,13 +611,7 @@ private function addUncoveredFilesFromWhitelist()
592
611
continue ;
593
612
}
594
613
595
- if ($ this ->processUncoveredFilesFromWhitelist ) {
596
- $ this ->processUncoveredFileFromWhitelist (
597
- $ uncoveredFile ,
598
- $ data ,
599
- $ uncoveredFiles
600
- );
601
- } else {
614
+ if (!$ this ->processUncoveredFilesFromWhitelist ) {
602
615
$ data [$ uncoveredFile ] = [];
603
616
604
617
$ lines = count (file ($ uncoveredFile ));
@@ -612,31 +625,6 @@ private function addUncoveredFilesFromWhitelist()
612
625
$ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
613
626
}
614
627
615
- /**
616
- * @param string $uncoveredFile
617
- * @param array $data
618
- * @param array $uncoveredFiles
619
- */
620
- private function processUncoveredFileFromWhitelist ($ uncoveredFile , array &$ data , array $ uncoveredFiles )
621
- {
622
- $ this ->driver ->start ();
623
- include_once $ uncoveredFile ;
624
- $ coverage = $ this ->driver ->stop ();
625
-
626
- foreach ($ coverage as $ file => $ fileCoverage ) {
627
- if (!isset ($ data [$ file ]) &&
628
- in_array ($ file , $ uncoveredFiles )) {
629
- foreach (array_keys ($ fileCoverage ) as $ key ) {
630
- if ($ fileCoverage [$ key ] == PHP_CodeCoverage_Driver::LINE_EXECUTED ) {
631
- $ fileCoverage [$ key ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
632
- }
633
- }
634
-
635
- $ data [$ file ] = $ fileCoverage ;
636
- }
637
- }
638
- }
639
-
640
628
/**
641
629
* Returns the lines of a source file that should be ignored.
642
630
*
@@ -909,4 +897,45 @@ private function selectDriver()
909
897
return new PHP_CodeCoverage_Driver_Xdebug ;
910
898
}
911
899
}
900
+
901
+ /**
902
+ * If we are processing uncovered files from whitelist,
903
+ * we can initialize the data before we start to speed up the tests
904
+ */
905
+ protected function initializeData ()
906
+ {
907
+ $ this ->isInitialized = true ;
908
+
909
+ if ($ this ->processUncoveredFilesFromWhitelist ) {
910
+
911
+ $ this ->shouldCheckForDeadAndUnused = false ;
912
+
913
+ $ this ->driver ->start (true );
914
+
915
+ foreach ($ this ->filter ->getWhitelist () as $ file ) {
916
+ if ($ this ->filter ->isFile ($ file )) {
917
+ include_once ($ file );
918
+ }
919
+ }
920
+
921
+ $ data = [];
922
+ $ coverage = $ this ->driver ->stop ();
923
+
924
+ foreach ($ coverage as $ file => $ fileCoverage ) {
925
+ if ($ this ->filter ->isFiltered ($ file )) {
926
+ continue ;
927
+ }
928
+
929
+ foreach (array_keys ($ fileCoverage ) as $ key ) {
930
+ if ($ fileCoverage [$ key ] == PHP_CodeCoverage_Driver::LINE_EXECUTED ) {
931
+ $ fileCoverage [$ key ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
932
+ }
933
+ }
934
+
935
+ $ data [$ file ] = $ fileCoverage ;
936
+ }
937
+
938
+ $ this ->append ($ coverage , 'UNCOVERED_FILES_FROM_WHITELIST ' );
939
+ }
940
+ }
912
941
}
0 commit comments