Skip to content

Commit

Permalink
Simplify (and always enable Xdebug dead code detection)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 14, 2025
1 parent 516c847 commit d4679aa
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 126 deletions.
1 change: 1 addition & 0 deletions ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
### Removed

* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
* Method `CodeCoverage::detectsDeadCode()`
* Optional argument `$linesToBeUsed` of `CodeCoverage::stop()` and `CodeCoverage::append()` methods
* This component is no longer supported on PHP 8.2
* This component no longer supports Xdebug versions before Xdebug 3.1
Expand Down
5 changes: 0 additions & 5 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,6 @@ public function collectsBranchAndPathCoverage(): bool
return $this->driver->collectsBranchAndPathCoverage();
}

public function detectsDeadCode(): bool
{
return $this->driver->detectsDeadCode();
}

public function validate(TargetCollection $targets): ValidationResult
{
return (new TargetCollectionValidator)->validate($this->targetMapper(), $targets);
Expand Down
34 changes: 0 additions & 34 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function sprintf;
use SebastianBergmann\CodeCoverage\BranchAndPathCoverageNotSupportedException;
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
use SebastianBergmann\CodeCoverage\DeadCodeDetectionNotSupportedException;

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

public function canCollectBranchAndPathCoverage(): bool
{
Expand Down Expand Up @@ -88,38 +86,6 @@ public function disableBranchAndPathCoverage(): void
$this->collectBranchAndPathCoverage = false;
}

public function canDetectDeadCode(): bool
{
return false;
}

public function detectsDeadCode(): bool
{
return $this->detectDeadCode;
}

/**
* @throws DeadCodeDetectionNotSupportedException
*/
public function enableDeadCodeDetection(): void
{
if (!$this->canDetectDeadCode()) {
throw new DeadCodeDetectionNotSupportedException(
sprintf(
'%s does not support dead code detection',
$this->nameAndVersion(),
),
);
}

$this->detectDeadCode = true;
}

public function disableDeadCodeDetection(): void
{
$this->detectDeadCode = false;
}

abstract public function nameAndVersion(): string;

abstract public function start(): void;
Expand Down
7 changes: 1 addition & 6 deletions src/Driver/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public function forLineCoverage(Filter $filter): Driver
}

if ($runtime->hasXdebug()) {
$driver = new XdebugDriver($filter);

$driver->enableDeadCodeDetection();

return $driver;
return new XdebugDriver($filter);
}

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

$driver->enableDeadCodeDetection();
$driver->enableBranchAndPathCoverage();

return $driver;
Expand Down
11 changes: 1 addition & 10 deletions src/Driver/XdebugDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,9 @@ public function canCollectBranchAndPathCoverage(): bool
return true;
}

public function canDetectDeadCode(): bool
{
return true;
}

public function start(): void
{
$flags = XDEBUG_CC_UNUSED;

if ($this->detectsDeadCode() || $this->collectsBranchAndPathCoverage()) {
$flags |= XDEBUG_CC_DEAD_CODE;
}
$flags = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;

if ($this->collectsBranchAndPathCoverage()) {
$flags |= XDEBUG_CC_BRANCH_CHECK;
Expand Down
16 changes: 0 additions & 16 deletions src/Exception/DeadCodeDetectionNotSupportedException.php

This file was deleted.

27 changes: 0 additions & 27 deletions tests/tests/Driver/PcovDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function extension_loaded;
use function ini_get;
use SebastianBergmann\CodeCoverage\BranchAndPathCoverageNotSupportedException;
use SebastianBergmann\CodeCoverage\DeadCodeDetectionNotSupportedException;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\TestCase;

Expand Down Expand Up @@ -60,32 +59,6 @@ public function testBranchAndPathCoverageIsNotCollected(): void
$this->assertFalse($this->driver()->collectsBranchAndPathCoverage());
}

public function testDoesNotSupportDeadCodeDetection(): void
{
$this->assertFalse($this->driver()->canDetectDeadCode());
}

public function testDeadCodeDetectionCanBeDisabled(): void
{
$driver = $this->driver();

$driver->disableDeadCodeDetection();

$this->assertFalse($driver->detectsDeadCode());
}

public function testDeadCodeDetectionCannotBeEnabled(): void
{
$this->expectException(DeadCodeDetectionNotSupportedException::class);

$this->driver()->enableDeadCodeDetection();
}

public function testDeadCodeIsNotDetected(): void
{
$this->assertFalse($this->driver()->detectsDeadCode());
}

public function testHasNameAndVersion(): void
{
$this->assertStringMatchesFormat('PCOV %s', $this->driver()->nameAndVersion());
Expand Down
28 changes: 0 additions & 28 deletions tests/tests/Driver/XdebugDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,6 @@ public function testBranchAndPathCoverageIsNotCollectedByDefault(): void
$this->assertFalse($this->driver()->collectsBranchAndPathCoverage());
}

public function testSupportsDeadCodeDetection(): void
{
$this->assertTrue($this->driver()->canDetectDeadCode());
}

public function testDeadCodeDetectionCanBeDisabled(): void
{
$driver = $this->driver();

$driver->disableDeadCodeDetection();

$this->assertFalse($driver->detectsDeadCode());
}

public function testDeadCodeDetectionCanBeEnabled(): void
{
$driver = $this->driver();

$driver->enableDeadCodeDetection();

$this->assertTrue($driver->detectsDeadCode());
}

public function testDeadCodeIsNotDetectedByDefault(): void
{
$this->assertFalse($this->driver()->detectsDeadCode());
}

public function testHasNameAndVersion(): void
{
$this->assertStringMatchesFormat('Xdebug %s', $this->driver()->nameAndVersion());
Expand Down

0 comments on commit d4679aa

Please sign in to comment.