Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * This file is part of the PHP_CodeCoverage package.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Driver for Xdebug's code coverage functionality.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @since Class available since Release 1.0.0
|
Chris@0
|
15 * @codeCoverageIgnore
|
Chris@0
|
16 */
|
Chris@0
|
17 class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
|
Chris@0
|
18 {
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Constructor.
|
Chris@0
|
21 */
|
Chris@0
|
22 public function __construct()
|
Chris@0
|
23 {
|
Chris@0
|
24 if (!extension_loaded('xdebug')) {
|
Chris@0
|
25 throw new PHP_CodeCoverage_Exception('This driver requires Xdebug');
|
Chris@0
|
26 }
|
Chris@0
|
27
|
Chris@0
|
28 if (version_compare(phpversion('xdebug'), '2.2.0-dev', '>=') &&
|
Chris@0
|
29 !ini_get('xdebug.coverage_enable')) {
|
Chris@0
|
30 throw new PHP_CodeCoverage_Exception(
|
Chris@0
|
31 'xdebug.coverage_enable=On has to be set in php.ini'
|
Chris@0
|
32 );
|
Chris@0
|
33 }
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Start collection of code coverage information.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function start()
|
Chris@0
|
40 {
|
Chris@0
|
41 xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Stop collection of code coverage information.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @return array
|
Chris@0
|
48 */
|
Chris@0
|
49 public function stop()
|
Chris@0
|
50 {
|
Chris@0
|
51 $data = xdebug_get_code_coverage();
|
Chris@0
|
52 xdebug_stop_code_coverage();
|
Chris@0
|
53
|
Chris@0
|
54 return $this->cleanup($data);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * @param array $data
|
Chris@0
|
59 * @return array
|
Chris@0
|
60 * @since Method available since Release 2.0.0
|
Chris@0
|
61 */
|
Chris@0
|
62 private function cleanup(array $data)
|
Chris@0
|
63 {
|
Chris@0
|
64 foreach (array_keys($data) as $file) {
|
Chris@0
|
65 unset($data[$file][0]);
|
Chris@0
|
66
|
Chris@0
|
67 if ($file != 'xdebug://debug-eval' && file_exists($file)) {
|
Chris@0
|
68 $numLines = $this->getNumberOfLinesInFile($file);
|
Chris@0
|
69
|
Chris@0
|
70 foreach (array_keys($data[$file]) as $line) {
|
Chris@0
|
71 if (isset($data[$file][$line]) && $line > $numLines) {
|
Chris@0
|
72 unset($data[$file][$line]);
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 return $data;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * @param string $file
|
Chris@0
|
83 * @return int
|
Chris@0
|
84 * @since Method available since Release 2.0.0
|
Chris@0
|
85 */
|
Chris@0
|
86 private function getNumberOfLinesInFile($file)
|
Chris@0
|
87 {
|
Chris@0
|
88 $buffer = file_get_contents($file);
|
Chris@0
|
89 $lines = substr_count($buffer, "\n");
|
Chris@0
|
90
|
Chris@0
|
91 if (substr($buffer, -1) !== "\n") {
|
Chris@0
|
92 $lines++;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 return $lines;
|
Chris@0
|
96 }
|
Chris@0
|
97 }
|