Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df9ddc8

Browse files
author
Rob Caiger
committedSep 28, 2015
Re-introduction of processUncoveredFilesFromWhitelist alongside performance tweaks
1 parent 71b3eae commit df9ddc8

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed
 

‎ChangeLog-3.0.md

-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ All notable changes of the PHP_CodeCoverage 3.0 release series are documented in
66

77
### Changed
88

9-
* It is now mandatory to configure a whitelist
10-
* Merged [#384](https://github.com/sebastianbergmann/php-code-coverage/pull/384): Performance improvements
119
* It is now mandatory to configure a whitelist
1210
* Merged [#384](https://github.com/sebastianbergmann/php-code-coverage/pull/384): Performance improvements
1311

1412
### Removed
1513

16-
* The blacklist functionality has been removed
17-
* The `PHP_CodeCoverage::setProcessUncoveredFilesFromWhitelist()` method has been remove
1814
* The blacklist functionality has been removed
1915
* The `PHP_CodeCoverage::setProcessUncoveredFilesFromWhitelist()` method has been remove
2016
* PHP_CodeCoverage is no longer supported on PHP 5.3, PHP 5.4, and PHP 5.5

‎src/CodeCoverage.php

+70-59
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;
@@ -280,7 +268,6 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
280268
}
281269

282270
$this->applyListsFilter($data);
283-
$this->applyIgnoredLinesFilter($data);
284271
$this->initializeFilesThatAreSeenTheFirstTime($data);
285272

286273
if (!$append) {
@@ -464,6 +451,22 @@ public function setAddUncoveredFilesFromWhitelist($flag)
464451
$this->addUncoveredFilesFromWhitelist = $flag;
465452
}
466453

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+
467470
/**
468471
* @param bool $flag
469472
* @throws PHP_CodeCoverage_InvalidArgumentException
@@ -535,24 +538,6 @@ private function applyListsFilter(array &$data)
535538
}
536539
}
537540

538-
/**
539-
* Applies the "ignored lines" filtering.
540-
*
541-
* @param array $data
542-
*/
543-
private function applyIgnoredLinesFilter(array &$data)
544-
{
545-
foreach (array_keys($data) as $filename) {
546-
if (!$this->filter->isFile($filename)) {
547-
continue;
548-
}
549-
550-
foreach ($this->getLinesToBeIgnored($filename) as $line) {
551-
unset($data[$filename][$line]);
552-
}
553-
}
554-
}
555-
556541
/**
557542
* @param array $data
558543
* @since Method available since Release 1.1.0
@@ -572,20 +557,6 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
572557
}
573558
}
574559

575-
/**
576-
* Processes whitelisted files that are not covered.
577-
*/
578-
private function addUncoveredFilesFromWhitelist()
579-
{
580-
$data = [];
581-
582-
foreach (array_keys($this->uncoveredFiles) as $file) {
583-
$data[$file] = $this->data[$file];
584-
}
585-
586-
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
587-
}
588-
589560
/**
590561
* Returns the lines of a source file that should be ignored.
591562
*
@@ -860,20 +831,60 @@ private function selectDriver()
860831
}
861832

862833
/**
863-
* Initialise the data with the executable/dead lines from each file in the whitelist
834+
* Determine uncovered lines
864835
*/
865-
private function initData()
836+
private function processUncoveredLines($raw = false)
866837
{
867-
foreach ($this->filter()->getWhitelist() as $file) {
868-
$this->driver->start();
869-
include_once($file);
870-
$data = $this->driver->stop();
838+
if (!$raw && $this->addUncoveredFilesFromWhitelist) {
839+
$filesToProcess = $this->filter()->getWhitelist();
840+
} else {
841+
$filesToProcess = array_keys($this->data);
842+
}
843+
844+
$coverageData = $this->data;
845+
$this->data = [];
846+
847+
foreach ($filesToProcess as $file) {
848+
849+
if (!file_exists($file)) {
850+
continue;
851+
}
852+
853+
$isCoveredFile = isset($this->data[$file]);
854+
$data = [];
855+
856+
// If we have coverage data, or we are allowed to process uncovered files
857+
if ($isCoveredFile || $this->processUncoveredFilesFromWhitelist) {
858+
$this->driver->start();
859+
include_once($file);
860+
$data = $this->driver->stop();
861+
862+
} else {
863+
$lines = count(file($file));
864+
865+
for ($i = 1; $i <= $lines; $i++) {
866+
$data[$file][$i] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
867+
}
868+
}
871869

872870
$this->initializeFilesThatAreSeenTheFirstTime($data);
871+
872+
if (!$isCoveredFile) {
873+
// If it is an uncovered file, just append the data
874+
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
875+
}
873876
}
874877

875-
foreach (array_keys($this->data) as $file) {
876-
$this->uncoveredFiles[$file] = 1;
878+
foreach ($coverageData as $file => $lines) {
879+
foreach ($lines as $k => $v) {
880+
$this->data[$file][$k] = $v;
881+
}
882+
}
883+
884+
foreach ($this->data as $file => $lines) {
885+
foreach ($this->getLinesToBeIgnored($file) as $line) {
886+
unset($this->data[$file][$line]);
887+
}
877888
}
878889
}
879890
}

0 commit comments

Comments
 (0)
Please sign in to comment.