Skip to content

Commit 29340e1

Browse files
staabmondrejmirtes
authored andcommitted
Simplify default-return path in Extensions
1 parent c997ea9 commit 29340e1

11 files changed

+32
-54
lines changed

Diff for: src/Type/Php/ClosureBindDynamicReturnTypeExtension.php

+2-3
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\Type\ClosureType;
1110
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
1211
use PHPStan\Type\Type;
@@ -24,11 +23,11 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
2423
return $methodReflection->getName() === 'bind';
2524
}
2625

27-
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
26+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
2827
{
2928
$closureType = $scope->getType($methodCall->getArgs()[0]->value);
3029
if (!($closureType instanceof ClosureType)) {
31-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
30+
return null;
3231
}
3332

3433
return $closureType;

Diff for: src/Type/Php/ClosureBindToDynamicReturnTypeExtension.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Expr\MethodCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Type\ClosureType;
1110
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1211
use PHPStan\Type\Type;
@@ -24,11 +23,11 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2423
return $methodReflection->getName() === 'bindTo';
2524
}
2625

27-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
26+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
2827
{
2928
$closureType = $scope->getType($methodCall->var);
3029
if (!($closureType instanceof ClosureType)) {
31-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
30+
return null;
3231
}
3332

3433
return $closureType;

Diff for: src/Type/Php/CompactFunctionReturnTypeExtension.php

+4-6
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\ConstantArrayType;
109
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1110
use PHPStan\Type\Constant\ConstantStringType;
@@ -30,23 +29,22 @@ public function getTypeFromFunctionCall(
3029
FunctionReflection $functionReflection,
3130
FuncCall $functionCall,
3231
Scope $scope,
33-
): Type
32+
): ?Type
3433
{
35-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
3634
if (count($functionCall->getArgs()) === 0) {
37-
return $defaultReturnType;
35+
return null;
3836
}
3937

4038
if ($scope->canAnyVariableExist() && !$this->checkMaybeUndefinedVariables) {
41-
return $defaultReturnType;
39+
return null;
4240
}
4341

4442
$array = ConstantArrayTypeBuilder::createEmpty();
4543
foreach ($functionCall->getArgs() as $arg) {
4644
$type = $scope->getType($arg->value);
4745
$constantStrings = $this->findConstantStrings($type);
4846
if ($constantStrings === null) {
49-
return $defaultReturnType;
47+
return null;
5048
}
5149
foreach ($constantStrings as $constantString) {
5250
$has = $scope->hasVariableType($constantString->getValue());

Diff for: src/Type/Php/CurlGetinfoFunctionDynamicReturnTypeExtension.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node\Name;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Reflection\ReflectionProvider;
1110
use PHPStan\Type\ArrayType;
1211
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
@@ -37,12 +36,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3736
return $functionReflection->getName() === 'curl_getinfo';
3837
}
3938

40-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
39+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4140
{
4241
if (count($functionCall->getArgs()) < 1) {
43-
return ParametersAcceptorSelector::selectSingle(
44-
$functionReflection->getVariants(),
45-
)->getReturnType();
42+
return null;
4643
}
4744

4845
if (count($functionCall->getArgs()) <= 1) {

Diff for: src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Reflection\ReflectionProvider;
1110
use PHPStan\Type\ClassStringType;
1211
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -37,14 +36,11 @@ public function getTypeFromFunctionCall(
3736
FunctionReflection $functionReflection,
3837
FuncCall $functionCall,
3938
Scope $scope,
40-
): Type
39+
): ?Type
4140
{
42-
$defaultReturnType = ParametersAcceptorSelector::selectSingle(
43-
$functionReflection->getVariants(),
44-
)->getReturnType();
4541
if (count($functionCall->getArgs()) === 0) {
4642
if ($scope->isInTrait()) {
47-
return $defaultReturnType;
43+
return null;
4844
}
4945
if ($scope->isInClass()) {
5046
return $this->findParentClassType(
@@ -57,7 +53,7 @@ public function getTypeFromFunctionCall(
5753

5854
$argType = $scope->getType($functionCall->getArgs()[0]->value);
5955
if ($scope->isInTrait() && TypeUtils::findThisType($argType) !== null) {
60-
return $defaultReturnType;
56+
return null;
6157
}
6258

6359
$constantStrings = $argType->getConstantStrings();
@@ -70,7 +66,7 @@ public function getTypeFromFunctionCall(
7066
return TypeCombinator::union(...array_map(fn (string $classNames): Type => $this->findParentClassNameType($classNames), $classNames));
7167
}
7268

73-
return $defaultReturnType;
69+
return null;
7470
}
7571

7672
private function findParentClassNameType(string $className): Type

Diff for: src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php

+3-5
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\IntegerType;
@@ -24,11 +23,10 @@ public function getTypeFromFunctionCall(
2423
FunctionReflection $functionReflection,
2524
FuncCall $functionCall,
2625
Scope $scope,
27-
): Type
26+
): ?Type
2827
{
29-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
3028
if (!isset($functionCall->getArgs()[0])) {
31-
return $defaultReturnType;
29+
return null;
3230
}
3331

3432
$argType = $scope->getType($functionCall->getArgs()[0]->value);
@@ -41,7 +39,7 @@ public function getTypeFromFunctionCall(
4139
return new ArrayType(new IntegerType(), new StringType());
4240
}
4341

44-
return $defaultReturnType;
42+
return null;
4543
}
4644

4745
}

Diff for: src/Type/Php/MbStrlenFunctionReturnTypeExtension.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ public function getTypeFromFunctionCall(
5353
FunctionReflection $functionReflection,
5454
FuncCall $functionCall,
5555
Scope $scope,
56-
): Type
56+
): ?Type
5757
{
5858
$args = $functionCall->getArgs();
59-
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
6059
if (count($args) === 0) {
61-
return $returnType;
60+
return null;
6261
}
6362

6463
$encodings = [];

Diff for: src/Type/Php/ParseUrlFunctionDynamicReturnTypeExtension.php

+2-5
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\ShouldNotHappenException;
109
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1110
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -45,12 +44,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
4544
return $functionReflection->getName() === 'parse_url';
4645
}
4746

48-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
47+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4948
{
5049
if (count($functionCall->getArgs()) < 1) {
51-
return ParametersAcceptorSelector::selectSingle(
52-
$functionReflection->getVariants(),
53-
)->getReturnType();
50+
return null;
5451
}
5552

5653
$this->cacheReturnTypes();

Diff for: src/Type/Php/SimpleXMLElementXpathMethodReturnTypeExtension.php

+4-5
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\ArrayType;
109
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1110
use PHPStan\Type\MixedType;
@@ -28,10 +27,10 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2827
return extension_loaded('simplexml') && $methodReflection->getName() === 'xpath';
2928
}
3029

31-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
30+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3231
{
3332
if (!isset($methodCall->getArgs()[0])) {
34-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
33+
return null;
3534
}
3635

3736
$argType = $scope->getType($methodCall->getArgs()[0]->value);
@@ -42,14 +41,14 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4241
$result = @$xmlElement->xpath($constantString->getValue());
4342
if ($result === false) {
4443
// We can't be sure since it's maybe a namespaced xpath
45-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
return null;
4645
}
4746

4847
$argType = TypeCombinator::remove($argType, $constantString);
4948
}
5049

5150
if (!$argType instanceof NeverType) {
52-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
51+
return null;
5352
}
5453

5554
return new ArrayType(new MixedType(), $scope->getType($methodCall->var));

Diff for: src/Type/Php/StrSplitFunctionReturnTypeExtension.php

+4-7
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\ShouldNotHappenException;
1110
use PHPStan\TrinaryLogic;
1211
use PHPStan\Type\Accessory\AccessoryArrayListType;
@@ -44,12 +43,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
4443
return in_array($functionReflection->getName(), ['str_split', 'mb_str_split'], true);
4544
}
4645

47-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
46+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4847
{
49-
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
50-
5148
if (count($functionCall->getArgs()) < 1) {
52-
return $defaultReturnType;
49+
return null;
5350
}
5451

5552
if (count($functionCall->getArgs()) >= 2) {
@@ -71,7 +68,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7168
$values = array_unique(array_map(static fn (ConstantStringType $encoding): string => $encoding->getValue(), $strings));
7269

7370
if (count($values) !== 1) {
74-
return $defaultReturnType;
71+
return null;
7572
}
7673

7774
$encoding = $values[0];
@@ -84,7 +81,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
8481
}
8582

8683
if (!isset($splitLength)) {
87-
return $defaultReturnType;
84+
return null;
8885
}
8986

9087
$stringType = $scope->getType($functionCall->getArgs()[0]->value);

Diff for: src/Type/Php/TypeSpecifyingFunctionsDynamicReturnTypeExtension.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\TypeSpecifier;
88
use PHPStan\Analyser\TypeSpecifierAwareExtension;
99
use PHPStan\Reflection\FunctionReflection;
10-
use PHPStan\Reflection\ParametersAcceptorSelector;
1110
use PHPStan\Reflection\ReflectionProvider;
1211
use PHPStan\Rules\Comparison\ImpossibleCheckTypeHelper;
1312
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -49,18 +48,18 @@ public function getTypeFromFunctionCall(
4948
FunctionReflection $functionReflection,
5049
FuncCall $functionCall,
5150
Scope $scope,
52-
): Type
51+
): ?Type
5352
{
5453
if (count($functionCall->getArgs()) === 0) {
55-
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
54+
return null;
5655
}
5756

5857
$isAlways = $this->getHelper()->findSpecifiedType(
5958
$scope,
6059
$functionCall,
6160
);
6261
if ($isAlways === null) {
63-
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
62+
return null;
6463
}
6564

6665
return new ConstantBooleanType($isAlways);

0 commit comments

Comments
 (0)