Skip to content

Commit 75111c6

Browse files
author
Rob Caiger
committed
Moved ignored lines check
1 parent 8f96ae3 commit 75111c6

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

src/CodeCoverage.php

+69-44
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class PHP_CodeCoverage
5252
*/
5353
private $addUncoveredFilesFromWhitelist = true;
5454

55+
/**
56+
* @var bool
57+
*/
58+
private $processUncoveredFilesFromWhitelist = false;
59+
5560
/**
5661
* @var mixed
5762
*/
@@ -81,13 +86,6 @@ class PHP_CodeCoverage
8186
*/
8287
private $tests = [];
8388

84-
/**
85-
* Store all uncovered files
86-
*
87-
* @var array
88-
*/
89-
private $uncoveredFiles = [];
90-
9189
/**
9290
* Constructor.
9391
*
@@ -107,8 +105,6 @@ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCove
107105

108106
$this->driver = $driver;
109107
$this->filter = $filter;
110-
111-
$this->initData();
112108
}
113109

114110
/**
@@ -155,9 +151,7 @@ public function filter()
155151
*/
156152
public function getData($raw = false)
157153
{
158-
if (!$raw && $this->addUncoveredFilesFromWhitelist) {
159-
$this->addUncoveredFilesFromWhitelist();
160-
}
154+
$this->processUncoveredLines($raw);
161155

162156
return $this->data;
163157
}
@@ -248,12 +242,6 @@ public function stop($append = true, $linesToBeCovered = [], array $linesToBeUse
248242
$data = $this->driver->stop();
249243
$this->append($data, null, $append, $linesToBeCovered, $linesToBeUsed);
250244

251-
foreach (array_keys($data) as $file) {
252-
if (isset($this->uncoveredFiles[$file])) {
253-
unset($this->uncoveredFiles[$file]);
254-
}
255-
}
256-
257245
$this->currentId = null;
258246

259247
return $data;
@@ -463,6 +451,22 @@ public function setAddUncoveredFilesFromWhitelist($flag)
463451
$this->addUncoveredFilesFromWhitelist = $flag;
464452
}
465453

454+
/**
455+
* @param bool $flag
456+
* @throws PHP_CodeCoverage_InvalidArgumentException
457+
*/
458+
public function setProcessUncoveredFilesFromWhitelist($flag)
459+
{
460+
if (!is_bool($flag)) {
461+
throw PHP_CodeCoverage_InvalidArgumentException::create(
462+
1,
463+
'boolean'
464+
);
465+
}
466+
467+
$this->processUncoveredFilesFromWhitelist = $flag;
468+
}
469+
466470
/**
467471
* @param bool $flag
468472
* @throws PHP_CodeCoverage_InvalidArgumentException
@@ -553,20 +557,6 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
553557
}
554558
}
555559

556-
/**
557-
* Processes whitelisted files that are not covered.
558-
*/
559-
private function addUncoveredFilesFromWhitelist()
560-
{
561-
$data = [];
562-
563-
foreach (array_keys($this->uncoveredFiles) as $file) {
564-
$data[$file] = $this->data[$file];
565-
}
566-
567-
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
568-
}
569-
570560
/**
571561
* Returns the lines of a source file that should be ignored.
572562
*
@@ -841,24 +831,59 @@ private function selectDriver()
841831
}
842832

843833
/**
844-
* Initialise the data with the executable/dead lines from each file in the whitelist
834+
* Determine uncovered lines
845835
*/
846-
private function initData()
836+
private function processUncoveredLines($raw = false)
847837
{
848-
foreach ($this->filter()->getWhitelist() as $file) {
849-
$this->driver->start();
850-
include_once($file);
851-
$data = $this->driver->stop();
838+
if (!$raw && $this->addUncoveredFilesFromWhitelist) {
839+
$filesToProcess = $this->filter()->getWhitelist();
840+
} else {
841+
$filesToProcess = array_keys($this->data);
842+
}
852843

853-
foreach ($this->getLinesToBeIgnored($file) as $line) {
854-
unset($data[$file][$line]);
844+
foreach ($filesToProcess as $file) {
845+
846+
if (!file_exists($file)) {
847+
continue;
855848
}
856849

857-
$this->initializeFilesThatAreSeenTheFirstTime($data);
858-
}
850+
$isCoveredFile = isset($this->data[$file]);
851+
$data = [];
852+
853+
// If we have coverage data, or we are allowed to process uncovered files
854+
if ($isCoveredFile || $this->processUncoveredFilesFromWhitelist) {
855+
$this->driver->start();
856+
include_once($file);
857+
$data = $this->driver->stop();
858+
} else {
859+
$lines = count(file($file));
859860

860-
foreach (array_keys($this->data) as $file) {
861-
$this->uncoveredFiles[$file] = 1;
861+
for ($i = 1; $i <= $lines; $i++) {
862+
$data[$file][$i] = [];
863+
}
864+
}
865+
866+
if (empty($data[$file])) {
867+
continue;
868+
}
869+
870+
if (!$isCoveredFile) {
871+
// If it is an uncovered file, just append the data
872+
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
873+
} else {
874+
875+
// Otherwise, we need to merge the uncovered/dead code with the coverage data
876+
877+
foreach ($data[$file] as $k => $v) {
878+
if (!isset($this->data[$file][$k])) {
879+
$this->data[$file][$k] = $v == -2 ? null : [];
880+
}
881+
}
882+
}
883+
884+
foreach ($this->getLinesToBeIgnored($file) as $line) {
885+
unset($this->data[$file][$line]);
886+
}
862887
}
863888
}
864889
}

0 commit comments

Comments
 (0)