Skip to content

Commit 710e09c

Browse files
committed
Refactored FunctionCallParametersCheck::check() parameters
1 parent b539c74 commit 710e09c

8 files changed

+128
-122
lines changed

src/Rules/AttributesCheck.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,23 @@ public function check(
136136
$scope,
137137
$attributeConstructor->getDeclaringClass()->isBuiltin(),
138138
new New_($attribute->name, $attribute->args, $nodeAttributes),
139-
[
140-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d required.',
141-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d required.',
142-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, at least %d required.',
143-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, at least %d required.',
144-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d-%d required.',
145-
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d-%d required.',
146-
'Parameter %s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given.',
147-
'', // constructor does not have a return type
148-
'Parameter %s of attribute class ' . $attributeClassName . ' constructor is passed by reference, so it expects variables only',
149-
'Unable to resolve the template type %s in instantiation of attribute class ' . $attributeClassName,
150-
'Missing parameter $%s in call to ' . $attributeClassName . ' constructor.',
151-
'Unknown parameter $%s in call to ' . $attributeClassName . ' constructor.',
152-
'Return type of call to ' . $attributeClassName . ' constructor contains unresolvable type.',
153-
'Parameter %s of attribute class ' . $attributeClassName . ' constructor contains unresolvable type.',
154-
'Attribute class ' . $attributeClassName . ' constructorinvoked with %s, but it\'s not allowed because of @no-named-arguments.',
155-
],
156139
'attribute',
157140
$attributeConstructor->acceptsNamedArguments(),
141+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d required.',
142+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d required.',
143+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, at least %d required.',
144+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, at least %d required.',
145+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameter, %d-%d required.',
146+
'Attribute class ' . $attributeClassName . ' constructor invoked with %d parameters, %d-%d required.',
147+
'Parameter %s of attribute class ' . $attributeClassName . ' constructor expects %s, %s given.',
148+
'', // constructor does not have a return type
149+
'Parameter %s of attribute class ' . $attributeClassName . ' constructor is passed by reference, so it expects variables only',
150+
'Unable to resolve the template type %s in instantiation of attribute class ' . $attributeClassName,
151+
'Missing parameter $%s in call to ' . $attributeClassName . ' constructor.',
152+
'Unknown parameter $%s in call to ' . $attributeClassName . ' constructor.',
153+
'Return type of call to ' . $attributeClassName . ' constructor contains unresolvable type.',
154+
'Parameter %s of attribute class ' . $attributeClassName . ' constructor contains unresolvable type.',
155+
'Attribute class ' . $attributeClassName . ' constructorinvoked with %s, but it\'s not allowed because of @no-named-arguments.',
158156
);
159157

160158
foreach ($parameterErrors as $error) {

src/Rules/Classes/InstantiationRule.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -201,25 +201,23 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
201201
$scope,
202202
$constructorReflection->getDeclaringClass()->isBuiltin(),
203203
$node,
204-
[
205-
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d required.',
206-
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d required.',
207-
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, at least %d required.',
208-
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, at least %d required.',
209-
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d-%d required.',
210-
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d-%d required.',
211-
'Parameter %s of class ' . $classDisplayName . ' constructor expects %s, %s given.',
212-
'', // constructor does not have a return type
213-
'Parameter %s of class ' . $classDisplayName . ' constructor is passed by reference, so it expects variables only',
214-
'Unable to resolve the template type %s in instantiation of class ' . $classDisplayName,
215-
'Missing parameter $%s in call to ' . $classDisplayName . ' constructor.',
216-
'Unknown parameter $%s in call to ' . $classDisplayName . ' constructor.',
217-
'Return type of call to ' . $classDisplayName . ' constructor contains unresolvable type.',
218-
'Parameter %s of class ' . $classDisplayName . ' constructor contains unresolvable type.',
219-
'Class ' . $classDisplayName . ' constructor invoked with %s, but it\'s not allowed because of @no-named-arguments.',
220-
],
221204
'new',
222205
$constructorReflection->acceptsNamedArguments(),
206+
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d required.',
207+
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d required.',
208+
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, at least %d required.',
209+
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, at least %d required.',
210+
'Class ' . $classDisplayName . ' constructor invoked with %d parameter, %d-%d required.',
211+
'Class ' . $classDisplayName . ' constructor invoked with %d parameters, %d-%d required.',
212+
'Parameter %s of class ' . $classDisplayName . ' constructor expects %s, %s given.',
213+
'', // constructor does not have a return type
214+
'Parameter %s of class ' . $classDisplayName . ' constructor is passed by reference, so it expects variables only',
215+
'Unable to resolve the template type %s in instantiation of class ' . $classDisplayName,
216+
'Missing parameter $%s in call to ' . $classDisplayName . ' constructor.',
217+
'Unknown parameter $%s in call to ' . $classDisplayName . ' constructor.',
218+
'Return type of call to ' . $classDisplayName . ' constructor contains unresolvable type.',
219+
'Parameter %s of class ' . $classDisplayName . ' constructor contains unresolvable type.',
220+
'Class ' . $classDisplayName . ' constructor invoked with %s, but it\'s not allowed because of @no-named-arguments.',
223221
));
224222
}
225223

src/Rules/FunctionCallParametersCheck.php

+30-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public function __construct(
5353

5454
/**
5555
* @param Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ $funcCall
56-
* @param array{0: string, 1: string, 2: string, 3: string, 4: string, 5: string, 6: string, 7: string, 8: string, 9: string, 10: string, 11: string, 12: string, 13?: string, 14?: string} $messages
5756
* @param 'attribute'|'callable'|'method'|'staticMethod'|'function'|'new' $nodeType
5857
* @return list<IdentifierRuleError>
5958
*/
@@ -62,9 +61,23 @@ public function check(
6261
Scope $scope,
6362
bool $isBuiltin,
6463
$funcCall,
65-
array $messages,
6664
string $nodeType,
6765
TrinaryLogic $acceptsNamedArguments,
66+
string $singleInsufficientParameterMessage,
67+
string $pluralInsufficientParametersMessage,
68+
string $singleInsufficientParameterInVariadicFunctionMessage,
69+
string $pluralInsufficientParametersInVariadicFunctionMessage,
70+
string $singleInsufficientParameterWithOptionalParametersMessage,
71+
string $pluralInsufficientParametersWithOptionalParametersMessage,
72+
string $wrongArgumentTypeMessage,
73+
string $voidReturnTypeUsed,
74+
string $parameterPassedByReferenceMessage,
75+
string $unresolvableTemplateTypeMessage,
76+
string $missingParameterMessage,
77+
string $unknownParameterMessage,
78+
string $unresolvableReturnTypeMessage,
79+
string $unresolvableParameterTypeMessage,
80+
string $namedArgumentMessage,
6881
): array
6982
{
7083
$functionParametersMinCount = 0;
@@ -204,7 +217,7 @@ public function check(
204217
) {
205218
if ($functionParametersMinCount === $functionParametersMaxCount) {
206219
$errors[] = RuleErrorBuilder::message(sprintf(
207-
$invokedParametersCount === 1 ? $messages[0] : $messages[1],
220+
$invokedParametersCount === 1 ? $singleInsufficientParameterMessage : $pluralInsufficientParametersMessage,
208221
$invokedParametersCount,
209222
$functionParametersMinCount,
210223
))
@@ -213,7 +226,7 @@ public function check(
213226
->build();
214227
} elseif ($functionParametersMaxCount === -1 && $invokedParametersCount < $functionParametersMinCount) {
215228
$errors[] = RuleErrorBuilder::message(sprintf(
216-
$invokedParametersCount === 1 ? $messages[2] : $messages[3],
229+
$invokedParametersCount === 1 ? $singleInsufficientParameterInVariadicFunctionMessage : $pluralInsufficientParametersInVariadicFunctionMessage,
217230
$invokedParametersCount,
218231
$functionParametersMinCount,
219232
))
@@ -222,7 +235,7 @@ public function check(
222235
->build();
223236
} elseif ($functionParametersMaxCount !== -1) {
224237
$errors[] = RuleErrorBuilder::message(sprintf(
225-
$invokedParametersCount === 1 ? $messages[4] : $messages[5],
238+
$invokedParametersCount === 1 ? $singleInsufficientParameterWithOptionalParametersMessage : $pluralInsufficientParametersWithOptionalParametersMessage,
226239
$invokedParametersCount,
227240
$functionParametersMinCount,
228241
$functionParametersMaxCount,
@@ -239,13 +252,13 @@ public function check(
239252
&& !$scope->isInFirstLevelStatement()
240253
&& $scope->getKeepVoidType($funcCall)->isVoid()->yes()
241254
) {
242-
$errors[] = RuleErrorBuilder::message($messages[7])
255+
$errors[] = RuleErrorBuilder::message($voidReturnTypeUsed)
243256
->identifier(sprintf('%s.void', $nodeType))
244257
->line($funcCall->getStartLine())
245258
->build();
246259
}
247260

248-
[$addedErrors, $argumentsWithParameters] = $this->processArguments($parametersAcceptor, $funcCall->getStartLine(), $isBuiltin, $arguments, $hasNamedArguments, $messages[10], $messages[11]);
261+
[$addedErrors, $argumentsWithParameters] = $this->processArguments($parametersAcceptor, $funcCall->getStartLine(), $isBuiltin, $arguments, $hasNamedArguments, $missingParameterMessage, $unknownParameterMessage);
249262
foreach ($addedErrors as $error) {
250263
$errors[] = $error;
251264
}
@@ -290,17 +303,17 @@ public function check(
290303
}
291304
}
292305

293-
if (!$acceptsNamedArguments->yes() && isset($messages[14])) {
306+
if (!$acceptsNamedArguments->yes()) {
294307
if ($argumentName !== null) {
295-
$errors[] = RuleErrorBuilder::message(sprintf($messages[14], sprintf('named argument $%s', $argumentName)))
308+
$errors[] = RuleErrorBuilder::message(sprintf($namedArgumentMessage, sprintf('named argument $%s', $argumentName)))
296309
->identifier('argument.named')
297310
->line($argumentLine)
298311
->build();
299312
} elseif ($unpack) {
300313
$unpackedArrayType = $scope->getType($argumentValue);
301314
$hasStringKey = $unpackedArrayType->getIterableKeyType()->isString();
302315
if (!$hasStringKey->no()) {
303-
$errors[] = RuleErrorBuilder::message(sprintf($messages[14], sprintf('unpacked array with %s', $hasStringKey->yes() ? 'string key' : 'possibly string key')))
316+
$errors[] = RuleErrorBuilder::message(sprintf($namedArgumentMessage, sprintf('unpacked array with %s', $hasStringKey->yes() ? 'string key' : 'possibly string key')))
304317
->identifier('argument.named')
305318
->line($argumentLine)
306319
->build();
@@ -317,7 +330,7 @@ public function check(
317330
if (!$accepts->result) {
318331
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
319332
$errors[] = RuleErrorBuilder::message(sprintf(
320-
$messages[6],
333+
$wrongArgumentTypeMessage,
321334
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
322335
$parameterType->describe($verbosityLevel),
323336
$argumentValueType->describe($verbosityLevel),
@@ -331,12 +344,11 @@ public function check(
331344

332345
if (
333346
$originalParameter !== null
334-
&& isset($messages[13])
335347
&& !$this->unresolvableTypeHelper->containsUnresolvableType($originalParameter->getType())
336348
&& $this->unresolvableTypeHelper->containsUnresolvableType($parameterType)
337349
) {
338350
$errors[] = RuleErrorBuilder::message(sprintf(
339-
$messages[13],
351+
$unresolvableParameterTypeMessage,
340352
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
341353
))->identifier('argument.unresolvableType')->line($argumentLine)->build();
342354
}
@@ -348,7 +360,7 @@ public function check(
348360
&& $argumentValue->static
349361
) {
350362
$errors[] = RuleErrorBuilder::message(sprintf(
351-
$messages[6],
363+
$wrongArgumentTypeMessage,
352364
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
353365
'bindable closure',
354366
'static closure',
@@ -368,7 +380,7 @@ public function check(
368380

369381
if ($this->nullsafeCheck->containsNullSafe($argumentValue)) {
370382
$errors[] = RuleErrorBuilder::message(sprintf(
371-
$messages[8],
383+
$parameterPassedByReferenceMessage,
372384
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
373385
))
374386
->identifier('argument.byRef')
@@ -412,7 +424,7 @@ public function check(
412424
}
413425

414426
$errors[] = RuleErrorBuilder::message(sprintf(
415-
$messages[8],
427+
$parameterPassedByReferenceMessage,
416428
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
417429
))->identifier('argument.byRef')->line($argumentLine)->build();
418430
}
@@ -469,7 +481,7 @@ static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Ty
469481
continue;
470482
}
471483

472-
$errors[] = RuleErrorBuilder::message(sprintf($messages[9], $name))
484+
$errors[] = RuleErrorBuilder::message(sprintf($unresolvableTemplateTypeMessage, $name))
473485
->identifier('argument.templateType')
474486
->line($funcCall->getStartLine())
475487
->tip('See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type')
@@ -481,7 +493,7 @@ static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Ty
481493
!$this->unresolvableTypeHelper->containsUnresolvableType($originalParametersAcceptor->getReturnType())
482494
&& $this->unresolvableTypeHelper->containsUnresolvableType($parametersAcceptor->getReturnType())
483495
) {
484-
$errors[] = RuleErrorBuilder::message($messages[12])
496+
$errors[] = RuleErrorBuilder::message($unresolvableReturnTypeMessage)
485497
->identifier(sprintf('%s.unresolvableReturnType', $nodeType))
486498
->line($funcCall->getStartLine())
487499
->build();

src/Rules/Functions/CallCallablesRule.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,23 @@ public function processNode(
118118
$scope,
119119
false,
120120
$node,
121-
[
122-
ucfirst($callableDescription) . ' invoked with %d parameter, %d required.',
123-
ucfirst($callableDescription) . ' invoked with %d parameters, %d required.',
124-
ucfirst($callableDescription) . ' invoked with %d parameter, at least %d required.',
125-
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
126-
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
127-
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
128-
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
129-
'Result of ' . $callableDescription . ' (void) is used.',
130-
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
131-
'Unable to resolve the template type %s in call to ' . $callableDescription,
132-
'Missing parameter $%s in call to ' . $callableDescription . '.',
133-
'Unknown parameter $%s in call to ' . $callableDescription . '.',
134-
'Return type of call to ' . $callableDescription . ' contains unresolvable type.',
135-
'Parameter %s of ' . $callableDescription . ' contains unresolvable type.',
136-
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
137-
],
138121
'callable',
139122
$acceptsNamedArguments,
123+
ucfirst($callableDescription) . ' invoked with %d parameter, %d required.',
124+
ucfirst($callableDescription) . ' invoked with %d parameters, %d required.',
125+
ucfirst($callableDescription) . ' invoked with %d parameter, at least %d required.',
126+
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
127+
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
128+
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
129+
'Parameter %s of ' . $callableDescription . ' expects %s, %s given.',
130+
'Result of ' . $callableDescription . ' (void) is used.',
131+
'Parameter %s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
132+
'Unable to resolve the template type %s in call to ' . $callableDescription,
133+
'Missing parameter $%s in call to ' . $callableDescription . '.',
134+
'Unknown parameter $%s in call to ' . $callableDescription . '.',
135+
'Return type of call to ' . $callableDescription . ' contains unresolvable type.',
136+
'Parameter %s of ' . $callableDescription . ' contains unresolvable type.',
137+
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
140138
),
141139
);
142140
}

src/Rules/Functions/CallToFunctionParametersRule.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,23 @@ public function processNode(Node $node, Scope $scope): array
4949
$scope,
5050
$function->isBuiltin(),
5151
$node,
52-
[
53-
'Function ' . $functionName . ' invoked with %d parameter, %d required.',
54-
'Function ' . $functionName . ' invoked with %d parameters, %d required.',
55-
'Function ' . $functionName . ' invoked with %d parameter, at least %d required.',
56-
'Function ' . $functionName . ' invoked with %d parameters, at least %d required.',
57-
'Function ' . $functionName . ' invoked with %d parameter, %d-%d required.',
58-
'Function ' . $functionName . ' invoked with %d parameters, %d-%d required.',
59-
'Parameter %s of function ' . $functionName . ' expects %s, %s given.',
60-
'Result of function ' . $functionName . ' (void) is used.',
61-
'Parameter %s of function ' . $functionName . ' is passed by reference, so it expects variables only.',
62-
'Unable to resolve the template type %s in call to function ' . $functionName,
63-
'Missing parameter $%s in call to function ' . $functionName . '.',
64-
'Unknown parameter $%s in call to function ' . $functionName . '.',
65-
'Return type of call to function ' . $functionName . ' contains unresolvable type.',
66-
'Parameter %s of function ' . $functionName . ' contains unresolvable type.',
67-
'Function ' . $functionName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
68-
],
6952
'function',
7053
$function->acceptsNamedArguments(),
54+
'Function ' . $functionName . ' invoked with %d parameter, %d required.',
55+
'Function ' . $functionName . ' invoked with %d parameters, %d required.',
56+
'Function ' . $functionName . ' invoked with %d parameter, at least %d required.',
57+
'Function ' . $functionName . ' invoked with %d parameters, at least %d required.',
58+
'Function ' . $functionName . ' invoked with %d parameter, %d-%d required.',
59+
'Function ' . $functionName . ' invoked with %d parameters, %d-%d required.',
60+
'Parameter %s of function ' . $functionName . ' expects %s, %s given.',
61+
'Result of function ' . $functionName . ' (void) is used.',
62+
'Parameter %s of function ' . $functionName . ' is passed by reference, so it expects variables only.',
63+
'Unable to resolve the template type %s in call to function ' . $functionName,
64+
'Missing parameter $%s in call to function ' . $functionName . '.',
65+
'Unknown parameter $%s in call to function ' . $functionName . '.',
66+
'Return type of call to function ' . $functionName . ' contains unresolvable type.',
67+
'Parameter %s of function ' . $functionName . ' contains unresolvable type.',
68+
'Function ' . $functionName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
7169
);
7270
}
7371

0 commit comments

Comments
 (0)