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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use root expression when checking impossible types #1254

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Apr 27, 2022

This is the implementation of the idea that Ondrej mentioned in phpstan/phpstan-webmozart-assert#130 (comment)

I'm not quite sure about details yet, it looks like it makes phpstan already smarter though. It also looks like phpstan now remembers more specified types as stated in https://phpstan.org/blog/remembering-and-forgetting-returned-values.

This PR is mostly to find out how much will break 馃憖

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

Tbh, I was even expecting more errors. But it looks like it's doing too much now, not sure. E.g. it complains in https://github.com/phpstan/phpstan-doctrine/blob/1.3.3/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php#L126 with

  ------ --------------------------------------------------------------------- 
  Line   tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php              
 ------ --------------------------------------------------------------------- 
  126    Call to method                                                       
         Doctrine\Common\Collections\ArrayCollection<*NEVER*,*NEVER*>::add()  
         with QueryResult\Entities\Many will always evaluate to true.         
 ------ --------------------------------------------------------------------- 

Not sure if this should even be looked at in the first place?

@ondrejmirtes ondrejmirtes force-pushed the use-root-expression-when-checking-impossible-types branch from 302acc0 to 4711b3f Compare April 27, 2022 13:29
@ondrejmirtes
Copy link
Member

Some jobs didn't run because I misunderstood the documentation of GitHub Actions. Hopefully it's gonna be better now: 3bf2958

@herndlm herndlm force-pushed the use-root-expression-when-checking-impossible-types branch 3 times, most recently from 2aba5cc to f26133f Compare April 27, 2022 14:33
@ondrejmirtes
Copy link
Member

This looks clean and promising. Could you please try to simulate the situation that I outlined here? phpstan/phpstan-webmozart-assert#130 (comment)

$str !== '' && SOME_FAUX_FUNCTION($str)

With this improvement we'll be able to write extensions for functions like "ipv4"...

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

the 10 webmozart failures are actual fixes, or respectively cases were phpstan got smarter :)
not sure about the remaining composer failures, I'm still looking at those. UPDATE: they might be mostly OK too, not sure about some details though. some things actually surprised me that phpstan understood them :D

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

This looks clean and promising. Could you please try to simulate the situation that I outlined here? phpstan/phpstan-webmozart-assert#130 (comment)

$str !== '' && SOME_FAUX_FUNCTION($str)

With this improvement we'll be able to write extensions for functions like "ipv4"...

I think it's not yet enough, I tried with #1068 here and with phpstan/phpstan-webmozart-assert#130 itself. I was running out of time, but I think such an expression would still resolve to a BooleanType and then my adaptions don't trigger anything and it falls back to the old behaviour. I'll continue later

@ondrejmirtes
Copy link
Member

BTW I'm trying afew more commits above your PR: #1256

@herndlm herndlm force-pushed the use-root-expression-when-checking-impossible-types branch 2 times, most recently from 2b10c8b to ca6065c Compare April 27, 2022 20:00
@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

Thx for the help Ondrej! I rebased on latest 1.6.x already, apparently ImpossibleCheckTypeMethodCallRuleEqualsTest still needs fixing, but we're getting closer.

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

OK, a first local check looks like you did it. great job! :)
I'll add the new things it found and push when it's ready local. UPDATE: you did that too xD ok then, I'll cherry-pick that commit too

@herndlm herndlm force-pushed the use-root-expression-when-checking-impossible-types branch from ca6065c to 6a58237 Compare April 27, 2022 20:22
@ondrejmirtes
Copy link
Member

Please let's not step on each other's toes right now, I'm very close to being green here and merging it :)

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

just cherry-picking your changes xD but yeah, sorry for hogging CI, every push I thought you're done

@ondrejmirtes ondrejmirtes marked this pull request as ready for review April 27, 2022 20:38
@ondrejmirtes ondrejmirtes merged commit 6bf335c into phpstan:1.6.x Apr 27, 2022
@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

there's a new legit webmozart thing it found, but a couple other loosely-equal checks are now not reported anymore. is that fine? Let me know what to adapt

@herndlm herndlm deleted the use-root-expression-when-checking-impossible-types branch April 27, 2022 20:38
@ondrejmirtes
Copy link
Member

Thank you :) I'm gonna wait for the issue bot and then go to bed :) And fix the integration tests in phpstan/phpstan tommorrow.

@ondrejmirtes
Copy link
Member

a couple other loosely-equal checks are now not reported anymore

A bigger mystery for me was that they actually were reported before. There's no logic in PHPStan to say that 1 == true is true.

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

I think I thought the same while adding them. weird. ok then

@ondrejmirtes
Copy link
Member

So it's fine to remove those ::eq asserts-reported errors from the tests.

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

than that shall be my last action for the day

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

sorry, one more thing, it looks like true == true is also not reported any more. does that one need fixing?

@ondrejmirtes
Copy link
Member

Sure, should be covered here :)

if (
($stringType->isSuperTypeOf($leftType)->yes() && $stringType->isSuperTypeOf($rightType)->yes())
|| ($integerType->isSuperTypeOf($leftType)->yes() && $integerType->isSuperTypeOf($rightType)->yes())
|| ($floatType->isSuperTypeOf($leftType)->yes() && $floatType->isSuperTypeOf($rightType)->yes())
) {
return $this->resolveIdenticalType($leftType, $rightType);

@ondrejmirtes
Copy link
Member

Wow, I'm shocked, the issue bot haven't found a single change in the issues :) But at least we haven't broken anything :)

@herndlm
Copy link
Contributor Author

herndlm commented Apr 27, 2022

I hope this opens the door for furhter type specifier cleanups at least. or features

@ondrejmirtes
Copy link
Member

Yeah, I think so, you can try it out in webmozart/assert extension :) Also this one should be possible now #1068

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants