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

Forbid positional arg after named arg

This commit is contained in:
adrew 2021-12-11 23:37:15 +03:00
parent 76bb8bc655
commit 6cf2e31c4a
2 changed files with 29 additions and 0 deletions

View File

@ -23,6 +23,7 @@ use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Issue\InvalidArgument;
use Psalm\Issue\InvalidNamedArgument;
use Psalm\Issue\InvalidPassByReference;
use Psalm\Issue\PossiblyUndefinedVariable;
@ -646,8 +647,20 @@ class ArgumentsAnalyzer
$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++) {
@ -712,6 +725,8 @@ class ArgumentsAnalyzer
}
}
} 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) {

View File

@ -1,6 +1,7 @@
<?php
namespace Psalm\Tests;
use Person;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
@ -406,6 +407,19 @@ class ArgTest extends TestCase
}',
'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 {