Skip to content

Commit e283d3a

Browse files
committed
Use ParametersAcceptorSelector::selectFromArgs() instead of selectSingle() wherever possible
1 parent 865c618 commit e283d3a

9 files changed

+47
-11
lines changed

src/Internal/ContainerDynamicReturnTypeExtension.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3333
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
3434
{
3535
if (count($methodCall->getArgs()) === 0) {
36-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
36+
return ParametersAcceptorSelector::selectFromArgs(
37+
$scope,
38+
$methodCall->getArgs(),
39+
$methodReflection->getVariants(),
40+
)->getReturnType();
3741
}
3842
$argType = $scope->getType($methodCall->getArgs()[0]->value);
3943
if (!$argType instanceof ConstantStringType) {
40-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
return ParametersAcceptorSelector::selectFromArgs(
45+
$scope,
46+
$methodCall->getArgs(),
47+
$methodReflection->getVariants(),
48+
)->getReturnType();
4149
}
4250

4351
$type = new ObjectType($argType->getValue());

src/Type/Php/HashFunctionsReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
8888

8989
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
9090
{
91-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
91+
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
92+
$scope,
93+
$functionCall->getArgs(),
94+
$functionReflection->getVariants(),
95+
)->getReturnType();
9296

9397
if (!isset($functionCall->getArgs()[0])) {
9498
return $defaultReturnType;

src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public function getTypeFromFunctionCall(
5454
): Type
5555
{
5656
$argumentPosition = $this->argumentPositions[$functionReflection->getName()];
57-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
57+
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
58+
$scope,
59+
$functionCall->getArgs(),
60+
$functionReflection->getVariants(),
61+
)->getReturnType();
5862

5963
if ($functionReflection->getName() === 'json_decode') {
6064
$defaultReturnType = $this->narrowTypeForJsonDecode($functionCall, $scope, $defaultReturnType);

src/Type/Php/MbFunctionsReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
4747

4848
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
4949
{
50-
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
50+
$returnType = ParametersAcceptorSelector::selectFromArgs(
51+
$scope,
52+
$functionCall->getArgs(),
53+
$functionReflection->getVariants(),
54+
)->getReturnType();
5155
$positionEncodingParam = $this->encodingPositionMap[$functionReflection->getName()];
5256

5357
if (count($functionCall->getArgs()) < $positionEncodingParam) {

src/Type/Php/MbStrlenFunctionReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ public function getTypeFromFunctionCall(
147147
$range = new ConstantIntegerType(0);
148148
} else {
149149
$range = TypeCombinator::remove(
150-
ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(),
150+
ParametersAcceptorSelector::selectFromArgs(
151+
$scope,
152+
$functionCall->getArgs(),
153+
$functionReflection->getVariants(),
154+
)->getReturnType(),
151155
new ConstantBooleanType(false),
152156
);
153157
}

src/Type/Php/PregFilterFunctionReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
2525

2626
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
2727
{
28-
$defaultReturn = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
28+
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
29+
$scope,
30+
$functionCall->getArgs(),
31+
$functionReflection->getVariants(),
32+
)->getReturnType();
2933

3034
$argsCount = count($functionCall->getArgs());
3135
if ($argsCount < 3) {

src/Type/Php/StrtotimeFunctionReturnTypeExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3131

3232
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3333
{
34-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
34+
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
35+
$scope,
36+
$functionCall->getArgs(),
37+
$functionReflection->getVariants(),
38+
)->getReturnType();
3539
if (count($functionCall->getArgs()) === 0) {
3640
return $defaultReturnType;
3741
}

tests/PHPStan/Analyser/data/MethodCallReturnsBoolExpressionTypeResolverExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function getType(Expr $expr, Scope $scope): ?Type
3434
return null;
3535
}
3636

37-
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
37+
$returnType = ParametersAcceptorSelector::selectFromArgs(
38+
$scope,
39+
$expr->getArgs(),
40+
$methodReflection->getVariants(),
41+
)->getReturnType();
3842

3943
if ($returnType instanceof StringType) {
4044
return null;

tests/PHPStan/Analyser/nsrt/preg_filter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function doFoo1() {
2424

2525
function doFoo2() {
2626
$subject = 123;
27-
assertType('list<string>|string|null', preg_filter('/\d/', '$0', $subject));
27+
assertType('string|null', preg_filter('/\d/', '$0', $subject));
2828

2929
$subject = 123.123;
30-
assertType('list<string>|string|null', preg_filter('/\d/', '$0', $subject));
30+
assertType('string|null', preg_filter('/\d/', '$0', $subject));
3131
}
3232

3333
public function dooFoo3(string $pattern, string $replace) {

0 commit comments

Comments
 (0)