-
Notifications
You must be signed in to change notification settings - Fork 534
PHPDoc-based type narrowing #1317
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
Oh, nice! Looks like we could get rid then of almost all of the custom expression building in the phpstan-assert* extensions :) |
Awesome, I haven't, I'm too deep in InitializerExprTypeResolver now (and also in my Verona phpDay talk) so great, I appreciate this :) I'm gonna go through the code soon, I just gonna list of some of my extra ideas for rules around this (you might have already done some of them, but I need to get them off my chest first :):
Before I dive into the review, can you please check this list? :) Thanks. |
Just to be sure - if you're gonna work on these, please add them as new commits, it makes the review easier :) To see how stubs-related functionality is tested, look at https://github.com/phpstan/phpstan-src/blob/1.7.x/tests/PHPStan/Analyser/ClassConstantStubFileTest.php. |
None of that list is tested right now, just the very basic type narrowing. I'll look into that soon, thanks :) |
I'm glad that I hit the nail on the head :) Also I'll probably question some of your reflection choices (where to put the new information), but I need to think about it and I'll shuffle things around myself later :) |
src/Analyser/TypeSpecifier.php
Outdated
$assert->isNegated() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createTrue(), | ||
false, | ||
$scope, | ||
$containsClassTemplate ? $call : null, |
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.
This is really tricky, but I couldn't think of something else.
I really like how it's coming together :) After this one is merged, I also look forward to support for properties and methods besides parameter names :) |
There's some work left to be done here, but it should be ready for review. I'm not sure if the always true/false errors should pop up from these asserts - a function could very well narrow a type down without that being the only thing the function does. This could actually be handled by specifying types from conditional return types. I have a PR almost ready for that. All that aside: I'll be on holiday for the next 2 weeks so I won't be able to spend much time on this. If you point out any issues I'll gladly work on them but I just don't know how quickly that will be. Thanks for understanding :) |
That's my worry as well but I'd like to see in practice if it's a real concern or not. In theory someone could write It'd be nice to have these "impossible or always-true type checks" reported only in bleeding edge, but I'm not sure we can easily isolate these cases from the already existing ones in rules like ImpossibleCheckTypeFunctionCallRule etc.
I have no idea what you mean by that, so I'm intrigued to see it :)
I hope you won't mind if we're going to postpone this feature for PHPStan 1.8. There's already a lot of stuff ready in PHPStan 1.7 (and it doesn't mean we can't release 1.8 immediately after the PHPDoc-based type narrowing is merged too) and I'd like to gather real-world feedback on it soon. Thank you for understanding. |
Sure, it's absolutely fine to let this sit until after 1.7 is released :) |
As far as i know this is the current behavior in psalm, which is why not all methods in |
BTW Do you have plans to support this syntax? Thanks! |
What would that do? Does that mean |
Not necessarily for the initial implementation, but I do plan on adding that as well. |
I have no idea, doesn't seem documented: https://psalm.dev/docs/annotating_code/adding_assertions/ /cc @orklah What does |
Those are equality assertions, though the documentation for it is hard to find: https://psalm.dev/annotating_code/assertion_syntax/ |
I believe this has become a bit too large and complex. I'd like to break it up into a few pieces to make it easier to reason about. |
Feel free to do what you think is best 😊 My idea for reflection is that I'm not sure if I want it on ParametersAcceptor vs. MethodReflection/FunctionReflection. I think it fits the latter better. |
this PR seems to implement phpstan/phpstan#7385, please include the demo https://phpstan.org/r/53ba9b04-685e-404c-ba77-a9616ca75ace code in the tests |
BTW @rvanvelzen This one would be a great thing to finish :) My idea is that instead of ParametersAcceptorWithAsserts / FunctionVariant etc., the |
@ondrejmirtes it's on my list to work on, but I need/want to finish phpstan/phpdoc-parser#139 first but I'm not really happy with the current version :/ |
Wow, just saw this in my notifications, sorry I seem to have missed it completely before. The |
I'm quite happy with the CI results, I'll make Rector freak out less about it (it's one of the problems I already suspected and mentioned) and then we can improve it on top of 1.9.x branch :) |
I want to merge this because we're already stepping on each other's toes (I have some local commits but you force-pushed meanwhile :)) |
We can do so much more stuff once this is merged!
I can't wait :) |
Thank you very much! |
OMG, I love this so much! ace76ce |
Hi @rvanvelzen, can this PR be used to solve phpstan/phpstan#7385? |
I don't think so, because |
Implementation for phpstan/phpstan#7110. I already had a big chunk of this quite soon, so I finished most of it.
Just putting this out there, hopefully you didn't already start on this 🥲