1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

fix @param-out with named arguments

This commit is contained in:
Daniel Linjama 2023-10-23 10:21:04 +03:00
parent c7d7b48bdd
commit a375f441f2
2 changed files with 39 additions and 1 deletions

View File

@ -51,6 +51,7 @@ use Psalm\Type\Union;
use UnexpectedValueException; use UnexpectedValueException;
use function array_map; use function array_map;
use function array_reduce;
use function array_reverse; use function array_reverse;
use function array_slice; use function array_slice;
use function array_values; use function array_values;
@ -1031,7 +1032,26 @@ class ArgumentsAnalyzer
$check_null_ref = true; $check_null_ref = true;
if ($last_param) { if ($last_param) {
if ($argument_offset < count($function_params)) { if ($arg->name !== null) {
$function_param = array_reduce(
$function_params,
static function (
?FunctionLikeParameter $function_param,
FunctionLikeParameter $param
) use (
$arg
) {
if ($param->name === $arg->name->name) {
return $param;
}
return $function_param;
},
null,
);
if ($function_param === null) {
return false;
}
} elseif ($argument_offset < count($function_params)) {
$function_param = $function_params[$argument_offset]; $function_param = $function_params[$argument_offset];
} else { } else {
$function_param = $last_param; $function_param = $last_param;

View File

@ -193,6 +193,24 @@ class ReferenceConstraintTest extends TestCase
if ($a) {}', if ($a) {}',
], ],
'PHP80-paramOutChangeTypeWithNamedArgument' => [
'code' => '<?php
/**
* @param-out int $s
*/
function addFoo(bool $five = true, ?string &$s = null) : void {
if ($five) {
$s = 5;
return;
}
$s = 4;
}
addFoo(s: $a);',
'assertions' => [
'$a' => 'int',
],
],
]; ];
} }