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

Incorrect comparison of inherited "static" types #11139

Open
mj4444ru opened this issue Oct 23, 2024 · 3 comments
Open

Incorrect comparison of inherited "static" types #11139

mj4444ru opened this issue Oct 23, 2024 · 3 comments

Comments

@mj4444ru
Copy link

https://psalm.dev/r/e205e7be5f
https://psalm.dev/r/0535cc284a

I expect that these 2 examples should pass the test equally correctly, since the types should inherit from the interface, but for some reason this does not happen.

Copy link

I found these snippets:

https://psalm.dev/r/e205e7be5f
<?php

declare(strict_types=1);

interface AI
{
    /**
     * @return $this
     */
    public function test(): static;
}

class A implements AI
{
    #[Override]
    public function test(): static
    {
        return $this;
    }
}

class B extends A
{
    #[Override]
    public function test(): static
    {
        return parent::test();
    }
}
Psalm output (using commit 03ee02c):

INFO: LessSpecificReturnStatement - 27:16 - The type 'A&static' is more general than the declared return type 'B&static' for B::test

INFO: MoreSpecificReturnType - 25:29 - The declared return type 'B&static' for B::test is more specific than the inferred return type 'A&static'
https://psalm.dev/r/0535cc284a
<?php

declare(strict_types=1);

interface AI
{
    /**
     * @return $this
     */
    public function test(): static;
}

class A implements AI
{
    /**
     * @return $this
     */
    #[Override]
    public function test(): static
    {
        return $this;
    }
}

class B extends A
{
    /**
     * @return $this
     */
    #[Override]
    public function test(): static
    {
        return parent::test();
    }
}
Psalm output (using commit 03ee02c):

No issues!

@mj4444ru
Copy link
Author

https://psalm.dev/r/51e9ac4bb6

Here is another case

Copy link

I found these snippets:

https://psalm.dev/r/51e9ac4bb6
<?php

declare(strict_types=1);

interface I
{
    /**
     * return $this
     */
    public function test(): static;
}

trait T1
{
    #[Override]
    public function test(): static
    {
        return $this;
    }
}

trait T2
{
    use T1;
    
    abstract public function test(): static;
}

class A
{
    use T1;
    use T2;
}
Psalm output (using commit 03ee02c):

ERROR: MethodSignatureMismatch - 15:5 - Method T1::test with return type 'A' is different to return type 'T1' of inherited method T2::test

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

No branches or pull requests

1 participant