Skip to content

Commit 857a5f4

Browse files
phpstan-botondrejmirtes
authored andcommitted
ParametersAcceptorSelector: keep the acceptor-level variadic flag separate from the per-parameter one
* `combineAcceptors()` used a single `$isVariadic` variable both for the variadic flag of the combined variant and for the variadic flag of the parameter currently being merged. Merging a non-variadic acceptor reset the accumulated flag, so the combined variant was returned as non-variadic even though its parameter list still ended in a variadic parameter. The per-parameter flag now lives in its own `$isParameterVariadic` variable. * `combineAcceptors()` also dropped the types of the parameters behind the merged variadic parameter when truncating the parameter list. Their types (plus native and PHPDoc types) are now merged into the variadic parameter, which makes the combined signature independent of the order of the union members. * `IntersectionTypeMethodReflection::getMethodWithMostParameters()` ranked variants by parameter count only, so `IA&IB` and `IB&IA` produced different signatures when one of the methods was variadic. A variadic variant now always wins over a non-variadic one. * Probed and found already correct: named arguments and argument unpacking on union method calls, `Closure::fromCallable()` on unions, `array_map()` with a union first-class callable, and intersections of `callable` PHPDoc types (those collapse in `TypeCombinator`).
1 parent 60f36f0 commit 857a5f4

8 files changed

Lines changed: 219 additions & 4 deletions

File tree

‎src/Reflection/ParametersAcceptorSelector.php‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
846846
continue;
847847
}
848848

849-
$isVariadic = $parameters[$i]->isVariadic() || $parameter->isVariadic();
849+
$isParameterVariadic = $parameters[$i]->isVariadic() || $parameter->isVariadic();
850850
$defaultValueLeft = $parameters[$i]->getDefaultValue();
851851
$defaultValueRight = $parameter->getDefaultValue();
852852
if ($defaultValueLeft !== null && $defaultValueRight !== null) {
@@ -888,6 +888,26 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
888888
$closureThisType = null;
889889
}
890890

891+
if ($isParameterVariadic) {
892+
// the variadic parameter swallows every parameter behind it,
893+
// so their types have to be merged into it before they're dropped
894+
$droppedParameters = array_merge(
895+
array_slice($parameters, $i + 1),
896+
array_slice($acceptor->getParameters(), $i + 1),
897+
);
898+
foreach ($droppedParameters as $droppedParameter) {
899+
$type = TypeCombinator::union($type, $droppedParameter->getType());
900+
if ($droppedParameter instanceof ExtendedParameterReflection) {
901+
$nativeType = TypeCombinator::union($nativeType, $droppedParameter->getNativeType());
902+
$phpDocType = TypeCombinator::union($phpDocType, $droppedParameter->getPhpDocType());
903+
continue;
904+
}
905+
906+
$nativeType = new MixedType();
907+
$phpDocType = TypeCombinator::union($phpDocType, $droppedParameter->getType());
908+
}
909+
}
910+
891911
$allowedConstants = $parameters[$i]->getAllowedConstants();
892912
if ($allowedConstants !== null) {
893913
$otherAllowedConstants = $parameter instanceof ExtendedParameterReflection ? $parameter->getAllowedConstants() : null;
@@ -905,7 +925,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
905925
$type,
906926
$i + 1 > $minimumNumberOfParameters,
907927
$parameters[$i]->passedByReference()->combine($parameter->passedByReference()),
908-
$isVariadic,
928+
$isParameterVariadic,
909929
$defaultValue,
910930
$nativeType,
911931
$phpDocType,
@@ -917,7 +937,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
917937
$pureUnlessCallableIsImpureParameter,
918938
);
919939

920-
if ($isVariadic) {
940+
if ($isParameterVariadic) {
921941
$parameters = array_slice($parameters, 0, $i + 1);
922942
break;
923943
}

‎src/Reflection/Type/IntersectionTypeMethodReflection.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,24 @@ private function getMethodWithMostParameters(): ExtendedMethodReflection
283283

284284
$methodWithMostParameters = $this->methods[0];
285285
$maxParameters = 0;
286+
$maxIsVariadic = false;
286287
foreach ($this->methods as $method) {
287288
foreach ($method->getVariants() as $variant) {
288-
if (count($variant->getParameters()) <= $maxParameters) {
289+
// a variadic signature accepts any number of arguments,
290+
// so it always wins over a non-variadic one
291+
if ($maxIsVariadic && !$variant->isVariadic()) {
292+
continue;
293+
}
294+
295+
if (
296+
$maxIsVariadic === $variant->isVariadic()
297+
&& count($variant->getParameters()) <= $maxParameters
298+
) {
289299
continue;
290300
}
291301

292302
$maxParameters = count($variant->getParameters());
303+
$maxIsVariadic = $variant->isVariadic();
293304
$methodWithMostParameters = $method;
294305
}
295306
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15251Nsrt;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class A
10+
{
11+
public function foo(int $a, int ...$rest): void {}
12+
}
13+
14+
class B
15+
{
16+
public function foo(int $a): void {}
17+
}
18+
19+
class C
20+
{
21+
public function foo(int ...$rest): void {}
22+
}
23+
24+
class D
25+
{
26+
public function foo(int $a, string $b, string $c): void {}
27+
}
28+
29+
function test(A|B $ab, C|B $cb, C|D $cd, D|C $dc): void
30+
{
31+
assertType('Closure(int, int ...): void', $ab->foo(...));
32+
assertType('Closure(int ...): void', $cb->foo(...));
33+
assertType('Closure(int|string ...): void', $cd->foo(...));
34+
assertType('Closure(int|string ...): void', $dc->foo(...));
35+
}

‎tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ public function testBug11935(): void
443443
$this->analyse([__DIR__ . '/data/bug-11935.php'], []);
444444
}
445445

446+
#[RequiresPhp('>= 8.1.0')]
447+
public function testBug15251(): void
448+
{
449+
$this->analyse([__DIR__ . '/../Methods/data/bug-15251.php'], []);
450+
}
451+
446452
#[RequiresPhp('>= 8.0.0')]
447453
public function testBug11935WithCheckExplicitMixed(): void
448454
{

‎tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ public function testNoNamedArguments(): void
183183
]);
184184
}
185185

186+
#[RequiresPhp('>= 8.1.0')]
187+
public function testBug15251(): void
188+
{
189+
$this->analyse([__DIR__ . '/../Methods/data/bug-15251.php'], []);
190+
}
191+
186192
#[RequiresPhp('>= 8.0.0')]
187193
public function testConstantParameterCheckCallUserFunc(): void
188194
{

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,4 +4447,13 @@ public function testBug15166(): void
44474447
$this->analyse([__DIR__ . '/data/bug-15166.php'], []);
44484448
}
44494449

4450+
#[RequiresPhp('>= 8.1.0')]
4451+
public function testBug15251(): void
4452+
{
4453+
$this->checkThisOnly = false;
4454+
$this->checkNullables = true;
4455+
$this->checkUnionTypes = true;
4456+
$this->analyse([__DIR__ . '/data/bug-15251.php'], []);
4457+
}
4458+
44504459
}

‎tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,4 +1093,16 @@ public function testBug10698(): void
10931093
$this->analyse([__DIR__ . '/data/bug-10698.php'], []);
10941094
}
10951095

1096+
#[RequiresPhp('>= 8.1.0')]
1097+
public function testBug15251(): void
1098+
{
1099+
$this->checkThisOnly = false;
1100+
$this->analyse([__DIR__ . '/data/bug-15251.php'], [
1101+
[
1102+
'Static method Bug15251\\A::sfoo() invoked with 0 parameters, at least 1 required.',
1103+
42,
1104+
],
1105+
]);
1106+
}
1107+
10961108
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15251;
6+
7+
class A
8+
{
9+
public function foo(int $a, int ...$rest): void {}
10+
11+
public static function sfoo(int $a, int ...$rest): void {}
12+
}
13+
14+
class B
15+
{
16+
public function foo(int $a): void {}
17+
18+
public static function sfoo(int $a): void {}
19+
}
20+
21+
class C
22+
{
23+
public function foo(int ...$rest): void {}
24+
25+
public static function sfoo(int ...$rest): void {}
26+
}
27+
28+
function test(A|B $ab, C|B $cb): void
29+
{
30+
$cb->foo(1, 2, 3);
31+
$ab->foo(1, 2, 3);
32+
33+
$closure = $ab->foo(...);
34+
$closure(1, 2, 3);
35+
}
36+
37+
function testStatic(A|B $ab, C|B $cb): void
38+
{
39+
$cb::sfoo(1, 2, 3);
40+
$ab::sfoo(1, 2, 3);
41+
42+
$ab::sfoo();
43+
}
44+
45+
function testCallable(A|B $ab): void
46+
{
47+
$callable = [$ab, 'foo'];
48+
$callable(1, 2, 3);
49+
50+
call_user_func([$ab, 'foo'], 1, 2, 3);
51+
}
52+
53+
interface IA
54+
{
55+
public function foo(int ...$rest): void;
56+
}
57+
58+
interface IB
59+
{
60+
public function foo(int $a): void;
61+
}
62+
63+
function testIntersection(IA&IB $ab, IB&IA $ba): void
64+
{
65+
$ab->foo(1, 2, 3);
66+
$ba->foo(1, 2, 3);
67+
}
68+
69+
interface IInvokeA
70+
{
71+
public function __invoke(int $a, int ...$rest): void;
72+
}
73+
74+
interface IInvokeB
75+
{
76+
public function __invoke(int $a): void;
77+
}
78+
79+
function testInvoke(IInvokeA|IInvokeB $union, IInvokeA&IInvokeB $intersection): void
80+
{
81+
$union(1, 2, 3);
82+
$intersection(1, 2, 3);
83+
}
84+
85+
interface IRefA
86+
{
87+
public function bar(int $a, int &...$rest): void;
88+
}
89+
90+
interface IRefB
91+
{
92+
public function bar(int $a): void;
93+
}
94+
95+
function testByRefVariadic(IRefA|IRefB $ab): void
96+
{
97+
$b = 2;
98+
$c = 3;
99+
$ab->bar(1, $b, $c);
100+
}
101+
102+
interface IManyA
103+
{
104+
public function foo(int ...$rest): void;
105+
}
106+
107+
interface IManyB
108+
{
109+
public function foo(int $a, string $b, string $c): void;
110+
}
111+
112+
function testDroppedParameters(IManyA|IManyB $ab, IManyB|IManyA $ba): void
113+
{
114+
$ab->foo(1, 'a', 'b', 'c');
115+
$ba->foo(1, 'a', 'b', 'c');
116+
}

0 commit comments

Comments
 (0)