1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Bind lower bounds to upper bounds as well when no upper bound can be inferred

Ref #4485
This commit is contained in:
Matt Brown 2020-11-11 17:46:09 -05:00
parent a8d7248c31
commit 2f7bf2a144
2 changed files with 40 additions and 10 deletions

View File

@ -307,17 +307,24 @@ class ArgumentAnalyzer
$template_result->upper_bounds
[$template_type->param_name]
[$template_type->defining_class]
)
&& !isset(
)) {
if (isset(
$template_result->lower_bounds
[$template_type->param_name]
[$template_type->defining_class]
)
) {
$template_result->upper_bounds[$template_type->param_name][$template_type->defining_class] = [
clone $template_type->as,
0
];
[$template_type->param_name]
[$template_type->defining_class]
)) {
$template_result->upper_bounds[$template_type->param_name][$template_type->defining_class] = [
clone $template_result->lower_bounds
[$template_type->param_name]
[$template_type->defining_class][0],
0
];
} else {
$template_result->upper_bounds[$template_type->param_name][$template_type->defining_class] = [
clone $template_type->as,
0
];
}
}
}
}

View File

@ -1421,6 +1421,29 @@ class FunctionTemplateTest extends TestCase
})();
}'
],
'allowClosureParamLowerBoundAndUpperBound' => [
'<?php
class Foo {}
/**
* @template TParam as Foo
* @psalm-param Closure(TParam): void $func
* @psalm-return Closure(TParam): TParam
*/
function takesClosure(callable $func): callable {
return
/**
* @psalm-param TParam $value
* @psalm-return TParam
*/
function ($value) use ($func) {
$func($value);
return $value;
};
}
$value = takesClosure(function(Foo $foo) : void {})(new Foo());'
],
];
}