| 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 function explode;
|
|
|
18 |
use function extension_loaded;
|
|
|
19 |
use function getenv;
|
|
|
20 |
use function in_array;
|
|
|
21 |
use function ini_get;
|
|
|
22 |
use function phpversion;
|
|
|
23 |
use function sprintf;
|
|
|
24 |
use function version_compare;
|
|
|
25 |
use function xdebug_get_code_coverage;
|
|
|
26 |
use function xdebug_set_filter;
|
|
|
27 |
use function xdebug_start_code_coverage;
|
|
|
28 |
use function xdebug_stop_code_coverage;
|
|
|
29 |
use SebastianBergmann\CodeCoverage\Filter;
|
|
|
30 |
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
|
|
34 |
*/
|
|
|
35 |
final class Xdebug3Driver extends Driver
|
|
|
36 |
{
|
|
|
37 |
/**
|
|
|
38 |
* @throws WrongXdebugVersionException
|
|
|
39 |
* @throws Xdebug3NotEnabledException
|
|
|
40 |
* @throws XdebugNotAvailableException
|
|
|
41 |
*/
|
|
|
42 |
public function __construct(Filter $filter)
|
|
|
43 |
{
|
|
|
44 |
if (!extension_loaded('xdebug')) {
|
|
|
45 |
throw new XdebugNotAvailableException;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if (version_compare(phpversion('xdebug'), '3', '<')) {
|
|
|
49 |
throw new WrongXdebugVersionException(
|
|
|
50 |
sprintf(
|
|
|
51 |
'This driver requires Xdebug 3 but version %s is loaded',
|
|
|
52 |
phpversion('xdebug')
|
|
|
53 |
)
|
|
|
54 |
);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
$mode = getenv('XDEBUG_MODE');
|
|
|
58 |
|
|
|
59 |
if ($mode === false || $mode === '') {
|
|
|
60 |
$mode = ini_get('xdebug.mode');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if ($mode === false ||
|
|
|
64 |
!in_array('coverage', explode(',', $mode), true)) {
|
|
|
65 |
throw new Xdebug3NotEnabledException;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if (!$filter->isEmpty()) {
|
|
|
69 |
xdebug_set_filter(
|
|
|
70 |
XDEBUG_FILTER_CODE_COVERAGE,
|
|
|
71 |
XDEBUG_PATH_INCLUDE,
|
|
|
72 |
$filter->files()
|
|
|
73 |
);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function canCollectBranchAndPathCoverage(): bool
|
|
|
78 |
{
|
|
|
79 |
return true;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function canDetectDeadCode(): bool
|
|
|
83 |
{
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function start(): void
|
|
|
88 |
{
|
|
|
89 |
$flags = XDEBUG_CC_UNUSED;
|
|
|
90 |
|
|
|
91 |
if ($this->detectsDeadCode() || $this->collectsBranchAndPathCoverage()) {
|
|
|
92 |
$flags |= XDEBUG_CC_DEAD_CODE;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($this->collectsBranchAndPathCoverage()) {
|
|
|
96 |
$flags |= XDEBUG_CC_BRANCH_CHECK;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
xdebug_start_code_coverage($flags);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function stop(): RawCodeCoverageData
|
|
|
103 |
{
|
|
|
104 |
$data = xdebug_get_code_coverage();
|
|
|
105 |
|
|
|
106 |
xdebug_stop_code_coverage();
|
|
|
107 |
|
|
|
108 |
if ($this->collectsBranchAndPathCoverage()) {
|
|
|
109 |
return RawCodeCoverageData::fromXdebugWithPathCoverage($data);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public function nameAndVersion(): string
|
|
|
116 |
{
|
|
|
117 |
return 'Xdebug ' . phpversion('xdebug');
|
|
|
118 |
}
|
|
|
119 |
}
|