From 7e9109986c772257f839230e3a869bb6a9638bf0 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sun, 23 Aug 2020 16:46:25 +0100 Subject: [PATCH] Remove preloading when using processUncoveredFiles As discussed in #799, this feature is no longer required because of Xdebug's built in filter --- src/CodeCoverage.php | 75 +++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 6eccd3b93..8e0a1d50e 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -20,6 +20,7 @@ use function explode; use function file_exists; use function get_class; +use function in_array; use function is_array; use function sort; use PHPUnit\Framework\TestCase; @@ -108,13 +109,6 @@ final class CodeCoverage */ private $parentClassesExcludedFromUnintentionallyCoveredCodeCheck = []; - /** - * Determine if the data has been initialized or not. - * - * @var bool - */ - private $isInitialized = false; - /** * @var ?CoveredFileAnalyser */ @@ -151,10 +145,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 +163,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 +209,6 @@ public function start($id, bool $clear = false): void $this->clear(); } - if ($this->isInitialized === false) { - $this->initializeData(); - } - $this->currentId = $id; $this->driver->start(); @@ -526,6 +519,24 @@ private function addUncoveredFilesFromFilter(): void } } + /** + * @throws UnintentionallyCoveredCodeException + */ + private function processUncoveredFilesFromFilter(): void + { + $coveredFiles = $this->data->coveredFiles(); + + $this->driver->start(); + + foreach ($this->filter->files() as $file) { + if (!in_array($file, $coveredFiles, true) && $this->filter->isFile($file)) { + include_once $file; + } + } + + $this->append($this->driver->stop(), self::UNCOVERED_FILES); + } + /** * @throws UnintentionallyCoveredCodeException * @throws ReflectionException @@ -628,36 +639,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) {