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
Added support for first-class callables #7113
Conversation
@@ -235,6 +243,45 @@ public static function analyze( | |||
); | |||
|
|||
$config->eventDispatcher->dispatchAfterEveryFunctionCallAnalysis($event); | |||
|
|||
if ($is_first_class_callable) { |
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.
Why are you returning early here?
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.
I wasn't sure about this, but it seemed the code below was generally analyzing a call to the given function, which isn't actually happening. Some guidance on what still needs to be called from the code below would be appreciated.
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.
you wouldn't go that far anyway, you have another if ($is_first_class_callable)
just below that create the closure. I just found weird to return before that only when this condition if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
is met and not otherwise
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.
The path taken by if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id)
invokes FunctionCallReturnTypeFetcher::fetch
, which returns a closure for first-class callables from named calls (e.g. strlen(...)
or var_dump(...)
). The if-statement below creates a closure for a first-class callable created from another expression, such as $closure(...)
(see the test labelled FirstClassCallable:FromClosure).
Perhaps there is a more elegant way of doing this, but that's why the separation exists here.
@@ -235,6 +243,45 @@ public static function analyze( | |||
); |
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.
the AfterEveryFunctionCallAnalysisEvent is the plugin hook. I feel like this should stay at the bottom of the Analyzer. Or maybe it should not be called at all when dealing with first class callable, not sure
EDIT: well, just saw this is currently not at the bottom of the function, I may be wrong here
src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php
Outdated
Show resolved
Hide resolved
src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php
Show resolved
Hide resolved
Wow, that's impressive work! |
Looks good to me in principle, but it seems it broke closure handling somewhere (hence all the failures). |
Skipping checking args for first-class callables, but fixes handling of other methods.
@weirdan I reordered a couple of calls in |
Looks good! Let's see how this behave in the wild :D! |
Hopefully the approach here is appropriate. First-class callables are treated as a function call with a single placeholder argument, which necessitates special handling in the call analyzers to swap out the the return value to a closure and skip analyzing function arguments.
Closes #6412.