Skip to content

Commit d4679aa

Browse files
Simplify (and always enable Xdebug dead code detection)
1 parent 516c847 commit d4679aa

8 files changed

+3
-126
lines changed

ChangeLog-12.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1111
### Removed
1212

1313
* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
14+
* Method `CodeCoverage::detectsDeadCode()`
1415
* Optional argument `$linesToBeUsed` of `CodeCoverage::stop()` and `CodeCoverage::append()` methods
1516
* This component is no longer supported on PHP 8.2
1617
* This component no longer supports Xdebug versions before Xdebug 3.1

src/CodeCoverage.php

-5
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,6 @@ public function collectsBranchAndPathCoverage(): bool
362362
return $this->driver->collectsBranchAndPathCoverage();
363363
}
364364

365-
public function detectsDeadCode(): bool
366-
{
367-
return $this->driver->detectsDeadCode();
368-
}
369-
370365
public function validate(TargetCollection $targets): ValidationResult
371366
{
372367
return (new TargetCollectionValidator)->validate($this->targetMapper(), $targets);

src/Driver/Driver.php

-34
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use function sprintf;
1313
use SebastianBergmann\CodeCoverage\BranchAndPathCoverageNotSupportedException;
1414
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
15-
use SebastianBergmann\CodeCoverage\DeadCodeDetectionNotSupportedException;
1615

1716
/**
1817
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
@@ -54,7 +53,6 @@ abstract class Driver
5453
*/
5554
public const int BRANCH_HIT = 1;
5655
private bool $collectBranchAndPathCoverage = false;
57-
private bool $detectDeadCode = false;
5856

5957
public function canCollectBranchAndPathCoverage(): bool
6058
{
@@ -88,38 +86,6 @@ public function disableBranchAndPathCoverage(): void
8886
$this->collectBranchAndPathCoverage = false;
8987
}
9088

91-
public function canDetectDeadCode(): bool
92-
{
93-
return false;
94-
}
95-
96-
public function detectsDeadCode(): bool
97-
{
98-
return $this->detectDeadCode;
99-
}
100-
101-
/**
102-
* @throws DeadCodeDetectionNotSupportedException
103-
*/
104-
public function enableDeadCodeDetection(): void
105-
{
106-
if (!$this->canDetectDeadCode()) {
107-
throw new DeadCodeDetectionNotSupportedException(
108-
sprintf(
109-
'%s does not support dead code detection',
110-
$this->nameAndVersion(),
111-
),
112-
);
113-
}
114-
115-
$this->detectDeadCode = true;
116-
}
117-
118-
public function disableDeadCodeDetection(): void
119-
{
120-
$this->detectDeadCode = false;
121-
}
122-
12389
abstract public function nameAndVersion(): string;
12490

12591
abstract public function start(): void;

src/Driver/Selector.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ public function forLineCoverage(Filter $filter): Driver
3232
}
3333

3434
if ($runtime->hasXdebug()) {
35-
$driver = new XdebugDriver($filter);
36-
37-
$driver->enableDeadCodeDetection();
38-
39-
return $driver;
35+
return new XdebugDriver($filter);
4036
}
4137

4238
throw new NoCodeCoverageDriverAvailableException;
@@ -53,7 +49,6 @@ public function forLineAndPathCoverage(Filter $filter): Driver
5349
if ((new Runtime)->hasXdebug()) {
5450
$driver = new XdebugDriver($filter);
5551

56-
$driver->enableDeadCodeDetection();
5752
$driver->enableBranchAndPathCoverage();
5853

5954
return $driver;

src/Driver/XdebugDriver.php

+1-10
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,9 @@ public function canCollectBranchAndPathCoverage(): bool
8282
return true;
8383
}
8484

85-
public function canDetectDeadCode(): bool
86-
{
87-
return true;
88-
}
89-
9085
public function start(): void
9186
{
92-
$flags = XDEBUG_CC_UNUSED;
93-
94-
if ($this->detectsDeadCode() || $this->collectsBranchAndPathCoverage()) {
95-
$flags |= XDEBUG_CC_DEAD_CODE;
96-
}
87+
$flags = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;
9788

9889
if ($this->collectsBranchAndPathCoverage()) {
9990
$flags |= XDEBUG_CC_BRANCH_CHECK;

src/Exception/DeadCodeDetectionNotSupportedException.php

-16
This file was deleted.

tests/tests/Driver/PcovDriverTest.php

-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function extension_loaded;
1414
use function ini_get;
1515
use SebastianBergmann\CodeCoverage\BranchAndPathCoverageNotSupportedException;
16-
use SebastianBergmann\CodeCoverage\DeadCodeDetectionNotSupportedException;
1716
use SebastianBergmann\CodeCoverage\Filter;
1817
use SebastianBergmann\CodeCoverage\TestCase;
1918

@@ -60,32 +59,6 @@ public function testBranchAndPathCoverageIsNotCollected(): void
6059
$this->assertFalse($this->driver()->collectsBranchAndPathCoverage());
6160
}
6261

63-
public function testDoesNotSupportDeadCodeDetection(): void
64-
{
65-
$this->assertFalse($this->driver()->canDetectDeadCode());
66-
}
67-
68-
public function testDeadCodeDetectionCanBeDisabled(): void
69-
{
70-
$driver = $this->driver();
71-
72-
$driver->disableDeadCodeDetection();
73-
74-
$this->assertFalse($driver->detectsDeadCode());
75-
}
76-
77-
public function testDeadCodeDetectionCannotBeEnabled(): void
78-
{
79-
$this->expectException(DeadCodeDetectionNotSupportedException::class);
80-
81-
$this->driver()->enableDeadCodeDetection();
82-
}
83-
84-
public function testDeadCodeIsNotDetected(): void
85-
{
86-
$this->assertFalse($this->driver()->detectsDeadCode());
87-
}
88-
8962
public function testHasNameAndVersion(): void
9063
{
9164
$this->assertStringMatchesFormat('PCOV %s', $this->driver()->nameAndVersion());

tests/tests/Driver/XdebugDriverTest.php

-28
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,6 @@ public function testBranchAndPathCoverageIsNotCollectedByDefault(): void
6666
$this->assertFalse($this->driver()->collectsBranchAndPathCoverage());
6767
}
6868

69-
public function testSupportsDeadCodeDetection(): void
70-
{
71-
$this->assertTrue($this->driver()->canDetectDeadCode());
72-
}
73-
74-
public function testDeadCodeDetectionCanBeDisabled(): void
75-
{
76-
$driver = $this->driver();
77-
78-
$driver->disableDeadCodeDetection();
79-
80-
$this->assertFalse($driver->detectsDeadCode());
81-
}
82-
83-
public function testDeadCodeDetectionCanBeEnabled(): void
84-
{
85-
$driver = $this->driver();
86-
87-
$driver->enableDeadCodeDetection();
88-
89-
$this->assertTrue($driver->detectsDeadCode());
90-
}
91-
92-
public function testDeadCodeIsNotDetectedByDefault(): void
93-
{
94-
$this->assertFalse($this->driver()->detectsDeadCode());
95-
}
96-
9769
public function testHasNameAndVersion(): void
9870
{
9971
$this->assertStringMatchesFormat('Xdebug %s', $this->driver()->nameAndVersion());

0 commit comments

Comments
 (0)