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.76k stars 370 forks source link

Single line method is ignored #1020

Closed mvorisek closed 8 months ago

mvorisek commented 8 months ago
Q A
php-code-coverage version 9.2.29
PHP version 8.2.12
Driver Xdebug
PCOV version (if used) n/a
Xdebug version (if used) 3.2.2
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.6.13

repro code:

class InitializerTraitTest extends TestCase
{
    public function testInitDeclaredPublicException(): void
    {
        $m = new class() extends AbstractInitializerMock {
            public function init(): void {}
        };
    }
}

(full source code: https://github.com/atk4/core/blob/5.0.0/tests/InitializerTraitTest.php#L72)

when the code above is modified like:

+            #[\Override]
             public function init(): void {}

then the coverage is collected. It seems this lib does not account for single line methods (methods starting and ending at the same line, even a method attribute on another line is enough to enable coverage).

image

sebastianbergmann commented 8 months ago

CC @Slamdunk

Slamdunk commented 8 months ago

Looking at here:

https://github.com/atk4/core/blob/9bc701001c6c7959d12b1a66cdeea5d618735abb/src/InitializerTrait.php#L46-L50

It seems to me that the CC is correctly reported both times:

  1. when the method is public, init() is not called so it's red
  2. when the method is protected, init() is called so it's green

The #[\Override] attribute has nothing to do with this.

I see no bug here :shrug:

sebastianbergmann commented 8 months ago

@Slamdunk Thank you!

mvorisek commented 7 months ago

Looking at here:

https://github.com/atk4/core/blob/9bc701001c6c7959d12b1a66cdeea5d618735abb/src/InitializerTrait.php#L46-L50

It seems to me that the CC is correctly reported both times:

1. when the method is `public`, `init()` is **not** called so it's red

2. when the method is `protected`, `init()` **is** called so it's green

The #[\Override] attribute has nothing to do with this.

I see no bug here 🤷

No, this is not what the issue is about.

see https://app.codecov.io/gh/atk4/core/blob/develop/tests%2FInitializerTraitTest.php#L50 and https://app.codecov.io/gh/atk4/core/blob/develop/tests%2FInitializerTraitTest.php#L72

image

when the method is defined as:

protected function init(): void {}

there is simply no red/green coverage at all, ie. coverage is not measured

when the method is reformatted to:

protected function init(): void
{
}

the coverage is then measured as expected, but the first format should be used per https://www.php-fig.org/per/coding-style/#4-classes-properties-and-methods

Slamdunk commented 7 months ago

@mvorisek now it makes sense: fix proposed in https://github.com/sebastianbergmann/php-code-coverage/pull/1021