Skip to content

Commit 7f69137

Browse files
committed
Remove ParametersAcceptorSelector::selectSingle()
1 parent e880a75 commit 7f69137

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,6 @@
6060
class ParametersAcceptorSelector
6161
{
6262

63-
/**
64-
* @deprecated See https://github.com/phpstan/phpstan-src/blob/2.0.x/UPGRADING.md#removed-deprecated-parametersacceptorselectorselectsingle
65-
*
66-
* @template T of ParametersAcceptor
67-
* @param T[] $parametersAcceptors
68-
* @return T
69-
*/
70-
public static function selectSingle(
71-
array $parametersAcceptors,
72-
): ParametersAcceptor
73-
{
74-
$count = count($parametersAcceptors);
75-
if ($count === 0) {
76-
throw new ShouldNotHappenException(
77-
'getVariants() must return at least one variant.',
78-
);
79-
}
80-
if ($count !== 1) {
81-
throw new ShouldNotHappenException('Multiple variants - use selectFromArgs() instead.');
82-
}
83-
84-
return $parametersAcceptors[0];
85-
}
86-
8763
/**
8864
* @param Node\Arg[] $args
8965
* @param ParametersAcceptor[] $parametersAcceptors

tests/PHPStan/Analyser/data/TestDynamicReturnTypeExtensions.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,28 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4141
{
4242
$args = $methodCall->args;
4343
if (count($args) === 0) {
44-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
return ParametersAcceptorSelector::selectFromArgs(
45+
$scope,
46+
$methodCall->getArgs(),
47+
$methodReflection->getVariants(),
48+
)->getReturnType();
4549
}
4650

4751
$arg = $args[0]->value;
4852
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
49-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
53+
return ParametersAcceptorSelector::selectFromArgs(
54+
$scope,
55+
$methodCall->getArgs(),
56+
$methodReflection->getVariants(),
57+
)->getReturnType();
5058
}
5159

5260
if (!($arg->class instanceof \PhpParser\Node\Name)) {
53-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
61+
return ParametersAcceptorSelector::selectFromArgs(
62+
$scope,
63+
$methodCall->getArgs(),
64+
$methodReflection->getVariants(),
65+
)->getReturnType();
5466
}
5567

5668
return new ObjectType((string) $arg->class);
@@ -75,12 +87,20 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7587
{
7688
$args = $methodCall->args;
7789
if (count($args) === 0) {
78-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
90+
return ParametersAcceptorSelector::selectFromArgs(
91+
$scope,
92+
$methodCall->getArgs(),
93+
$methodReflection->getVariants(),
94+
)->getReturnType();
7995
}
8096

8197
$argType = $scope->getType($args[0]->value);
8298
if (!$argType instanceof ConstantStringType) {
83-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
99+
return ParametersAcceptorSelector::selectFromArgs(
100+
$scope,
101+
$methodCall->getArgs(),
102+
$methodReflection->getVariants(),
103+
)->getReturnType();
84104
}
85105

86106
return new ObjectType($argType->getValue());
@@ -105,16 +125,28 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
105125
{
106126
$args = $methodCall->args;
107127
if (count($args) === 0) {
108-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
128+
return ParametersAcceptorSelector::selectFromArgs(
129+
$scope,
130+
$methodCall->getArgs(),
131+
$methodReflection->getVariants(),
132+
)->getReturnType();
109133
}
110134

111135
$arg = $args[0]->value;
112136
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
113-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
137+
return ParametersAcceptorSelector::selectFromArgs(
138+
$scope,
139+
$methodCall->getArgs(),
140+
$methodReflection->getVariants(),
141+
)->getReturnType();
114142
}
115143

116144
if (!($arg->class instanceof \PhpParser\Node\Name)) {
117-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
145+
return ParametersAcceptorSelector::selectFromArgs(
146+
$scope,
147+
$methodCall->getArgs(),
148+
$methodReflection->getVariants(),
149+
)->getReturnType();
118150
}
119151

120152
return new ObjectType((string) $arg->class);
@@ -215,7 +247,11 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
215247

216248
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
217249
{
218-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
250+
return ParametersAcceptorSelector::selectFromArgs(
251+
$scope,
252+
$methodCall->getArgs(),
253+
$methodReflection->getVariants(),
254+
)->getReturnType();
219255
}
220256

221257
}

tests/PHPStan/Reflection/UnionTypesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testUnionTypes(): void
2222
$this->assertSame('bool|int', $propertyType->describe(VerbosityLevel::precise()));
2323

2424
$method = $class->getNativeMethod('doFoo');
25-
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
25+
$methodVariant = $method->getOnlyVariant();
2626
$methodReturnType = $methodVariant->getReturnType();
2727
$this->assertInstanceOf(UnionType::class, $methodReturnType);
2828
$this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $methodReturnType->describe(VerbosityLevel::precise()));
@@ -32,7 +32,7 @@ public function testUnionTypes(): void
3232
$this->assertSame('bool|int', $methodParameterType->describe(VerbosityLevel::precise()));
3333

3434
$function = $reflectionProvider->getFunction(new Name('NativeUnionTypes\doFoo'), null);
35-
$functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants());
35+
$functionVariant = $function->getOnlyVariant();
3636
$functionReturnType = $functionVariant->getReturnType();
3737
$this->assertInstanceOf(UnionType::class, $functionReturnType);
3838
$this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $functionReturnType->describe(VerbosityLevel::precise()));

tests/PHPStan/Rules/Api/data/static-call-out-of-phpstan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function doFoo(): void
1919

2020
public function doBar(FunctionReflection $f): void
2121
{
22-
ParametersAcceptorSelector::selectSingle($f->getVariants()); // @api above class
22+
ParametersAcceptorSelector::selectFromArgs($f->getVariants()); // @api above class
2323
ScopeContext::create(__DIR__ . '/test.php'); // @api above method
2424
}
2525

0 commit comments

Comments
 (0)