Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove preloading when using processUncoveredFiles #800

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 33 additions & 48 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 = [];
}

/**
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run the tests locally, and as codecov tells, there's no test that covers this line nor the processUncoveredFilesFromFilter method

} elseif ($this->includeUncoveredFiles) {
$this->addUncoveredFilesFromFilter();
}
}

return $this->data;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down