3 * This file is part of the PHP_CodeCoverage package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Driver for PHPDBG's code coverage functionality.
14 * @since Class available since Release 2.2.0
17 class PHP_CodeCoverage_Driver_PHPDBG implements PHP_CodeCoverage_Driver
22 public function __construct()
24 if (PHP_SAPI !== 'phpdbg') {
25 throw new PHP_CodeCoverage_Exception(
26 'This driver requires the PHPDBG SAPI'
30 if (!function_exists('phpdbg_start_oplog')) {
31 throw new PHP_CodeCoverage_Exception(
32 'This build of PHPDBG does not support code coverage'
38 * Start collection of code coverage information.
40 public function start()
46 * Stop collection of code coverage information.
50 public function stop()
52 static $fetchedLines = array();
54 $dbgData = phpdbg_end_oplog();
56 if ($fetchedLines == array()) {
57 $sourceLines = phpdbg_get_executable();
59 $newFiles = array_diff(
61 array_keys($fetchedLines)
65 $sourceLines = phpdbg_get_executable(
66 array('files' => $newFiles)
69 $sourceLines = array();
73 foreach ($sourceLines as $file => $lines) {
74 foreach ($lines as $lineNo => $numExecuted) {
75 $sourceLines[$file][$lineNo] = self::LINE_NOT_EXECUTED;
79 $fetchedLines = array_merge($fetchedLines, $sourceLines);
81 return $this->detectExecutedLines($fetchedLines, $dbgData);
85 * Convert phpdbg based data into the format CodeCoverage expects
87 * @param array $sourceLines
88 * @param array $dbgData
91 private function detectExecutedLines(array $sourceLines, array $dbgData)
93 foreach ($dbgData as $file => $coveredLines) {
94 foreach ($coveredLines as $lineNo => $numExecuted) {
95 // phpdbg also reports $lineNo=0 when e.g. exceptions get thrown.
96 // make sure we only mark lines executed which are actually executable.
97 if (isset($sourceLines[$file][$lineNo])) {
98 $sourceLines[$file][$lineNo] = self::LINE_EXECUTED;