Skip to content

Commit a881e98

Browse files
authored
Simplify default-return path in extensions
1 parent 9463e66 commit a881e98

6 files changed

+17
-33
lines changed

src/Type/Php/ArrayIntersectKeyFunctionReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Php\PhpVersion;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\NeverType;
1211
use PHPStan\Type\NullType;
@@ -27,11 +26,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
2726
return $functionReflection->getName() === 'array_intersect_key';
2827
}
2928

30-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
29+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
3130
{
3231
$args = $functionCall->getArgs();
3332
if (count($args) === 0) {
34-
return ParametersAcceptorSelector::selectFromArgs($scope, $args, $functionReflection->getVariants())->getReturnType();
33+
return null;
3534
}
3635

3736
$argTypes = [];

src/Type/Php/ArraySpliceFunctionReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\ArrayType;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\Type;
@@ -22,10 +21,10 @@ public function getTypeFromFunctionCall(
2221
FunctionReflection $functionReflection,
2322
FuncCall $functionCall,
2423
Scope $scope,
25-
): Type
24+
): ?Type
2625
{
2726
if (!isset($functionCall->getArgs()[0])) {
28-
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
27+
return null;
2928
}
3029

3130
$arrayArg = $scope->getType($functionCall->getArgs()[0]->value);

src/Type/Php/ClosureFromCallableDynamicReturnTypeExtension.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Expr\StaticCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
1110
use PHPStan\Type\ClosureType;
1211
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
@@ -28,14 +27,10 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
2827
return $methodReflection->getName() === 'fromCallable';
2928
}
3029

31-
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
30+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
3231
{
3332
if (!isset($methodCall->getArgs()[0])) {
34-
return ParametersAcceptorSelector::selectFromArgs(
35-
$scope,
36-
$methodCall->getArgs(),
37-
$methodReflection->getVariants(),
38-
)->getReturnType();
33+
return null;
3934
}
4035

4136
$callableType = $scope->getType($methodCall->getArgs()[0]->value);

src/Type/Php/DsMapDynamicReturnTypeExtension.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\DynamicMethodReturnTypeExtension;
109
use PHPStan\Type\Type;
1110
use PHPStan\Type\TypeWithClassName;
@@ -25,41 +24,35 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2524
return in_array($methodReflection->getName(), ['get', 'remove'], true);
2625
}
2726

28-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
27+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
2928
{
30-
$returnType = ParametersAcceptorSelector::selectFromArgs(
31-
$scope,
32-
$methodCall->getArgs(),
33-
$methodReflection->getVariants(),
34-
)->getReturnType();
35-
3629
$argsCount = count($methodCall->getArgs());
3730
if ($argsCount > 1) {
38-
return $returnType;
31+
return null;
3932
}
4033

4134
if ($argsCount === 0) {
42-
return $returnType;
35+
return null;
4336
}
4437

4538
$mapType = $scope->getType($methodCall->var);
4639
if (!$mapType instanceof TypeWithClassName) {
47-
return $returnType;
40+
return null;
4841
}
4942

5043
$mapAncestor = $mapType->getAncestorWithClassName('Ds\Map');
5144
if ($mapAncestor === null) {
52-
return $returnType;
45+
return null;
5346
}
5447

5548
$mapAncestorClass = $mapAncestor->getClassReflection();
5649
if ($mapAncestorClass === null) {
57-
return $returnType;
50+
return null;
5851
}
5952

6053
$valueType = $mapAncestorClass->getActiveTemplateTypeMap()->getType('TValue');
6154
if ($valueType === null) {
62-
return $returnType;
55+
return null;
6356
}
6457

6558
return $valueType;

src/Type/Php/RandomIntFunctionReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\Constant\ConstantIntegerType;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\IntegerRangeType;
@@ -26,14 +25,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
2625
return in_array($functionReflection->getName(), ['random_int', 'rand', 'mt_rand'], true);
2726
}
2827

29-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
28+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
3029
{
3130
if (in_array($functionReflection->getName(), ['rand', 'mt_rand'], true) && count($functionCall->getArgs()) === 0) {
3231
return IntegerRangeType::fromInterval(0, null);
3332
}
3433

3534
if (count($functionCall->getArgs()) < 2) {
36-
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
35+
return null;
3736
}
3837

3938
$minType = $scope->getType($functionCall->getArgs()[0]->value)->toInteger();

src/Type/Php/VersionCompareFunctionDynamicReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\BooleanType;
109
use PHPStan\Type\Constant\ConstantBooleanType;
1110
use PHPStan\Type\Constant\ConstantIntegerType;
@@ -28,10 +27,10 @@ public function getTypeFromFunctionCall(
2827
FunctionReflection $functionReflection,
2928
FuncCall $functionCall,
3029
Scope $scope,
31-
): Type
30+
): ?Type
3231
{
3332
if (count($functionCall->getArgs()) < 2) {
34-
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
33+
return null;
3534
}
3635

3736
$version1Strings = $scope->getType($functionCall->getArgs()[0]->value)->getConstantStrings();

0 commit comments

Comments
 (0)