sebastianbergmann / php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.
BSD 3-Clause "New" or "Revised" License
8.81k stars 373 forks source link

Argument `$linesToBeIgnored` of `CodeCoverage::stop()` has no effect for files that are not executed at all #994

Closed sebastianbergmann closed 1 year ago

sebastianbergmann commented 1 year ago

src/A.php

<?php declare(strict_types=1);
final class A
{
    public function doSomething(): bool
    {
        return true;
    }
}

src/B.php

<?php declare(strict_types=1);
final class B
{
    public function doSomething(): bool
    {
        return true;
    }
}

test.php

<?php declare(strict_types=1);
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;

require __DIR__ . '/vendor/autoload.php';

$filter = new Filter;
$filter->includeFiles(
    [
        __DIR__ . '/src/A.php',
        __DIR__ . '/src/B.php',
    ]
);

$coverage = new CodeCoverage(
    (new Selector)->forLineCoverage($filter),
    $filter
);

$coverage->start('test');

$a = new A;
$a->doSomething();

#$b = new B;
#$b->doSomething();

$coverage->stop(
    linesToBeIgnored: [
        __DIR__ . '/src/B.php' => range(1, 8)
    ]
);

(new HtmlReport)->process($coverage, '/tmp/code-coverage');

HTML Code Coverage Report

grafik

When

#$b = new B;
#$b->doSomething();

is changed to

$b = new B;
$b->doSomething();

then the lines of code that make up class B are ignored as requested:

grafik