Skip to content

Commit

Permalink
Merge pull request #7136 from klimick/forbid-positional-args-after-named
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 12, 2021
2 parents f79f857 + e08a4f2 commit 41ca7f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -644,8 +644,20 @@ public static function checkArgumentsMatch(

$arg_function_params = [];
$matched_args = [];
$named_args_was_used = false;

foreach ($args as $argument_offset => $arg) {
if ($named_args_was_used && !$arg->name) {
IssueBuffer::maybeAdd(
new InvalidNamedArgument(
'Cannot use positional argument after named argument',
new CodeLocation($statements_analyzer, $arg),
(string)$method_id
),
$statements_analyzer->getSuppressedIssues()
);
}

if ($arg->unpack) {
if ($function_param_count > $argument_offset) {
for ($i = $argument_offset; $i < $function_param_count; $i++) {
Expand Down Expand Up @@ -710,6 +722,8 @@ public static function checkArgumentsMatch(
}
}
} elseif ($arg->name && (!$function_storage || $function_storage->allow_named_arg_calls)) {
$named_args_was_used = true;

foreach ($function_params as $candidate_param) {
if ($candidate_param->name === $arg->name->name || $candidate_param->is_variadic) {
if ($candidate_param->name === $arg->name->name) {
Expand Down
13 changes: 13 additions & 0 deletions tests/ArgTest.php
Expand Up @@ -406,6 +406,19 @@ function foo(array $input) : CustomerData {
}',
'error_message' => 'InvalidNamedArgument'
],
'usePositionalArgAfterNamed' => [
'<?php
final class Person
{
public function __construct(
public string $name,
public int $age,
) { }
}
new Person(name: "", 0);',
'error_message' => 'InvalidNamedArgument'
],
'useUnpackedInvalidNamedArgument' => [
'<?php
class CustomerData {
Expand Down

0 comments on commit 41ca7f7

Please sign in to comment.