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 Xdebug's code coverage functionality.
14 * @since Class available since Release 1.0.0
17 class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
22 public function __construct()
24 if (!extension_loaded('xdebug')) {
25 throw new PHP_CodeCoverage_Exception('This driver requires Xdebug');
28 if (version_compare(phpversion('xdebug'), '2.2.0-dev', '>=') &&
29 !ini_get('xdebug.coverage_enable')) {
30 throw new PHP_CodeCoverage_Exception(
31 'xdebug.coverage_enable=On has to be set in php.ini'
37 * Start collection of code coverage information.
39 public function start()
41 xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
45 * Stop collection of code coverage information.
49 public function stop()
51 $data = xdebug_get_code_coverage();
52 xdebug_stop_code_coverage();
54 return $this->cleanup($data);
60 * @since Method available since Release 2.0.0
62 private function cleanup(array $data)
64 foreach (array_keys($data) as $file) {
65 unset($data[$file][0]);
67 if ($file != 'xdebug://debug-eval' && file_exists($file)) {
68 $numLines = $this->getNumberOfLinesInFile($file);
70 foreach (array_keys($data[$file]) as $line) {
71 if (isset($data[$file][$line]) && $line > $numLines) {
72 unset($data[$file][$line]);
84 * @since Method available since Release 2.0.0
86 private function getNumberOfLinesInFile($file)
88 $buffer = file_get_contents($file);
89 $lines = substr_count($buffer, "\n");
91 if (substr($buffer, -1) !== "\n") {