-
Notifications
You must be signed in to change notification settings - Fork 534
Use InitializerExprTypeResolver instead of ConstantTypeHelper #1276
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
Alright, so this one is for you @rvanvelzen to finish if you're interested :) The original reasoning is here but since then I came up with different names so I'm gonna reiterate all of that below again. There are several problems we're solving here. The main one is that right now in the stable version, some types are resolved through this path: So I decided we should take a direct "Expr -> Type" path. First step I took is that for PHPStan 1.7.0, there's going to be fully static reflection (#1265). That allows us to typehint Then I added the extra methods that allow us to access the various The "Expr -> runtime value" part in BetterReflection is done with the "CompileNodeToValue" class: https://github.com/ondrejmirtes/BetterReflection/blob/5.3.x/src/NodeCompiler/CompileNodeToValue.php In PHPStan the equivalent code will live in "InitializerExprTypeResolver" class that I'm adding here in this PR. "CompileNodeToValue" works with the help of "CompilerContext" class: https://github.com/ondrejmirtes/BetterReflection/blob/5.3.x/src/NodeCompiler/CompilerContext.php In PHPStan the equivalent code will live in "InitializerExprContext" class that I'm adding here in this PR. Its job is to tell InitializerExprTypeResolver how to resolve various things like The "runtime value -> Type" part in PHPStan was done by ConstantTypeHelper which I'm deprecating in this PR. So all places should now do direct "Expr -> Type" resolution. What I want you to do here @rvanvelzen is to finish implementing InitializerExprTypeResolver and InitializerExprContext. There's a comment with the remaining expr types to resolve in InitializerExprTypeResolver. There's going to be some overlap with Why aren't we always using Once this PR is done, we can use it for various nice things like:
I think that these items are also good pointers on how to test that InitializerExprTypeResolver is correctly implemented. I'm worried about correct values for things like Or in case of class constants it's really easy. You can have In case of BinaryOps here are all the expr types that are supported in initializers and that should be resolved in InitializerExprTypeResolver correctly: https://github.com/nikic/PHP-Parser/blob/a6e34665fd6a2bcefc924cb5fd73788767ae510e/lib/PhpParser/ConstExprEvaluator.php#L173-L217 (ConstExprEvaluator is used internally in CompileNodeToValue for some simple expressions so we need to replicate that too). Please let me know @rvanvelzen if you're going to tackle this or if I should do it myself :) Thank you very much. |
Very, very nice. While I'm definitely interested in working on it I'm not sure if I have enough time available. I'll tinker a bit in the coming days and will get back to you :) |
Alright, I'm looking forward to that! :) And hopefully meanwhile I'm gonna look at your other PRs :) |
@ondrejmirtes I managed to get the tests passing, but unfortunately I won't be able to invest much more time into it this week: https://github.com/rvanvelzen/phpstan-src/tree/initializer-expr At some points I wondered whether the approach I was taking made sense. If I can help with anything else please let me know :) |
Awesome, I can take over from here, I'm gonna push your commit in this PR and continue :) Thank you very much! |
b440e88
to
792d90d
Compare
It's not finished by any means, but it's green enough for now :) |
No description provided.