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

False positive MixedPropertyFetch after instanceof $this #11281

Open
kynx opened this issue Feb 14, 2025 · 5 comments
Open

False positive MixedPropertyFetch after instanceof $this #11281

kynx opened this issue Feb 14, 2025 · 5 comments
Labels

Comments

@kynx
Copy link

kynx commented Feb 14, 2025

<?php

abstract class A
{
    public function __construct(private string $value)
    {
    }

    public function equals(mixed $other): bool
    {
        if (! $other instanceof $this) {
            return false;
        }

        return $this->value === $other->value;
    }
}

Results in:

ERROR: MixedPropertyFetch - src/A.php:15:33 - Cannot fetch property on mixed var $other (see https://psalm.dev/034)
        return $this->value === $other->value;
Copy link

Hey @kynx, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

@kynx
Copy link
Author

kynx commented Feb 14, 2025

Copy link

I found these snippets:

https://psalm.dev/r/814236f6c7
<?php

abstract class A
{
    public function __construct(private string $value)
    {
    }

    public function equals(mixed $other): bool
    {
        if (! $other instanceof $this) {
            return false;
        }

        return $this->value === $other->value;
    }
}
Psalm output (using commit c56f798):

INFO: MixedPropertyFetch - 15:33 - Cannot fetch property on mixed var $other

@orklah orklah added the bug label Feb 14, 2025
@orklah
Copy link
Collaborator

orklah commented Feb 14, 2025

Note that replacing $this by static works if it's not inverted
https://psalm.dev/r/0afbd90e20

Copy link

I found these snippets:

https://psalm.dev/r/0afbd90e20
<?php

abstract class A
{
    public function __construct(private string $value)
    {
    }

    public function equals(mixed $other): bool
    {
        if ($other instanceof static) {
            /** @psalm-trace $other */;
            return false;
        }
		/** @psalm-trace $other */;
        return $this->value === $other->value;
    }
}
Psalm output (using commit c56f798):

INFO: Trace - 12:39 - $other: A&static

INFO: Trace - 15:29 - $other: mixed

INFO: MixedPropertyFetch - 16:33 - Cannot fetch property on mixed var $other

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