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

PublicVisibility method issue in Traits that are used in classes with a matching Interface method. #1834

Open
paul-crashley opened this issue Mar 10, 2023 · 2 comments
Labels

Comments

@paul-crashley
Copy link

paul-crashley commented Mar 10, 2023

Question Answer
Infection version 0.26.19
Test Framework version PHPUnit 9.6.3
PHP version 8.1.15
Platform Ubuntu/Windows/MacOS
Github Repo -

Apologies if this has been asked before, but I couldn't find another example, or figure out a way around it.

Essentially, if I have the following interface, trait and class:

interface HasID
{
    public function getId(): ?int;
}

trait WithID
{
    private ?int $id;

    public function getId(): ?int
    {
        return $this->id;
    }
}

class SourceClass implements HasID
{
    use WithID;

    public function __construct(?int $id = null)
    {
        $this->id = $id;
    }
}

And I have the following tests to cover both the trait and the class:

class WithIDTest extends TestCase
{
    public function test_trait_method(): void
    {
        $source = new class() {
            use WithID;
        };

        self::assertNull($source->getId());
    }
}

class SourceClassTest extends TestCase
{
    public function test_get_id(): void
    {
        $source = new SourceClass(99);

        self::assertSame(99, $source->getId());
    }
}

When I run infection, despite having coverage of the public getID method being used, infection still tries to change the visibility of the method in the trait to protected, which then causes a fatal error because it no longer matches the interface.

Fatal error: Access level to Infected\SourceClass::getId() must be public (as in class Infected\HasID)

Is there a way around this?

Playground version is available here: https://infection-php.dev/r/wmz2

@sanmai sanmai added the Bug label Mar 11, 2023
@sanmai
Copy link
Member

sanmai commented Mar 11, 2023

To be clear, this doesn't affect the scores, right?

@paul-crashley
Copy link
Author

To be clear, this doesn't affect the scores, right?

No effect to scores, no.

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

No branches or pull requests

2 participants