diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 6eccd3b93..9a92b91b1 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -108,13 +108,6 @@ final class CodeCoverage */ private $parentClassesExcludedFromUnintentionallyCoveredCodeCheck = []; - /** - * Determine if the data has been initialized or not. - * - * @var bool - */ - private $isInitialized = false; - /** * @var ?CoveredFileAnalyser */ @@ -151,10 +144,9 @@ public function getReport(): Directory */ public function clear(): void { - $this->isInitialized = false; - $this->currentId = null; - $this->data = new ProcessedCodeCoverageData; - $this->tests = []; + $this->currentId = null; + $this->data = new ProcessedCodeCoverageData; + $this->tests = []; } /** @@ -170,8 +162,12 @@ public function filter(): Filter */ public function getData(bool $raw = false): ProcessedCodeCoverageData { - if (!$raw && $this->includeUncoveredFiles) { - $this->addUncoveredFilesFromFilter(); + if (!$raw) { + if ($this->processUncoveredFiles) { + $this->processUncoveredFilesFromFilter(); + } elseif ($this->includeUncoveredFiles) { + $this->addUncoveredFilesFromFilter(); + } } return $this->data; @@ -212,10 +208,6 @@ public function start($id, bool $clear = false): void $this->clear(); } - if ($this->isInitialized === false) { - $this->initializeData(); - } - $this->currentId = $id; $this->driver->start(); @@ -510,7 +502,7 @@ private function addUncoveredFilesFromFilter(): void { $uncoveredFiles = array_diff( $this->filter->files(), - array_keys($this->data->lineCoverage()) + $this->data->coveredFiles() ); foreach ($uncoveredFiles as $uncoveredFile) { @@ -526,6 +518,29 @@ private function addUncoveredFilesFromFilter(): void } } + /** + * @throws UnintentionallyCoveredCodeException + */ + private function processUncoveredFilesFromFilter(): void + { + $uncoveredFiles = array_diff( + $this->filter->files(), + $this->data->coveredFiles() + ); + + $this->driver->start(); + + foreach ($this->filter->files() as $file) { + foreach ($uncoveredFiles as $uncoveredFile) { + if (file_exists($uncoveredFile)) { + include_once $file; + } + } + } + + $this->append($this->driver->stop(), self::UNCOVERED_FILES); + } + /** * @throws UnintentionallyCoveredCodeException * @throws ReflectionException @@ -628,36 +643,6 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere return array_values($unintentionallyCoveredUnits); } - /** - * @throws UnintentionallyCoveredCodeException - */ - private function initializeData(): void - { - $this->isInitialized = true; - - if ($this->processUncoveredFiles) { - // by collecting dead code data here on an initial pass, future runs with test data do not need to - if ($this->driver->canDetectDeadCode()) { - $this->driver->enableDeadCodeDetection(); - } - - $this->driver->start(); - - foreach ($this->filter->files() as $file) { - if ($this->filter->isFile($file)) { - include_once $file; - } - } - - // having now collected dead code for the entire list of files, we can safely skip this data on subsequent runs - if ($this->driver->canDetectDeadCode()) { - $this->driver->disableDeadCodeDetection(); - } - - $this->append($this->driver->stop(), self::UNCOVERED_FILES); - } - } - private function coveredFileAnalyser(): CoveredFileAnalyser { if ($this->coveredFileAnalyser !== null) {