-
Notifications
You must be signed in to change notification settings - Fork 507
Fix usage of context->truthy #2230
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
Conversation
This feels weird, shouldn't truthy include the true context in such cases? What I mean: truthy is kind of like a super type of true and should the fix be maybe somewhere else? |
I have the same feeling as @herndlm |
I've long suspected it's not intuitive and that it's actually switched. Would be nice to have some unit tests for TypeSpecifierContext to verify that. |
ec4ac51
to
78a314d
Compare
Indeed, truthy is a super type type of true @herndlm. But, let's say
and
or
This would be a great way, but I'm not sure how. The fact is
Sure, but is there a way to write php code and assert the context inside ?
for instance ? |
@@ -15,14 +15,14 @@ public function createRedirectRequest(string $redirectUri): ?string | |||
return null; | |||
} | |||
|
|||
assertType('array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}', $redirectUrlParts); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a return null
if true === array_key_exists('host', $redirectUrlParts)
so the key shouldn't exist anymore.
I edited all my example, because I made a little mistake. The error is in the fact that when doing
check, both path (if and else) are truthy. |
Thinking more about this, I'm hesitating between two point of view about the context:
Because it reports the context without knowing anything about the return values of the function. Therefore, it the type specifying method to care about. For example changing
to
at some point when working on the context. The more I think about it and the more I think the (1) solution is the right one, it's just that the right context check must be used. So if you agree with me @ondrejmirtes, I'll keep doing this kind of fix for all the extensions |
This pull request has been marked as ready for review. |
I have made all the truthy->true changes in typeSpecifyingFunction about function which only returns boolean. I tried to see if it fix some bugs and if so, I added test. So far it fixes for
I tried to reproduce the same potential bugs for others functions with type specifying function but wasn't able to find one for them (like is_a, is_array, method_exists, ...). I don't know why such bug existed for some functions and not others. With my level of understanding of context/typeSpecifier, I'm not sure to be able to do more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might have also fixed phpstan/phpstan#7686, please look into that. Thank you.
Indeed, it's also solved. I added a non regression test. |
Awesome 👏 |
8cf8e27
to
d0eef23
Compare
adac589
to
515f46a
Compare
515f46a
to
ee4437c
Compare
Yes, please do, I'd really appreciate it! |
Thank you! |
Closes phpstan/phpstan#3013
Closes phpstan/phpstan#7686
I tried to work on this issue to understand more
TypeSpecifyingExtension
,I discovered like explained in phpstan/phpstan#3013 (comment) that we don't get the same behavior with
than with
I started a PR to solve this, it seems like replacing
$context->truthy()
by$context->true()
solve the issue.It's because we get
but
Then I started to look at another type specifying extensions I worked on, the array_key_exists one, and I was able to reproduce the same issue by using
array_key_exists($foo, $bar) === true)
. And again I solved it by changing$context->truthy()
by$context->true()
.There is 8 others TypeSpecifyingExtensions with
$context->truthy()
about method which can only return false or true:And I wonder if there is not the same issue for them.
(Also there is 7 truthy calls (and 3 falsey calls) in the TypeSpecifier and I don't know if similar issues can occur in it.)
Since I don't really know if I'm going in the right direction, I'd like your opinion @ondrejmirtes
(cc @staabm if you can help me)