Skip to content

Commit c6cf2cd

Browse files
committed
Use named argument in error for variadic types
When a type is variadic, and multiple named arguments are used, it's often hard to tell for which argument the error is coming. Since we have the named argument, we could use it in this situation.
1 parent 3dd5a01 commit c6cf2cd

File tree

10 files changed

+21
-8
lines changed

10 files changed

+21
-8
lines changed

src/Rules/AttributesCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function check(
143143
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, at least %d required.',
144144
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d-%d required.',
145145
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d-%d required.',
146-
'Parameter %s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given.',
146+
'Parameter %s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given%s.',
147147
'', // constructor does not have a return type
148148
'Parameter %s of attribute class ' . $attributeClassName . ' constructor is passed by reference, so it expects variables only',
149149
'Unable to resolve the template type %s in instantiation of attribute class ' . $attributeClassName,

src/Rules/Classes/InstantiationRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
208208
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, at least %d required.',
209209
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d-%d required.',
210210
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d-%d required.',
211-
'Parameter %s of class ' . $classDisplayName . ' constructor expects %s, %s given.',
211+
'Parameter %s of class ' . $classDisplayName . ' constructor expects %s, %s given%s.',
212212
'', // constructor does not have a return type
213213
'Parameter %s of class ' . $classDisplayName . ' constructor is passed by reference, so it expects variables only',
214214
'Unable to resolve the template type %s in instantiation of class ' . $classDisplayName,

src/Rules/FunctionCallParametersCheck.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ public function check(
324324
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
325325
$parameterType->describe($verbosityLevel),
326326
$argumentValueType->describe($verbosityLevel),
327+
$parameter->isVariadic() && $argumentName !== null ? sprintf(
328+
' as named argument %s',
329+
$argumentName,
330+
) : '',
327331
))
328332
->identifier('argument.type')
329333
->line($argumentLine)

src/Rules/Functions/CallCallablesRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function processNode(
124124
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
125125
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
126126
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
127-
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
127+
'Parameter %s of ' . $callableDescription . ' expects %s, %s given%s.',
128128
'Result of ' . $callableDescription . ' (void) is used.',
129129
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
130130
'Unable to resolve the template type %s in call to ' . $callableDescription,

src/Rules/Functions/CallToFunctionParametersRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
5656
'Function ' . $functionName . ' invoked with %d parameters, at least %d required.',
5757
'Function ' . $functionName . ' invoked with %d parameter, %d-%d required.',
5858
'Function ' . $functionName . ' invoked with %d parameters, %d-%d required.',
59-
'Parameter %s of function ' . $functionName . ' expects %s, %s given.',
59+
'Parameter %s of function ' . $functionName . ' expects %s, %s given%s.',
6060
'Result of function ' . $functionName . ' (void) is used.',
6161
'Parameter %s of function ' . $functionName . ' is passed by reference, so it expects variables only.',
6262
'Unable to resolve the template type %s in call to function ' . $functionName,

src/Rules/Functions/CallUserFuncRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
6767
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
6868
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
6969
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
70-
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
70+
'Parameter %s of ' . $callableDescription . ' expects %s, %s given%s.',
7171
'Result of ' . $callableDescription . ' (void) is used.',
7272
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
7373
'Unable to resolve the template type %s in call to ' . $callableDescription,

src/Rules/Methods/CallMethodsRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array
6262
'Method ' . $messagesMethodName . ' invoked with %d parameters, at least %d required.',
6363
'Method ' . $messagesMethodName . ' invoked with %d parameter, %d-%d required.',
6464
'Method ' . $messagesMethodName . ' invoked with %d parameters, %d-%d required.',
65-
'Parameter %s of method ' . $messagesMethodName . ' expects %s, %s given.',
65+
'Parameter %s of method ' . $messagesMethodName . ' expects %s, %s given%s.',
6666
'Result of method ' . $messagesMethodName . ' (void) is used.',
6767
'Parameter %s of method ' . $messagesMethodName . ' is passed by reference, so it expects variables only.',
6868
'Unable to resolve the template type %s in call to method ' . $messagesMethodName,

src/Rules/Methods/CallStaticMethodsRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array
7070
$displayMethodName . ' invoked with %d parameters, at least %d required.',
7171
$displayMethodName . ' invoked with %d parameter, %d-%d required.',
7272
$displayMethodName . ' invoked with %d parameters, %d-%d required.',
73-
'Parameter %s of ' . $lowercasedMethodName . ' expects %s, %s given.',
73+
'Parameter %s of ' . $lowercasedMethodName . ' expects %s, %s given%s.',
7474
'Result of ' . $lowercasedMethodName . ' (void) is used.',
7575
'Parameter %s of ' . $lowercasedMethodName . ' is passed by reference, so it expects variables only.',
7676
'Unable to resolve the template type %s in call to method ' . $lowercasedMethodName,

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ public function testNamedArguments(): void
19031903
91,
19041904
],
19051905
[
1906-
'Parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given.',
1906+
'Parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given as named argument foo.',
19071907
91,
19081908
],
19091909
[
@@ -1918,6 +1918,14 @@ public function testNamedArguments(): void
19181918
'Unpacked argument (...) cannot be followed by a non-unpacked argument.',
19191919
94,
19201920
],
1921+
[
1922+
'Parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given as named argument foo.',
1923+
95,
1924+
],
1925+
[
1926+
'Parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given as named argument bar.',
1927+
95,
1928+
]
19211929
]);
19221930
}
19231931

tests/PHPStan/Rules/Methods/data/named-arguments.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function doDolor(): void
9292
$this->doIpsum(...['a' => 1, 'foo' => 'foo']);
9393
$this->doIpsum(...['b' => 1, 'foo' => 'foo']);
9494
$this->doIpsum(...[1, 2], 'foo');
95+
$this->doIpsum(1, 2, foo: 1, bar: 2);
9596
}
9697

9798
}

0 commit comments

Comments
 (0)