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

Feature/branch coverage #400

Closed
wants to merge 18 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
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
145 changes: 110 additions & 35 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,32 @@ class PHP_CodeCoverage
/**
* 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;
}

/**
Expand Down Expand Up @@ -307,15 +317,37 @@ 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 (!in_array($id, $lineData['tests'])) {
$lineData['tests'][] = $id;
}
}
}

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,22 +365,25 @@ 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;
if (!isset($this->data[$file]['lines'][$line])) {
$this->data[$file]['lines'][$line] = $data;
} else {
$this->data[$file][$line] = array_unique(
array_merge($this->data[$file][$line], $data)
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 +521,10 @@ private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, ar
{
if ($linesToBeCovered === false ||
($this->forceCoversAnnotation && empty($linesToBeCovered))) {
$data = [];
$data = [
'lines' => [],
'functions' => [],
];

return;
}
Expand All @@ -508,8 +546,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 +580,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 +591,45 @@ 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' => [],
'branches' =>[],
'paths' => [],
];

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' => [],
];
}
}

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 +658,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 +889,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 +952,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 +969,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);
}
}
}
36 changes: 30 additions & 6 deletions src/CodeCoverage/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
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');
Expand All @@ -31,14 +36,26 @@ public function __construct()
'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 +79,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
21 changes: 11 additions & 10 deletions src/CodeCoverage/Report/Clover.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
for ($i = $method['startLine'];
$i <= $method['endLine'];
$i++) {
if (isset($coverage[$i]) && ($coverage[$i] !== null)) {
$methodCount = max($methodCount, count($coverage[$i]));
if (isset($coverage['lines'][$i])) {
$methodCount = max($methodCount, count($coverage['lines'][$i]['tests']));
}
}

Expand Down Expand Up @@ -135,8 +135,8 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
$xmlMetrics->setAttribute('complexity', $class['ccn']);
$xmlMetrics->setAttribute('methods', $classMethods);
$xmlMetrics->setAttribute('coveredmethods', $coveredMethods);
$xmlMetrics->setAttribute('conditionals', 0);
$xmlMetrics->setAttribute('coveredconditionals', 0);
$xmlMetrics->setAttribute('conditionals', $class['executablePaths']);
$xmlMetrics->setAttribute('coveredconditionals', $class['executedPaths']);
$xmlMetrics->setAttribute('statements', $classStatements);
$xmlMetrics->setAttribute(
'coveredstatements',
Expand All @@ -157,13 +157,14 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
$xmlClass->appendChild($xmlMetrics);
}

foreach ($coverage as $line => $data) {
foreach ($coverage['lines'] as $line => $data) {
if ($data === null || isset($lines[$line])) {
continue;
}

$lines[$line] = [
'count' => count($data), 'type' => 'stmt'
'count' => count($data['tests']),
'type' => 'stmt',
];
}

Expand Down Expand Up @@ -205,8 +206,8 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
'coveredmethods',
$item->getNumTestedMethods()
);
$xmlMetrics->setAttribute('conditionals', 0);
$xmlMetrics->setAttribute('coveredconditionals', 0);
$xmlMetrics->setAttribute('conditionals', $item->getNumExecutablePaths());
$xmlMetrics->setAttribute('coveredconditionals', $item->getNumExecutedPaths());
$xmlMetrics->setAttribute(
'statements',
$item->getNumExecutableLines()
Expand Down Expand Up @@ -258,8 +259,8 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
'coveredmethods',
$report->getNumTestedMethods()
);
$xmlMetrics->setAttribute('conditionals', 0);
$xmlMetrics->setAttribute('coveredconditionals', 0);
$xmlMetrics->setAttribute('conditionals', $report->getNumExecutablePaths());
$xmlMetrics->setAttribute('coveredconditionals', $report->getNumExecutedPaths());
$xmlMetrics->setAttribute(
'statements',
$report->getNumExecutableLines()
Expand Down
Loading