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

::class syntax equivalence with instanceof #9542

Closed
Seb33300 opened this issue Jun 30, 2023 · 8 comments
Closed

::class syntax equivalence with instanceof #9542

Seb33300 opened this issue Jun 30, 2023 · 8 comments
Labels
Milestone

Comments

@Seb33300
Copy link

Seb33300 commented Jun 30, 2023

Bug report

PHPStan is able to recognize a condition using instanceof syntax (eg: if ($foo instanceof Bar)), but not a condition using the ::class syntax (eg: if ($foo::class === Bar::class)) to determine the object class.

This result in that break in EasyAdmin project: https://github.com/EasyCorp/EasyAdminBundle/pull/5813/files#r1247414831

Code snippet that reproduces the problem

https://phpstan.org/r/0c84ecb2-1b23-42a8-9161-a32f7096c5b8
https://phpstan.org/r/05f9613a-d9cf-4321-986e-247723efcbb9

Expected output

if ($foo instanceof Bar) should be equivalent to if ($foo::class === Bar::class)

Did PHPStan help you today? Did it make you happy in any way?

No response

@mergeable
Copy link

mergeable bot commented Jun 30, 2023

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@herndlm
Copy link
Contributor

herndlm commented Jun 30, 2023

A minimal reproducer, that can then act as regression test too, would be great :)

@Seb33300
Copy link
Author

Updated the initial message with: https://phpstan.org/r/823b3c7f-6e94-4f71-8a74-3d02c427f4b2

@herndlm
Copy link
Contributor

herndlm commented Jun 30, 2023

Isn't this a logic error in your early exit condition? E.g. looks like the following works fine: https://phpstan.org/r/0c84ecb2-1b23-42a8-9161-a32f7096c5b8

with the && that you have it does not exclude anything else than object which is needed to properly narrow mixed.

or the alternative without the reversed logic / early exit: https://phpstan.org/r/0a5e065c-1fec-4ac4-95eb-580fa0524b0f

@Seb33300
Copy link
Author

Seb33300 commented Jun 30, 2023

Right, sorry for the mistake i did trying to simplify the code.

Here is a reproducer: https://phpstan.org/r/0c84ecb2-1b23-42a8-9161-a32f7096c5b8

Another example using custom classes: https://phpstan.org/r/05f9613a-d9cf-4321-986e-247723efcbb9

The issue come from the order of the condition.
The same condition works if we inverse the elements.

@ondrejmirtes
Copy link
Member

Yeah, this is easy to fix, TypeSpecifier just handles the condition on one side, we need to handle the inversed variant (yoda condition) too.

@ondrejmirtes
Copy link
Member

fixed: phpstan/phpstan-src#2498

@github-actions
Copy link

github-actions bot commented Aug 1, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants