Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single line method is ignored #1020

Closed
mvorisek opened this issue Nov 20, 2023 · 5 comments · Fixed by #1021
Closed

Single line method is ignored #1020

mvorisek opened this issue Nov 20, 2023 · 5 comments · Fixed by #1021

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Nov 20, 2023

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
Copy link
Owner

CC @Slamdunk

@Slamdunk
Copy link
Contributor

Slamdunk commented Nov 21, 2023

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 🤷

@sebastianbergmann
Copy link
Owner

@Slamdunk Thank you!

@mvorisek
Copy link
Contributor Author

mvorisek commented Nov 21, 2023

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
Copy link
Contributor

Slamdunk commented Nov 21, 2023

@mvorisek now it makes sense: fix proposed in #1021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants