1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #4631 - prevent reusing named params

This commit is contained in:
Matt Brown 2021-01-27 22:53:55 -05:00 committed by Daniil Gentili
parent ebc8b613de
commit 3a50cdef81
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 45 additions and 0 deletions

View File

@ -622,6 +622,7 @@ class ArgumentsAnalyzer
}
$arg_function_params = [];
$matched_args = [];
foreach ($args as $argument_offset => $arg) {
if ($arg->unpack && $function_param_count > $argument_offset) {
@ -631,6 +632,24 @@ class ArgumentsAnalyzer
} elseif ($arg->name && (!$function_storage || $function_storage->allow_named_arg_calls)) {
foreach ($function_params as $candidate_param) {
if ($candidate_param->name === $arg->name->name || $candidate_param->is_variadic) {
if ($candidate_param->name === $arg->name->name) {
if (isset($matched_args[$candidate_param->name])) {
if (IssueBuffer::accepts(
new InvalidNamedArgument(
'Parameter $' . $arg->name->name . ' has already been used in '
. ($cased_method_id ?: $method_id),
new CodeLocation($statements_analyzer, $arg->name),
(string) $method_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
$matched_args[$candidate_param->name] = true;
}
$arg_function_params[$argument_offset] = [$candidate_param];
break;
}
@ -651,8 +670,10 @@ class ArgumentsAnalyzer
}
} elseif ($function_param_count > $argument_offset) {
$arg_function_params[$argument_offset] = [$function_params[$argument_offset]];
$matched_args[$function_params[$argument_offset]->name] = true;
} elseif ($last_param && $last_param->is_variadic) {
$arg_function_params[$argument_offset] = [$last_param];
$matched_args[$last_param->name] = true;
}
}

View File

@ -480,6 +480,30 @@ class ArgTest extends TestCase
echo $b;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'overwriteNamedParam' => [
'<?php
function test(int $param, int $param2): void {
echo $param + $param2;
}
test(param: 1, param: 2);',
'error_message' => 'InvalidNamedArgument',
[],
false,
'8.0'
],
'overwriteOrderedNamedParam' => [
'<?php
function test(int $param, int $param2): void {
echo $param + $param2;
}
test(1, param: 2);',
'error_message' => 'InvalidNamedArgument',
[],
false,
'8.0'
],
];
}
}