@@ -113,6 +113,20 @@ class CodeCoverage
113113 */
114114 private $ unintentionallyCoveredSubclassesWhitelist = [];
115115
116+ /**
117+ * Determine if the data has been initialized or not
118+ *
119+ * @var bool
120+ */
121+ private $ isInitialized = false ;
122+
123+ /**
124+ * Determine whether we need to check for dead and unused code on each test
125+ *
126+ * @var bool
127+ */
128+ private $ shouldCheckForDeadAndUnused = true ;
129+
116130 /**
117131 * Constructor.
118132 *
@@ -154,6 +168,7 @@ public function getReport()
154168 */
155169 public function clear ()
156170 {
171+ $ this ->isInitialized = false ;
157172 $ this ->currentId = null ;
158173 $ this ->data = [];
159174 $ this ->tests = [];
@@ -237,9 +252,13 @@ public function start($id, $clear = false)
237252 $ this ->clear ();
238253 }
239254
255+ if ($ this ->isInitialized === false ) {
256+ $ this ->initializeData ();
257+ }
258+
240259 $ this ->currentId = $ id ;
241260
242- $ this ->driver ->start ();
261+ $ this ->driver ->start ($ this -> shouldCheckForDeadAndUnused );
243262 }
244263
245264 /**
@@ -687,13 +706,7 @@ private function addUncoveredFilesFromWhitelist()
687706 continue ;
688707 }
689708
690- if ($ this ->processUncoveredFilesFromWhitelist ) {
691- $ this ->processUncoveredFileFromWhitelist (
692- $ uncoveredFile ,
693- $ data ,
694- $ uncoveredFiles
695- );
696- } else {
709+ if (!$ this ->processUncoveredFilesFromWhitelist ) {
697710 $ data [$ uncoveredFile ] = [];
698711
699712 $ lines = count (file ($ uncoveredFile ));
@@ -707,31 +720,6 @@ private function addUncoveredFilesFromWhitelist()
707720 $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
708721 }
709722
710- /**
711- * @param string $uncoveredFile
712- * @param array $data
713- * @param array $uncoveredFiles
714- */
715- private function processUncoveredFileFromWhitelist ($ uncoveredFile , array &$ data , array $ uncoveredFiles )
716- {
717- $ this ->driver ->start ();
718- include_once $ uncoveredFile ;
719- $ coverage = $ this ->driver ->stop ();
720-
721- foreach ($ coverage as $ file => $ fileCoverage ) {
722- if (!isset ($ data [$ file ]) &&
723- in_array ($ file , $ uncoveredFiles )) {
724- foreach (array_keys ($ fileCoverage ) as $ key ) {
725- if ($ fileCoverage [$ key ] == Driver::LINE_EXECUTED ) {
726- $ fileCoverage [$ key ] = Driver::LINE_NOT_EXECUTED ;
727- }
728- }
729-
730- $ data [$ file ] = $ fileCoverage ;
731- }
732- }
733- }
734-
735723 /**
736724 * Returns the lines of a source file that should be ignored.
737725 *
@@ -1074,4 +1062,45 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere
10741062
10751063 return array_values ($ unintentionallyCoveredUnits );
10761064 }
1065+
1066+ /**
1067+ * If we are processing uncovered files from whitelist,
1068+ * we can initialize the data before we start to speed up the tests
1069+ */
1070+ protected function initializeData ()
1071+ {
1072+ $ this ->isInitialized = true ;
1073+
1074+ if ($ this ->processUncoveredFilesFromWhitelist ) {
1075+
1076+ $ this ->shouldCheckForDeadAndUnused = false ;
1077+
1078+ $ this ->driver ->start (true );
1079+
1080+ foreach ($ this ->filter ->getWhitelist () as $ file ) {
1081+ if ($ this ->filter ->isFile ($ file )) {
1082+ include_once ($ file );
1083+ }
1084+ }
1085+
1086+ $ data = [];
1087+ $ coverage = $ this ->driver ->stop ();
1088+
1089+ foreach ($ coverage as $ file => $ fileCoverage ) {
1090+ if ($ this ->filter ->isFiltered ($ file )) {
1091+ continue ;
1092+ }
1093+
1094+ foreach (array_keys ($ fileCoverage ) as $ key ) {
1095+ if ($ fileCoverage [$ key ] == PHP_CodeCoverage_Driver::LINE_EXECUTED ) {
1096+ $ fileCoverage [$ key ] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED ;
1097+ }
1098+ }
1099+
1100+ $ data [$ file ] = $ fileCoverage ;
1101+ }
1102+
1103+ $ this ->append ($ coverage , 'UNCOVERED_FILES_FROM_WHITELIST ' );
1104+ }
1105+ }
10771106}
0 commit comments