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 return type in ternary operator #2288

Closed
EgorBurykin opened this issue Jul 9, 2019 · 22 comments
Closed

Wrong return type in ternary operator #2288

EgorBurykin opened this issue Jul 9, 2019 · 22 comments

Comments

@EgorBurykin
Copy link

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.

@ondrejmirtes
Copy link
Member

Are you sure? :D https://3v4l.org/jNRDj

@ondrejmirtes
Copy link
Member

I mean, your example shows exactly why this error is reported.

@EgorBurykin
Copy link
Author

I modified example to show why it is bad. https://phpstan.org/r/37170718-c033-437e-bbe0-2e9524ea6875

@ondrejmirtes
Copy link
Member

And what error do you expect? The original post states:

Expected output

Stan should not complain about type.

@pepakriz
Copy link
Contributor

I expect this because of every $test->test() call can return different result type.

+---------------------------------------------------------------+
| Line | test.php                                               |
+---------------------------------------------------------------+
| 17   | Cannot call method format() on DateTimeImmutable|null. |
+---------------------------------------------------------------+

I know that phpstan has some kind of black magic which uses caching for return types but I don't like it.

@ondrejmirtes
Copy link
Member

Yeah, I plan to do something about it, stay tuned.

@teohhanhui
Copy link
Contributor

Is this the same issue?

https://phpstan.org/r/4911db47-3f00-4340-b211-6d1e85f64db2

@ondrejmirtes
Copy link
Member

@teohhanhui What output do you expect?

@teohhanhui
Copy link
Contributor

phpstan should be able to tell that json_decode might not return an object, and so the assignment should raise an error for incompatible types.

@ondrejmirtes
Copy link
Member

json_decode return type is mixed so it can return anything, but it also might return the correct thing. That's why it doesn't complain. And it's unrelated to this issue.

@teohhanhui
Copy link
Contributor

But we know which types json_decode can return, right? It's only the types that are possible in JSON.

Something like:

Property MatchesJsonSchema::$schema (object) does not accept object|array|string|int|float|bool|null.

@ondrejmirtes
Copy link
Member

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.

@teohhanhui
Copy link
Contributor

teohhanhui commented Sep 19, 2019

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
https://3v4l.org/bMtP1

@ondrejmirtes
Copy link
Member

Yes, you can use them. Or assert().

@ondrejmirtes
Copy link
Member

But it would be annoying to users to require it.

@ondrejmirtes
Copy link
Member

And PHPStan checks in that case work: https://phpstan.org/r/126f39f4-6143-43a7-8eb7-b44728d54b25

@ondrejmirtes
Copy link
Member

But please stop discussing this in this issue, it's unrelated.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-17: Cannot call method format() on DateTimeImmutable|null.
+No errors

@phpstan-bot
Copy link
Contributor

@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
Line Error
13 Method MatchesJsonSchema::__construct() has parameter $schema with no value type specified in iterable type array.

@phpstan-bot
Copy link
Contributor

@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
Line Error
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.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@d4edc59

@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

5 participants