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

Exception catch scope infers incorrect types #1670

Closed
cs278 opened this issue Nov 29, 2018 · 6 comments
Closed

Exception catch scope infers incorrect types #1670

cs278 opened this issue Nov 29, 2018 · 6 comments

Comments

@cs278
Copy link
Contributor

cs278 commented Nov 29, 2018

Summary of a problem or a feature request

Exception catch scope infers incorrect types. Apologies this isn't easy to describe and I didn't have any luck finding a similar issue. The code below is a minimum reproducer of the problem.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/** @var \stdClass|null */
$object = null;

try {
    if ($object === null) {
        throw new \InvalidArgumentException();
    }

    throw new \RuntimeException(); // If you remove this the same error is present as well, which makes even less sense to me
} catch (\Throwable $e) {
    if ($object === null) { // Strict comparison using === between stdClass and null will always evaluate to false.
    }
}

https://phpstan.org/r/c56d3540-d5fa-4366-8ca8-fb6bb63d2484

Expected output

No error

@cs278
Copy link
Contributor Author

cs278 commented Jun 27, 2019

@ondrejmirtes this seems to have been fixed with: 00c781c

@ondrejmirtes
Copy link
Member

Hi, that's weird. This area definitely hasn't been implemented yet. PHPStan does not understand when are exceptions in try try block and what are the resulting types in respective catch blocks.

@phpstan-bot
Copy link
Contributor

@cs278 PHPStan now reports different result with your code snippet:

@@ @@
-13: Strict comparison using === between stdClass and null will always evaluate to false.
+No errors

@ondrejmirtes
Copy link
Member

I have no idea why this isn't reported by the playground, but locally it's still just stdClass, not stdClass|null.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#481

@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 Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants