-
-
Notifications
You must be signed in to change notification settings - Fork 934
Description
Bug report
The new @phpstan-assert-if-true type assertion seems to apply an invalid logical conclusion. Take for example a function that asserts some things about a variable:
- When the function returns true, you can be certain that a value is not null.
- When the function returns false, the value could be null or any other condition leading to returning false.
The correct typings are these:
| Input | Type Check return | Valid Type |
|---|---|---|
| ?int | true | int |
| ?int | false | ?int |
But PHPStan is producing these:
| Input | Type Check return | PHPStan Type |
|---|---|---|
| ?int | true | int |
| ?int | false | null |
So PHPStan is implying a type when the assertion does not match. But this isn't possible as shown in the example. The only correct way is to keep the type when the assertion returns false. As you can't apply any logical conclusion from a not-matching type assertion.
A type definition for a non-matching type assertion can't be automatically asserted. The @phpstan-assert-if-true and @phpstan-assert-if-false definitions should be used if really a type can be inferred from a true and false value.
Code snippet that reproduces the problem
https://phpstan.org/r/892c5da4-9f8a-4e48-bd80-e80d1514ac85
Did PHPStan help you today? Did it make you happy in any way?
PHPStan has been a crucial tool I use when writing code. Writing tests is nice, but you can't test every single case. The more I transform my codebases to be statically analyzable the better PHPStan results get, and I have been warned of quite some bugs when refactoring code.