mirror of
https://github.com/danog/psalm.git
synced 2024-11-29 20:28:59 +01:00
Fix #4631 - prevent reusing named params
This commit is contained in:
parent
ebc8b613de
commit
3a50cdef81
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user