Skip to content

Path clover #1

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

Open
wants to merge 19 commits into
base: master-copy
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ language: php
php:
- 5.6

install:
- pecl install xdebug
- php --version

before_script:
- COMPOSER_ROOT_VERSION=dev-master composer install --prefer-source

Expand Down
160 changes: 123 additions & 37 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,41 @@ class PHP_CodeCoverage
*/
private $tests = [];

/**
* @var bool
*/
private $pathCoverage;

/**
* Constructor.
*
* @param PHP_CodeCoverage_Driver $driver
* @param PHP_CodeCoverage_Filter $filter
* @throws PHP_CodeCoverage_RuntimeException
* @param PHP_CodeCoverage_Driver $driver
* @param PHP_CodeCoverage_Filter $filter
* @param null|bool $pathCoverage `null` enables path coverage if supported.
* @throws PHP_CodeCoverage_InvalidArgumentException
*/
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null)
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null, $pathCoverage = null)
{
if ($pathCoverage === null) {
$pathCoverage = version_compare(phpversion('xdebug'), '2.3.2', '>=');
} elseif (!is_bool($pathCoverage)) {
throw PHP_CodeCoverage_InvalidArgumentException::create(
3,
'boolean'
);
}

if ($driver === null) {
$driver = $this->selectDriver();
$driver = $this->selectDriver($pathCoverage);
}

if ($filter === null) {
$filter = new PHP_CodeCoverage_Filter;
}

$this->driver = $driver;
$this->filter = $filter;
$this->driver = $driver;
$this->filter = $filter;
$this->pathCoverage = $pathCoverage;
}

/**
Expand Down Expand Up @@ -307,15 +323,39 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere

$this->tests[$id] = ['size' => $size, 'status' => $status];

foreach ($data as $file => $lines) {
foreach ($data as $file => $fileData) {
if (!$this->filter->isFile($file)) {
continue;
}

foreach ($lines as $k => $v) {
if ($v == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
if (empty($this->data[$file][$k]) || !in_array($id, $this->data[$file][$k])) {
$this->data[$file][$k][] = $id;
foreach ($fileData['lines'] as $function => $functionCoverage) {
if ($functionCoverage === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
$lineData = &$this->data[$file]['lines'][$function];
if ($lineData === null) {
$lineData = [
'pathCovered' => false,
'tests' => [$id],
];
} elseif ($this->pathCoverage && !in_array($id, $lineData['tests'])) {
$lineData['tests'][] = $id;
}
}
}

if ($this->pathCoverage) {
foreach ($fileData['functions'] as $function => $functionCoverage) {
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
if ($branchCoverage['hit'] === 1) {
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
if (!in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'])) {
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
}
}
}
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0) {
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
}
}
}
}
Expand All @@ -333,23 +373,28 @@ public function merge(PHP_CodeCoverage $that)
array_merge($this->filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles())
);

foreach ($that->data as $file => $lines) {
foreach ($that->getData() as $file => $fileData) {
if (!isset($this->data[$file])) {
if (!$this->filter->isFiltered($file)) {
$this->data[$file] = $lines;
if (!$that->filter()->isFiltered($file)) {
$this->data[$file] = $fileData;
}

continue;
}

foreach ($lines as $line => $data) {
foreach ($fileData['lines'] as $line => $data) {
if ($data !== null) {
if (!isset($this->data[$file][$line])) {
$this->data[$file][$line] = $data;
} else {
$this->data[$file][$line] = array_unique(
array_merge($this->data[$file][$line], $data)
);
if ($this->pathCoverage) {
if (!isset($this->data[$file]['lines'][$line])) {
$this->data[$file]['lines'][$line] = $data;
} else {
if ($data['pathCovered']) {
$this->data[$file]['lines'][$line]['pathCovered'] = $data['pathCovered'];
}
$this->data[$file]['lines'][$line]['tests'] = array_unique(
array_merge($this->data[$file]['lines'][$line]['tests'], $data['tests'])
);
}
}
}
}
Expand Down Expand Up @@ -486,7 +531,10 @@ private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, ar
{
if ($linesToBeCovered === false ||
($this->forceCoversAnnotation && empty($linesToBeCovered))) {
$data = [];
$data = [
'lines' => [],
'functions' => [],
];

return;
}
Expand All @@ -508,8 +556,8 @@ private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, ar
foreach (array_keys($data) as $filename) {
$_linesToBeCovered = array_flip($linesToBeCovered[$filename]);

$data[$filename] = array_intersect_key(
$data[$filename],
$data[$filename]['lines'] = array_intersect_key(
$data[$filename]['lines'],
$_linesToBeCovered
);
}
Expand Down Expand Up @@ -542,7 +590,7 @@ private function applyIgnoredLinesFilter(array &$data)
}

foreach ($this->getLinesToBeIgnored($filename) as $line) {
unset($data[$filename][$line]);
unset($data[$filename]['lines'][$line]);
}
}
}
Expand All @@ -553,12 +601,46 @@ private function applyIgnoredLinesFilter(array &$data)
*/
private function initializeFilesThatAreSeenTheFirstTime(array $data)
{
foreach ($data as $file => $lines) {
if ($this->filter->isFile($file) && !isset($this->data[$file])) {
$this->data[$file] = [];
foreach ($data as $file => $fileData) {
if (!$this->filter->isFile($file) || isset($this->data[$file])) {
continue;
}

foreach ($lines as $k => $v) {
$this->data[$file][$k] = $v == -2 ? null : [];
$this->data[$file] = ['lines' => []];

foreach ($fileData['lines'] as $lineNumber => $flag) {
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
$this->data[$file]['lines'][$lineNumber] = null;
} else {
$this->data[$file]['lines'][$lineNumber] = [
'pathCovered' => false,
'tests' => [],
];
}
}

if ($this->pathCoverage) {
$this->data[$file]['branches'] = [];
$this->data[$file]['paths'] = [];

foreach ($fileData['functions'] as $functionName => $functionData) {
$this->data[$file]['branches'][$functionName] = [];
$this->data[$file]['paths'][$functionName] = $functionData['paths'];

foreach ($functionData['branches'] as $index => $branch) {
$this->data[$file]['branches'][$functionName][$index] = [
'hit' => $branch['hit'],
'line_start' => $branch['line_start'],
'line_end' => $branch['line_end'],
'tests' => []
];

for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
if (isset($this->data[$file]['lines'][$i])) {
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
}
}
}
}
}
}
Expand Down Expand Up @@ -587,12 +669,15 @@ private function addUncoveredFilesFromWhitelist()
$uncoveredFiles
);
} else {
$data[$uncoveredFile] = [];
$data[$uncoveredFile] = [
'lines' => [],
'functions' => [],
];

$lines = count(file($uncoveredFile));

for ($i = 1; $i <= $lines; $i++) {
$data[$uncoveredFile][$i] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
$data[$uncoveredFile]['lines'][$i] = PHP_CodeCoverage_Driver::LINE_NOT_EXECUTED;
}
}
}
Expand Down Expand Up @@ -815,8 +900,8 @@ private function performUnintentionallyCoveredCodeCheck(array &$data, array $lin

$message = '';

foreach ($data as $file => $_data) {
foreach ($_data as $line => $flag) {
foreach ($data as $file => $fileData) {
foreach ($fileData['lines'] as $line => $flag) {
if ($flag == 1 &&
(!isset($allowedLines[$file]) ||
!isset($allowedLines[$file][$line]))) {
Expand Down Expand Up @@ -878,10 +963,11 @@ private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed)
}

/**
* @param bool $pathCoverage
* @return PHP_CodeCoverage_Driver
* @throws PHP_CodeCoverage_RuntimeException
*/
private function selectDriver()
private function selectDriver($pathCoverage)
{
$runtime = new Runtime;

Expand All @@ -894,7 +980,7 @@ private function selectDriver()
} elseif ($runtime->isPHPDBG()) {
return new PHP_CodeCoverage_Driver_PHPDBG;
} else {
return new PHP_CodeCoverage_Driver_Xdebug;
return new PHP_CodeCoverage_Driver_Xdebug($pathCoverage);
}
}
}
39 changes: 31 additions & 8 deletions src/CodeCoverage/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,44 @@
class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
{
/**
* Constructor.
* @var int
*/
public function __construct()
private $flags;

/**
* @param bool $pathCoverage
*/
public function __construct($pathCoverage = false)
{
if (!extension_loaded('xdebug')) {
throw new PHP_CodeCoverage_RuntimeException('This driver requires Xdebug');
}

if (version_compare(phpversion('xdebug'), '2.2.0-dev', '>=') &&
!ini_get('xdebug.coverage_enable')) {
if (!ini_get('xdebug.coverage_enable')) {
throw new PHP_CodeCoverage_RuntimeException(
'xdebug.coverage_enable=On has to be set in php.ini'
);
}

$this->flags = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;

if ($pathCoverage) {
if (version_compare(phpversion('xdebug'), '2.3.2', '<')) {
throw new PHP_CodeCoverage_RuntimeException(
'Path coverage requires Xdebug 2.3.2 (or newer)'
);
}

$this->flags |= XDEBUG_CC_BRANCH_CHECK;
}
}

/**
* Start collection of code coverage information.
*/
public function start()
{
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
xdebug_start_code_coverage($this->flags);
}

/**
Expand All @@ -62,14 +78,21 @@ public function stop()
private function cleanup(array $data)
{
foreach (array_keys($data) as $file) {
unset($data[$file][0]);
if (!isset($data[$file]['lines'])) {
$data[$file] = ['lines' => $data[$file]];
}
if (!isset($data[$file]['functions'])) {
$data[$file]['functions'] = [];
}

unset($data[$file]['lines'][0]);

if ($file != 'xdebug://debug-eval' && file_exists($file)) {
$numLines = $this->getNumberOfLinesInFile($file);

foreach (array_keys($data[$file]) as $line) {
foreach (array_keys($data[$file]['lines']) as $line) {
if ($line > $numLines) {
unset($data[$file][$line]);
unset($data[$file]['lines'][$line]);
}
}
}
Expand Down
Loading