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 return type in ternary operator #2288
Comments
Are you sure? :D https://3v4l.org/jNRDj |
I mean, your example shows exactly why this error is reported. |
I modified example to show why it is bad. https://phpstan.org/r/37170718-c033-437e-bbe0-2e9524ea6875 |
And what error do you expect? The original post states: Expected outputStan should not complain about type. |
I expect this because of every
I know that phpstan has some kind of black magic which uses caching for return types but I don't like it. |
Yeah, I plan to do something about it, stay tuned. |
Is this the same issue? |
@teohhanhui What output do you expect? |
phpstan should be able to tell that |
|
But we know which types Something like:
|
Yeah but it would be too annoying to have them listed, because when you know that your JSON string contains an object, it would be too annoying to check for the other types. |
Isn't that where inline phpdocs come in to save the day? We already have to do the same in many places. If we "know" something, we have to give the correct hints to the static analyzer. For example: https://phpstan.org/r/526680b3-006f-466c-b005-f5ab44f1156a |
Yes, you can use them. Or |
But it would be annoying to users to require it. |
And PHPStan checks in that case work: https://phpstan.org/r/126f39f4-6143-43a7-8eb7-b44728d54b25 |
But please stop discussing this in this issue, it's unrelated. |
@EgorBurykin PHPStan now reports different result with your code snippet: @@ @@
-17: Cannot call method format() on DateTimeImmutable|null.
+No errors |
@teohhanhui PHPStan now reports different result with your code snippet: @@ @@
-No errors
+13: Method MatchesJsonSchema::__construct() has parameter $schema with no value type specified in iterable type array. Full report
|
@ondrejmirtes PHPStan now reports different result with your code snippet: @@ @@
+13: Method MatchesJsonSchema::__construct() has parameter $schema with no value type specified in iterable type array.
+17: PHPDoc tag @var for variable $array has no value type specified in iterable type array.
18: Property MatchesJsonSchema::$schema (object) does not accept array. Full report
|
Fixed: phpstan/phpstan-src@d4edc59 |
Closes phpstan/phpstan#4588 Closes phpstan/phpstan#4091 Closes phpstan/phpstan#3382 Closes phpstan/phpstan#4177 Closes phpstan/phpstan#2288 Closes phpstan/phpstan#1157
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. |
Bug report
Stan seems can not derive correct type if ternary operator used like this:
$test->test() ? $test->test()->format('d') : null
Code snippet that reproduces the problem
https://phpstan.org/r/ff44c8c8-06ca-470a-a716-ebd334aee215
Expected output
Stan should not complain about type.
The text was updated successfully, but these errors were encountered: