Skip to content

Commit 4163491

Browse files
Require xdebug_info('mode') to work when Xdebug is used
1 parent b919664 commit 4163491

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

ChangeLog-12.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1313
* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
1414
* Optional argument `$linesToBeUsed` of `CodeCoverage::stop()` and `CodeCoverage::append()` methods
1515
* This component is no longer supported on PHP 8.2
16+
* This component no longer supports Xdebug versions before Xdebug 3.1
1617

1718
[12.0.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/11.0...main

src/Driver/Selector.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class Selector
2121
* @throws PcovNotAvailableException
2222
* @throws XdebugNotAvailableException
2323
* @throws XdebugNotEnabledException
24+
* @throws XdebugVersionNotSupportedException
2425
*/
2526
public function forLineCoverage(Filter $filter): Driver
2627
{
@@ -45,6 +46,7 @@ public function forLineCoverage(Filter $filter): Driver
4546
* @throws NoCodeCoverageDriverWithPathCoverageSupportAvailableException
4647
* @throws XdebugNotAvailableException
4748
* @throws XdebugNotEnabledException
49+
* @throws XdebugVersionNotSupportedException
4850
*/
4951
public function forLineAndPathCoverage(Filter $filter): Driver
5052
{

src/Driver/XdebugDriver.php

+6-24
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
use const XDEBUG_CC_UNUSED;
1515
use const XDEBUG_FILTER_CODE_COVERAGE;
1616
use const XDEBUG_PATH_INCLUDE;
17-
use function explode;
1817
use function extension_loaded;
19-
use function getenv;
2018
use function in_array;
21-
use function ini_get;
2219
use function phpversion;
2320
use function version_compare;
2421
use function xdebug_get_code_coverage;
@@ -65,11 +62,11 @@ final class XdebugDriver extends Driver
6562
/**
6663
* @throws XdebugNotAvailableException
6764
* @throws XdebugNotEnabledException
65+
* @throws XdebugVersionNotSupportedException
6866
*/
6967
public function __construct(Filter $filter)
7068
{
7169
$this->ensureXdebugIsAvailable();
72-
$this->ensureXdebugCodeCoverageFeatureIsEnabled();
7370

7471
if (!$filter->isEmpty()) {
7572
xdebug_set_filter(
@@ -127,35 +124,20 @@ public function nameAndVersion(): string
127124

128125
/**
129126
* @throws XdebugNotAvailableException
127+
* @throws XdebugNotEnabledException
128+
* @throws XdebugVersionNotSupportedException
130129
*/
131130
private function ensureXdebugIsAvailable(): void
132131
{
133132
if (!extension_loaded('xdebug')) {
134133
throw new XdebugNotAvailableException;
135134
}
136-
}
137-
138-
/**
139-
* @throws XdebugNotEnabledException
140-
*/
141-
private function ensureXdebugCodeCoverageFeatureIsEnabled(): void
142-
{
143-
if (version_compare(phpversion('xdebug'), '3.1', '>=')) {
144-
if (!in_array('coverage', xdebug_info('mode'), true)) {
145-
throw new XdebugNotEnabledException;
146-
}
147-
148-
return;
149-
}
150-
151-
$mode = getenv('XDEBUG_MODE');
152135

153-
if ($mode === false || $mode === '') {
154-
$mode = ini_get('xdebug.mode');
136+
if (!version_compare(phpversion('xdebug'), '3.1', '>=')) {
137+
throw new XdebugVersionNotSupportedException(phpversion('xdebug'));
155138
}
156139

157-
if ($mode === false ||
158-
!in_array('coverage', explode(',', $mode), true)) {
140+
if (!in_array('coverage', xdebug_info('mode'), true)) {
159141
throw new XdebugNotEnabledException;
160142
}
161143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
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 function sprintf;
13+
use RuntimeException;
14+
use SebastianBergmann\CodeCoverage\Exception;
15+
16+
final class XdebugVersionNotSupportedException extends RuntimeException implements Exception
17+
{
18+
/**
19+
* @param non-empty-string $version
20+
*/
21+
public function __construct(string $version)
22+
{
23+
parent::__construct(
24+
sprintf(
25+
'Version %s of the Xdebug extension is not supported',
26+
$version,
27+
),
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)