Skip to content

Commit 5c3a711

Browse files
committed
Merge remote-tracking branch 'origin/1.10.x' into 1.11.x
2 parents 70fb8fc + f71da02 commit 5c3a711

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use function array_fill;
2828
use function array_key_exists;
2929
use function count;
30+
use function implode;
3031
use function is_string;
3132
use function max;
3233
use function sprintf;
@@ -278,7 +279,6 @@ public function check(
278279

279280
if ($this->checkArgumentTypes) {
280281
$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());
281-
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
282282

283283
if (
284284
!$parameter->passedByReference()->createsNewVariable()
@@ -290,11 +290,7 @@ public function check(
290290
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
291291
$errors[] = RuleErrorBuilder::message(sprintf(
292292
$messages[6],
293-
$argumentName === null ? sprintf(
294-
'#%d %s',
295-
$i + 1,
296-
$parameterDescription,
297-
) : $parameterDescription,
293+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
298294
$parameterType->describe($verbosityLevel),
299295
$argumentValueType->describe($verbosityLevel),
300296
))
@@ -313,11 +309,7 @@ public function check(
313309
) {
314310
$errors[] = RuleErrorBuilder::message(sprintf(
315311
$messages[13],
316-
$argumentName === null ? sprintf(
317-
'#%d %s',
318-
$i + 1,
319-
$parameterDescription,
320-
) : $parameterDescription,
312+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
321313
))->identifier('argument.unresolvableType')->line($argumentLine)->build();
322314
}
323315

@@ -329,11 +321,7 @@ public function check(
329321
) {
330322
$errors[] = RuleErrorBuilder::message(sprintf(
331323
$messages[6],
332-
$argumentName === null ? sprintf(
333-
'#%d %s',
334-
$i + 1,
335-
$parameterDescription,
336-
) : $parameterDescription,
324+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
337325
'bindable closure',
338326
'static closure',
339327
))
@@ -351,10 +339,9 @@ public function check(
351339
}
352340

353341
if ($this->nullsafeCheck->containsNullSafe($argumentValue)) {
354-
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
355342
$errors[] = RuleErrorBuilder::message(sprintf(
356343
$messages[8],
357-
$argumentName === null ? sprintf('#%d %s', $i + 1, $parameterDescription) : $parameterDescription,
344+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
358345
))
359346
->identifier('argument.byRef')
360347
->line($argumentLine)
@@ -381,10 +368,9 @@ public function check(
381368
$propertyDescription = sprintf('readonly property %s::$%s', $propertyReflection->getDeclaringClass()->getDisplayName(), $propertyReflection->getName());
382369
}
383370

384-
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
385371
$errors[] = RuleErrorBuilder::message(sprintf(
386372
'Parameter %s is passed by reference so it does not accept %s.',
387-
$argumentName === null ? sprintf('#%d %s', $i + 1, $parameterDescription) : $parameterDescription,
373+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
388374
$propertyDescription,
389375
))->identifier('argument.byRef')->line($argumentLine)->build();
390376
}
@@ -397,10 +383,9 @@ public function check(
397383
continue;
398384
}
399385

400-
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
401386
$errors[] = RuleErrorBuilder::message(sprintf(
402387
$messages[8],
403-
$argumentName === null ? sprintf('#%d %s', $i + 1, $parameterDescription) : $parameterDescription,
388+
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
404389
))->identifier('argument.byRef')->line($argumentLine)->build();
405390
}
406391

@@ -600,4 +585,19 @@ private function processArguments(
600585
return [$errors, $newArguments];
601586
}
602587

588+
private function describeParameter(ParameterReflection $parameter, ?int $position): string
589+
{
590+
$parts = [];
591+
if ($position !== null) {
592+
$parts[] = '#' . $position;
593+
}
594+
595+
$name = $parameter->getName();
596+
if ($name !== '') {
597+
$parts[] = ($parameter->isVariadic() ? '...$' : '$') . $name;
598+
}
599+
600+
return implode(' ', $parts);
601+
}
602+
603603
}

tests/PHPStan/Levels/data/callableVariance-5.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"message": "Parameter #1 $ of callable callable(Levels\\CallableVariance\\B): void expects Levels\\CallableVariance\\B, Levels\\CallableVariance\\A given.",
3+
"message": "Parameter #1 of callable callable(Levels\\CallableVariance\\B): void expects Levels\\CallableVariance\\B, Levels\\CallableVariance\\A given.",
44
"line": 14,
55
"ignorable": true
66
},
@@ -39,4 +39,4 @@
3939
"line": 85,
4040
"ignorable": true
4141
}
42-
]
42+
]

tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function dataBug3566(): array
190190
true,
191191
[
192192
[
193-
'Parameter #1 $ of closure expects int, TMemberType given.',
193+
'Parameter #1 of closure expects int, TMemberType given.',
194194
29,
195195
],
196196
],
@@ -280,7 +280,7 @@ public function testBug6485(): void
280280
{
281281
$this->analyse([__DIR__ . '/data/bug-6485.php'], [
282282
[
283-
'Parameter #1 $ of closure expects never, TBlockType of Bug6485\Block given.',
283+
'Parameter #1 of closure expects never, TBlockType of Bug6485\Block given.',
284284
33,
285285
],
286286
]);
@@ -306,4 +306,14 @@ public function testBug9614(): void
306306
$this->analyse([__DIR__ . '/data/bug-9614.php'], []);
307307
}
308308

309+
public function testBug10814(): void
310+
{
311+
$this->analyse([__DIR__ . '/data/bug-10814.php'], [
312+
[
313+
'Parameter #1 of closure expects DateTime, DateTimeImmutable given.',
314+
10,
315+
],
316+
]);
317+
}
318+
309319
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10814;
4+
5+
class HelloWorld
6+
{
7+
/** @param \Closure(\DateTime): void $fx */
8+
public function foo($fx): void
9+
{
10+
$fx(new \DateTimeImmutable());
11+
}
12+
}

0 commit comments

Comments
 (0)