| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of phpunit/php-code-coverage.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace SebastianBergmann\CodeCoverage\Driver;
|
|
|
11 |
|
|
|
12 |
use const XDEBUG_CC_BRANCH_CHECK;
|
|
|
13 |
use const XDEBUG_CC_DEAD_CODE;
|
|
|
14 |
use const XDEBUG_CC_UNUSED;
|
|
|
15 |
use const XDEBUG_FILTER_CODE_COVERAGE;
|
|
|
16 |
use const XDEBUG_PATH_INCLUDE;
|
|
|
17 |
use const XDEBUG_PATH_WHITELIST;
|
|
|
18 |
use function defined;
|
|
|
19 |
use function extension_loaded;
|
|
|
20 |
use function ini_get;
|
|
|
21 |
use function phpversion;
|
|
|
22 |
use function sprintf;
|
|
|
23 |
use function version_compare;
|
|
|
24 |
use function xdebug_get_code_coverage;
|
|
|
25 |
use function xdebug_set_filter;
|
|
|
26 |
use function xdebug_start_code_coverage;
|
|
|
27 |
use function xdebug_stop_code_coverage;
|
|
|
28 |
use SebastianBergmann\CodeCoverage\Filter;
|
|
|
29 |
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
|
|
33 |
*/
|
|
|
34 |
final class Xdebug2Driver extends Driver
|
|
|
35 |
{
|
|
|
36 |
/**
|
|
|
37 |
* @var bool
|
|
|
38 |
*/
|
|
|
39 |
private $pathCoverageIsMixedCoverage;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @throws WrongXdebugVersionException
|
|
|
43 |
* @throws Xdebug2NotEnabledException
|
|
|
44 |
* @throws XdebugNotAvailableException
|
|
|
45 |
*/
|
|
|
46 |
public function __construct(Filter $filter)
|
|
|
47 |
{
|
|
|
48 |
if (!extension_loaded('xdebug')) {
|
|
|
49 |
throw new XdebugNotAvailableException;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if (version_compare(phpversion('xdebug'), '3', '>=')) {
|
|
|
53 |
throw new WrongXdebugVersionException(
|
|
|
54 |
sprintf(
|
|
|
55 |
'This driver requires Xdebug 2 but version %s is loaded',
|
|
|
56 |
phpversion('xdebug')
|
|
|
57 |
)
|
|
|
58 |
);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if (!ini_get('xdebug.coverage_enable')) {
|
|
|
62 |
throw new Xdebug2NotEnabledException;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
if (!$filter->isEmpty()) {
|
|
|
66 |
if (defined('XDEBUG_PATH_WHITELIST')) {
|
|
|
67 |
$listType = XDEBUG_PATH_WHITELIST;
|
|
|
68 |
} else {
|
|
|
69 |
$listType = XDEBUG_PATH_INCLUDE;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
xdebug_set_filter(
|
|
|
73 |
XDEBUG_FILTER_CODE_COVERAGE,
|
|
|
74 |
$listType,
|
|
|
75 |
$filter->files()
|
|
|
76 |
);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$this->pathCoverageIsMixedCoverage = version_compare(phpversion('xdebug'), '2.9.6', '<');
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function canCollectBranchAndPathCoverage(): bool
|
|
|
83 |
{
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function canDetectDeadCode(): bool
|
|
|
88 |
{
|
|
|
89 |
return true;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public function start(): void
|
|
|
93 |
{
|
|
|
94 |
$flags = XDEBUG_CC_UNUSED;
|
|
|
95 |
|
|
|
96 |
if ($this->detectsDeadCode() || $this->collectsBranchAndPathCoverage()) {
|
|
|
97 |
$flags |= XDEBUG_CC_DEAD_CODE;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
if ($this->collectsBranchAndPathCoverage()) {
|
|
|
101 |
$flags |= XDEBUG_CC_BRANCH_CHECK;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
xdebug_start_code_coverage($flags);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public function stop(): RawCodeCoverageData
|
|
|
108 |
{
|
|
|
109 |
$data = xdebug_get_code_coverage();
|
|
|
110 |
|
|
|
111 |
xdebug_stop_code_coverage();
|
|
|
112 |
|
|
|
113 |
if ($this->collectsBranchAndPathCoverage()) {
|
|
|
114 |
if ($this->pathCoverageIsMixedCoverage) {
|
|
|
115 |
return RawCodeCoverageData::fromXdebugWithMixedCoverage($data);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return RawCodeCoverageData::fromXdebugWithPathCoverage($data);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
public function nameAndVersion(): string
|
|
|
125 |
{
|
|
|
126 |
return 'Xdebug ' . phpversion('xdebug');
|
|
|
127 |
}
|
|
|
128 |
}
|