Skip to content

Added a phpdbg based code coverage driver. #360

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

Closed
wants to merge 5 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
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"phpunit/php-file-iterator": "~1.3",
"phpunit/php-token-stream": "~1.3",
"phpunit/php-text-template": "~1.2",
"sebastian/environment": "~1.0",
"sebastian/environment": "dev-phpdbg_cc",
"sebastian/version": "~1.0"
},
"require-dev": {
Expand All @@ -46,5 +46,12 @@
"branch-alias": {
"dev-master": "2.2.x-dev"
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/staabm/environment"
}
]

}
14 changes: 9 additions & 5 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ class PHP_CodeCoverage
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null)
{
if ($driver === null) {
$runtime = new Runtime;
if (PHP_SAPI === 'phpdbg') {
$driver = new PHP_CodeCoverage_Driver_Phpdbg;
} else {
$runtime = new Runtime;

if (!$runtime->hasXdebug()) {
throw new PHP_CodeCoverage_Exception('No code coverage driver available');
}
if (!$runtime->hasXdebug()) {
throw new PHP_CodeCoverage_Exception('No code coverage driver available');
}

$driver = new PHP_CodeCoverage_Driver_Xdebug;
$driver = new PHP_CodeCoverage_Driver_Xdebug;
}
}

if ($filter === null) {
Expand Down
6 changes: 6 additions & 0 deletions src/CodeCoverage/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
interface PHP_CodeCoverage_Driver
{
// const values map to the defaults provided by xdebug-code-coverage
// see http://xdebug.org/docs/code_coverage
const LINE_EXECUTED = 1;
const LINE_NOT_EXECUTED = -1;
const LINE_NOT_EXECUTABLE = -2;

/**
* Start collection of code coverage information.
*/
Expand Down
96 changes: 96 additions & 0 deletions src/CodeCoverage/Driver/Phpdbg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the PHP_CodeCoverage package.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Driver for Phpdbg's code coverage functionality.
*
* @since Class available since Release 2.2.0
* @codeCoverageIgnore
*/
class PHP_CodeCoverage_Driver_Phpdbg implements PHP_CodeCoverage_Driver
{
/**
* Constructor.
*/
public function __construct()
{
if (PHP_SAPI !== 'phpdbg') {
throw new PHP_CodeCoverage_Exception('This driver requires the phpdbg sapi');
}

if (version_compare(phpversion(), '7.0', '<')) {
// actually we require the phpdbg version shipped with php7, not php7 itself
throw new PHP_CodeCoverage_Exception(
'phpdbg based code coverage requires at least php7'
);
}
}

/**
* Start collection of code coverage information.
*/
public function start()
{
phpdbg_start_oplog();
}

/**
* Stop collection of code coverage information.
*
* @return array
*/
public function stop()
{
static $fetchedLines = array();

$dbgData = phpdbg_end_oplog();

if ($fetchedLines == array()) {
$sourceLines = phpdbg_get_executable();
} else {
$newFiles = array_diff(get_included_files(), array_keys($fetchedLines));
if ($newFiles) {
$sourceLines = phpdbg_get_executable(array("files" => $newFiles));
} else {
$sourceLines = array();
}
}

foreach ($sourceLines as &$lines) {
foreach ($lines as &$line) {
$line = self::LINE_NOT_EXECUTED;
}
}

$fetchedLines += $sourceLines;

$data = $this->detectExecutedLines($fetchedLines, $dbgData);

return $data;
}

/**
* Convert phpdbg based data into the format CodeCoverage expects
*
* @param array $sourceLines
* @param array $dbgData
* @return array
*/
private function detectExecutedLines(array $sourceLines, array $dbgData)
{
foreach ($dbgData as $file => $coveredLines) {
foreach ($coveredLines as $lineNo => $numExecuted) {
$sourceLines[$file][$lineNo] = self::LINE_EXECUTED;
}
}

return $sourceLines;
}
}