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

Speedup #387

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
95 changes: 62 additions & 33 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ class PHP_CodeCoverage
*/
private $tests = [];

/**
* Determine if the data has been initialized or not
*
* @var bool
*/
private $isInitialized = false;

/**
* Determine whether we need to check for dead and unused code on each test
*
* @var bool
*/
private $shouldCheckForDeadAndUnused = true;

/**
* Constructor.
*
Expand Down Expand Up @@ -121,6 +135,7 @@ public function getReport()
*/
public function clear()
{
$this->isInitialized = false;
$this->currentId = null;
$this->data = [];
$this->tests = [];
Expand Down Expand Up @@ -206,9 +221,13 @@ public function start($id, $clear = false)
$this->clear();
}

if ($this->isInitialized === false) {
$this->initializeData();
}

$this->currentId = $id;

$this->driver->start();
$this->driver->start($this->shouldCheckForDeadAndUnused);
}

/**
Expand Down Expand Up @@ -580,13 +599,7 @@ private function addUncoveredFilesFromWhitelist()
continue;
}

if ($this->processUncoveredFilesFromWhitelist) {
$this->processUncoveredFileFromWhitelist(
$uncoveredFile,
$data,
$uncoveredFiles
);
} else {
if (!$this->processUncoveredFilesFromWhitelist) {
$data[$uncoveredFile] = [];

$lines = count(file($uncoveredFile));
Expand All @@ -600,31 +613,6 @@ private function addUncoveredFilesFromWhitelist()
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
}

/**
* @param string $uncoveredFile
* @param array $data
* @param array $uncoveredFiles
*/
private function processUncoveredFileFromWhitelist($uncoveredFile, array &$data, array $uncoveredFiles)
{
$this->driver->start();
include_once $uncoveredFile;
$coverage = $this->driver->stop();

foreach ($coverage as $file => $fileCoverage) {
if (!isset($data[$file]) &&
in_array($file, $uncoveredFiles)) {
foreach (array_keys($fileCoverage) as $key) {
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
}
}

$data[$file] = $fileCoverage;
}
}
}

/**
* Returns the lines of a source file that should be ignored.
*
Expand Down Expand Up @@ -897,4 +885,45 @@ private function selectDriver()
return new PHP_CodeCoverage_Driver_Xdebug;
}
}

/**
* If we are processing uncovered files from whitelist,
* we can initialize the data before we start to speed up the tests
*/
protected function initializeData()
{
$this->isInitialized = true;

if ($this->processUncoveredFilesFromWhitelist) {

$this->shouldCheckForDeadAndUnused = false;

$this->driver->start(true);

foreach ($this->filter->getWhitelist() as $file) {
if ($this->filter->isFile($file)) {
include_once($file);
}
}

$data = [];
$coverage = $this->driver->stop();

foreach ($coverage as $file => $fileCoverage) {
if ($this->filter->isFiltered($file)) {
continue;
}

foreach (array_keys($fileCoverage) as $key) {
if ($fileCoverage[$key] == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
$fileCoverage[$key] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
}
}

$data[$file] = $fileCoverage;
}

$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
}
}
}
2 changes: 1 addition & 1 deletion src/CodeCoverage/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface PHP_CodeCoverage_Driver
/**
* Start collection of code coverage information.
*/
public function start();
public function start($determineUnusedAndDead = true);

/**
* Stop collection of code coverage information.
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCoverage/Driver/HHVM.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PHP_CodeCoverage_Driver_HHVM extends PHP_CodeCoverage_Driver_Xdebug
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
xdebug_start_code_coverage();
}
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCoverage/Driver/PHPDBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct()
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
phpdbg_start_oplog();
}
Expand Down
29 changes: 22 additions & 7 deletions src/CodeCoverage/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
{
/**
* Cache the number of lines for each file
*
* @var array
*/
private $cacheNumLines = [];

/**
* Constructor.
*/
Expand All @@ -36,9 +43,13 @@ public function __construct()
/**
* Start collection of code coverage information.
*/
public function start()
public function start($determineUnusedAndDead = true)
{
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
if ($determineUnusedAndDead) {
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
} else {
xdebug_start_code_coverage();
Copy link
Contributor

Choose a reason for hiding this comment

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

Path coverage will need all flags enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

Anyway path coverage in this point only facilitate ccn so it's ok.

}
}

/**
Expand Down Expand Up @@ -85,13 +96,17 @@ private function cleanup(array $data)
*/
private function getNumberOfLinesInFile($file)
{
$buffer = file_get_contents($file);
$lines = substr_count($buffer, "\n");
if (!isset($this->cacheNumLines[$file])) {
$buffer = file_get_contents($file);
$lines = substr_count($buffer, "\n");

if (substr($buffer, -1) !== "\n") {
$lines++;
}

if (substr($buffer, -1) !== "\n") {
$lines++;
$this->cacheNumLines[$file] = $lines;
}

return $lines;
return $this->cacheNumLines[$file];
}
}