20
20
use function explode ;
21
21
use function file_exists ;
22
22
use function get_class ;
23
+ use function in_array ;
23
24
use function is_array ;
24
25
use function sort ;
25
26
use PHPUnit \Framework \TestCase ;
@@ -108,13 +109,6 @@ final class CodeCoverage
108
109
*/
109
110
private $ parentClassesExcludedFromUnintentionallyCoveredCodeCheck = [];
110
111
111
- /**
112
- * Determine if the data has been initialized or not.
113
- *
114
- * @var bool
115
- */
116
- private $ isInitialized = false ;
117
-
118
112
/**
119
113
* @var ?CoveredFileAnalyser
120
114
*/
@@ -151,10 +145,9 @@ public function getReport(): Directory
151
145
*/
152
146
public function clear (): void
153
147
{
154
- $ this ->isInitialized = false ;
155
- $ this ->currentId = null ;
156
- $ this ->data = new ProcessedCodeCoverageData ;
157
- $ this ->tests = [];
148
+ $ this ->currentId = null ;
149
+ $ this ->data = new ProcessedCodeCoverageData ;
150
+ $ this ->tests = [];
158
151
}
159
152
160
153
/**
@@ -170,8 +163,12 @@ public function filter(): Filter
170
163
*/
171
164
public function getData (bool $ raw = false ): ProcessedCodeCoverageData
172
165
{
173
- if (!$ raw && $ this ->includeUncoveredFiles ) {
174
- $ this ->addUncoveredFilesFromFilter ();
166
+ if (!$ raw ) {
167
+ if ($ this ->processUncoveredFiles ) {
168
+ $ this ->processUncoveredFilesFromFilter ();
169
+ } elseif ($ this ->includeUncoveredFiles ) {
170
+ $ this ->addUncoveredFilesFromFilter ();
171
+ }
175
172
}
176
173
177
174
return $ this ->data ;
@@ -212,10 +209,6 @@ public function start($id, bool $clear = false): void
212
209
$ this ->clear ();
213
210
}
214
211
215
- if ($ this ->isInitialized === false ) {
216
- $ this ->initializeData ();
217
- }
218
-
219
212
$ this ->currentId = $ id ;
220
213
221
214
$ this ->driver ->start ();
@@ -526,6 +519,24 @@ private function addUncoveredFilesFromFilter(): void
526
519
}
527
520
}
528
521
522
+ /**
523
+ * @throws UnintentionallyCoveredCodeException
524
+ */
525
+ private function processUncoveredFilesFromFilter (): void
526
+ {
527
+ $ coveredFiles = $ this ->data ->coveredFiles ();
528
+
529
+ $ this ->driver ->start ();
530
+
531
+ foreach ($ this ->filter ->files () as $ file ) {
532
+ if (!in_array ($ file , $ coveredFiles , true ) && $ this ->filter ->isFile ($ file )) {
533
+ include_once $ file ;
534
+ }
535
+ }
536
+
537
+ $ this ->append ($ this ->driver ->stop (), self ::UNCOVERED_FILES );
538
+ }
539
+
529
540
/**
530
541
* @throws UnintentionallyCoveredCodeException
531
542
* @throws ReflectionException
@@ -628,36 +639,6 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere
628
639
return array_values ($ unintentionallyCoveredUnits );
629
640
}
630
641
631
- /**
632
- * @throws UnintentionallyCoveredCodeException
633
- */
634
- private function initializeData (): void
635
- {
636
- $ this ->isInitialized = true ;
637
-
638
- if ($ this ->processUncoveredFiles ) {
639
- // by collecting dead code data here on an initial pass, future runs with test data do not need to
640
- if ($ this ->driver ->canDetectDeadCode ()) {
641
- $ this ->driver ->enableDeadCodeDetection ();
642
- }
643
-
644
- $ this ->driver ->start ();
645
-
646
- foreach ($ this ->filter ->files () as $ file ) {
647
- if ($ this ->filter ->isFile ($ file )) {
648
- include_once $ file ;
649
- }
650
- }
651
-
652
- // having now collected dead code for the entire list of files, we can safely skip this data on subsequent runs
653
- if ($ this ->driver ->canDetectDeadCode ()) {
654
- $ this ->driver ->disableDeadCodeDetection ();
655
- }
656
-
657
- $ this ->append ($ this ->driver ->stop (), self ::UNCOVERED_FILES );
658
- }
659
- }
660
-
661
642
private function coveredFileAnalyser (): CoveredFileAnalyser
662
643
{
663
644
if ($ this ->coveredFileAnalyser !== null ) {
0 commit comments