Skip to content

Commit

Permalink
Merge pull request #7135 from klimick/fix-closure-param-type-inference
Browse files Browse the repository at this point in the history
Fix type inference of closure params
  • Loading branch information
orklah committed Dec 11, 2021
2 parents b7a1528 + 603e1c9 commit 6632ddf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Expand Up @@ -181,8 +181,6 @@ public static function analyze(

if (($arg->value instanceof PhpParser\Node\Expr\Closure
|| $arg->value instanceof PhpParser\Node\Expr\ArrowFunction)
&& $template_result
&& $template_result->lower_bounds
&& $param
&& !$arg->value->getDocComment()
) {
Expand All @@ -191,7 +189,7 @@ public static function analyze(
$args,
$method_id,
$context,
$template_result,
$template_result ?? new TemplateResult([], []),
$argument_offset,
$arg,
$param
Expand Down
22 changes: 22 additions & 0 deletions tests/CallableTest.php
Expand Up @@ -63,6 +63,28 @@ function(string $a) {
'error_levels' => [],
'7.4',
],
'inferArgFromClassContext' => [
'<?php
final class Calc
{
/**
* @param Closure(int, int): int $_fn
*/
public function __invoke(Closure $_fn): int
{
return $_fn(42, 42);
}
}
$calc = new Calc();
$a = $calc(fn($a, $b) => $a + $b);',
'assertions' => [
'$a' => 'int',
],
'error_levels' => [],
'7.4',
],
'varReturnType' => [
'<?php
$add_one = function(int $a) : int {
Expand Down
2 changes: 0 additions & 2 deletions tests/FunctionCallTest.php
Expand Up @@ -1377,8 +1377,6 @@ function (array $matches) : string {
'<?php
/**
* @param string[] $ids
* @psalm-suppress MissingClosureReturnType
* @psalm-suppress MixedArgumentTypeCoercion
*/
function(array $ids): array {
return \preg_replace_callback(
Expand Down

0 comments on commit 6632ddf

Please sign in to comment.