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

Wrong inference that get_parent_class() always return false #4302

Closed
noemi-salaun opened this issue Dec 31, 2020 · 4 comments · Fixed by phpstan/phpstan-src#2336
Closed

Wrong inference that get_parent_class() always return false #4302

noemi-salaun opened this issue Dec 31, 2020 · 4 comments · Fixed by phpstan/phpstan-src#2336
Labels
Milestone

Comments

@noemi-salaun
Copy link

Bug report

When using get_parent_class($object) on something that may be a subclass, PHPStan assume that it can only return false.

Code snippet that reproduces the problem

class HelloWorld
{	
    public function theMethod(MyInterface $object) {
        if (get_parent_class($object)) {    // <-- ERROR: if condition is always false
            return 1;
        }
    }
}

interface MyInterface {}

https://phpstan.org/r/a374c54d-1422-4c1e-9a34-5005f6668c8c

Expected output

When using an interface or a superclass, PHPStan should not be sure about the return being false, it should keep the type string|false, because the real object may be a subclass of the one typehinted

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jan 3, 2021
@mvorisek
Copy link
Contributor

this issue is still present, demo: https://phpstan.org/r/fe18bc9e-c405-4579-a30b-1ed7f55378e8

@phpstan-bot
Copy link
Contributor

@noemi-salaun After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-6: If condition is always false.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2336

@github-actions
Copy link

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 May 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants