Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Rules/AttributesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ public function check(
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, at least %d required.',
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d-%d required.',
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d-%d required.',
'Parameter %s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given.',
'%s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given.',
'', // constructor does not have a return type
'Parameter %s of attribute class ' . $attributeClassName . ' constructor is passed by reference, so it expects variables only',
'%s of attribute class ' . $attributeClassName . ' constructor is passed by reference, so it expects variables only',
'Unable to resolve the template type %s in instantiation of attribute class ' . $attributeClassName,
'Missing parameter $%s in call to ' . $attributeClassName . ' constructor.',
'Unknown parameter $%s in call to ' . $attributeClassName . ' constructor.',
'Return type of call to ' . $attributeClassName . ' constructor contains unresolvable type.',
'Parameter %s of attribute class ' . $attributeClassName . ' constructor contains unresolvable type.',
'%s of attribute class ' . $attributeClassName . ' constructor contains unresolvable type.',
'Attribute class ' . $attributeClassName . ' constructorinvoked with %s, but it\'s not allowed because of @no-named-arguments.',
);

Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Classes/InstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, at least %d required.',
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d-%d required.',
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d-%d required.',
'Parameter %s of class ' . $classDisplayName . ' constructor expects %s, %s given.',
'%s of class ' . $classDisplayName . ' constructor expects %s, %s given.',
'', // constructor does not have a return type
'Parameter %s of class ' . $classDisplayName . ' constructor is passed by reference, so it expects variables only',
' %s of class ' . $classDisplayName . ' constructor is passed by reference, so it expects variables only',
'Unable to resolve the template type %s in instantiation of class ' . $classDisplayName,
'Missing parameter $%s in call to ' . $classDisplayName . ' constructor.',
'Unknown parameter $%s in call to ' . $classDisplayName . ' constructor.',
'Return type of call to ' . $classDisplayName . ' constructor contains unresolvable type.',
'Parameter %s of class ' . $classDisplayName . ' constructor contains unresolvable type.',
'%s of class ' . $classDisplayName . ' constructor contains unresolvable type.',
'Class ' . $classDisplayName . ' constructor invoked with %s, but it\'s not allowed because of @no-named-arguments.',
));
}
Expand Down
15 changes: 10 additions & 5 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use function array_key_exists;
use function count;
use function implode;
use function is_int;
use function is_string;
use function max;
use function sprintf;
Expand Down Expand Up @@ -331,7 +332,7 @@ public function check(
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
$errors[] = RuleErrorBuilder::message(sprintf(
$wrongArgumentTypeMessage,
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
$this->describeParameter($parameter, $argumentName ?? $i + 1),
$parameterType->describe($verbosityLevel),
$argumentValueType->describe($verbosityLevel),
))
Expand Down Expand Up @@ -409,7 +410,7 @@ public function check(
}

$errors[] = RuleErrorBuilder::message(sprintf(
'Parameter %s is passed by reference so it does not accept %s.',
'%s is passed by reference so it does not accept %s.',
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
$propertyDescription,
))->identifier('argument.byRef')->line($argumentLine)->build();
Expand Down Expand Up @@ -625,11 +626,15 @@ private function processArguments(
return [$errors, $newArguments];
}

private function describeParameter(ParameterReflection $parameter, ?int $position): string
private function describeParameter(ParameterReflection $parameter, int|string|null $positionOrNamed): string
{
$parts = [];
if ($position !== null) {
$parts[] = '#' . $position;
if (is_int($positionOrNamed)) {
$parts[] = 'Parameter #' . $positionOrNamed;
} elseif ($parameter->isVariadic() && is_string($positionOrNamed)) {
$parts[] = 'Named argument ' . $positionOrNamed . ' for variadic parameter';
} else {
$parts[] = 'Parameter';
}

$name = $parameter->getName();
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Functions/CallCallablesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ public function processNode(
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
'%s of ' . $callableDescription . ' expects %s, %s given.',
'Result of ' . $callableDescription . ' (void) is used.',
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
'%s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to ' . $callableDescription,
'Missing parameter $%s in call to ' . $callableDescription . '.',
'Unknown parameter $%s in call to ' . $callableDescription . '.',
'Return type of call to ' . $callableDescription . ' contains unresolvable type.',
'Parameter %s of ' . $callableDescription . ' contains unresolvable type.',
'%s of ' . $callableDescription . ' contains unresolvable type.',
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
),
);
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Functions/CallToFunctionParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public function processNode(Node $node, Scope $scope): array
'Function ' . $functionName . ' invoked with %d parameters, at least %d required.',
'Function ' . $functionName . ' invoked with %d parameter, %d-%d required.',
'Function ' . $functionName . ' invoked with %d parameters, %d-%d required.',
'Parameter %s of function ' . $functionName . ' expects %s, %s given.',
'%s of function ' . $functionName . ' expects %s, %s given.',
'Result of function ' . $functionName . ' (void) is used.',
'Parameter %s of function ' . $functionName . ' is passed by reference, so it expects variables only.',
'%s of function ' . $functionName . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to function ' . $functionName,
'Missing parameter $%s in call to function ' . $functionName . '.',
'Unknown parameter $%s in call to function ' . $functionName . '.',
'Return type of call to function ' . $functionName . ' contains unresolvable type.',
'Parameter %s of function ' . $functionName . ' contains unresolvable type.',
'%s of function ' . $functionName . ' contains unresolvable type.',
'Function ' . $functionName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Functions/CallUserFuncRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public function processNode(Node $node, Scope $scope): array
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
'%s of ' . $callableDescription . ' expects %s, %s given.',
'Result of ' . $callableDescription . ' (void) is used.',
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
'%s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to ' . $callableDescription,
'Missing parameter $%s in call to ' . $callableDescription . '.',
'Unknown parameter $%s in call to ' . $callableDescription . '.',
'Return type of call to ' . $callableDescription . ' contains unresolvable type.',
'Parameter %s of ' . $callableDescription . ' contains unresolvable type.',
'%s of ' . $callableDescription . ' contains unresolvable type.',
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Methods/CallMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public function processNode(Node $node, Scope $scope): array
'Method ' . $messagesMethodName . ' invoked with %d parameters, at least %d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameter, %d-%d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameters, %d-%d required.',
'Parameter %s of method ' . $messagesMethodName . ' expects %s, %s given.',
'%s of method ' . $messagesMethodName . ' expects %s, %s given.',
'Result of method ' . $messagesMethodName . ' (void) is used.',
'Parameter %s of method ' . $messagesMethodName . ' is passed by reference, so it expects variables only.',
'%s of method ' . $messagesMethodName . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to method ' . $messagesMethodName,
'Missing parameter $%s in call to method ' . $messagesMethodName . '.',
'Unknown parameter $%s in call to method ' . $messagesMethodName . '.',
'Return type of call to method ' . $messagesMethodName . ' contains unresolvable type.',
'Parameter %s of method ' . $messagesMethodName . ' contains unresolvable type.',
'%s of method ' . $messagesMethodName . ' contains unresolvable type.',
'Method ' . $messagesMethodName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public function processNode(Node $node, Scope $scope): array
$displayMethodName . ' invoked with %d parameters, at least %d required.',
$displayMethodName . ' invoked with %d parameter, %d-%d required.',
$displayMethodName . ' invoked with %d parameters, %d-%d required.',
'Parameter %s of ' . $lowercasedMethodName . ' expects %s, %s given.',
'%s of ' . $lowercasedMethodName . ' expects %s, %s given.',
'Result of ' . $lowercasedMethodName . ' (void) is used.',
'Parameter %s of ' . $lowercasedMethodName . ' is passed by reference, so it expects variables only.',
'%s of ' . $lowercasedMethodName . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to method ' . $lowercasedMethodName,
'Missing parameter $%s in call to ' . $lowercasedMethodName . '.',
'Unknown parameter $%s in call to ' . $lowercasedMethodName . '.',
'Return type of call to ' . $lowercasedMethodName . ' contains unresolvable type.',
'Parameter %s of ' . $lowercasedMethodName . ' contains unresolvable type.',
'%s of ' . $lowercasedMethodName . ' contains unresolvable type.',
$displayMethodName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
));

Expand Down
10 changes: 9 additions & 1 deletion tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ public function testNamedArguments(): void
91,
],
[
'Parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given.',
'Named argument foo for variadic parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given.',
91,
],
[
Expand All @@ -1922,6 +1922,14 @@ public function testNamedArguments(): void
'Unpacked argument (...) cannot be followed by a non-unpacked argument.',
94,
],
[
'Named argument foo for variadic parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given.',
95,
],
[
'Named argument bar for variadic parameter ...$args of method NamedArgumentsMethod\Foo::doIpsum() expects string, int given.',
95,
],
]);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Methods/data/named-arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function doDolor(): void
$this->doIpsum(...['a' => 1, 'foo' => 'foo']);
$this->doIpsum(...['b' => 1, 'foo' => 'foo']);
$this->doIpsum(...[1, 2], 'foo');
$this->doIpsum(1, 2, foo: 1, bar: 2);
}

}
Loading