Skip to content

Commit fca0834

Browse files
clxmstaabondrejmirtes
authored andcommitted
Utilize null-default return to simplify extensions
1 parent dac2474 commit fca0834

6 files changed

+30
-46
lines changed

src/Type/Symfony/CommandGetHelperDynamicReturnTypeExtension.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3636
return $methodReflection->getName() === 'getHelper';
3737
}
3838

39-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
39+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4040
{
41-
$defaultReturnType = new ObjectType('Symfony\Component\Console\Helper\HelperInterface');
42-
4341
if (!isset($methodCall->getArgs()[0])) {
44-
return $defaultReturnType;
42+
return null;
4543
}
4644

4745
$classReflection = $scope->getClassReflection();
4846
if ($classReflection === null) {
49-
return $defaultReturnType;
47+
return null;
5048
}
5149

5250
$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
5351
if (count($argStrings) !== 1) {
54-
return $defaultReturnType;
52+
return null;
5553
}
5654
$argName = $argStrings[0]->getValue();
5755

@@ -65,7 +63,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6563
}
6664
}
6765

68-
return count($returnTypes) > 0 ? TypeCombinator::union(...$returnTypes) : $defaultReturnType;
66+
return count($returnTypes) > 0 ? TypeCombinator::union(...$returnTypes) : null;
6967
}
7068

7169
}

src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php

+5-8
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\Symfony\ConsoleApplicationResolver;
1110
use PHPStan\Type\ArrayType;
1211
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -38,22 +37,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3837
return $methodReflection->getName() === 'getArgument';
3938
}
4039

41-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
40+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4241
{
43-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
44-
4542
if (!isset($methodCall->getArgs()[0])) {
46-
return $defaultReturnType;
43+
return null;
4744
}
4845

4946
$classReflection = $scope->getClassReflection();
5047
if ($classReflection === null) {
51-
return $defaultReturnType;
48+
return null;
5249
}
5350

5451
$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
5552
if (count($argStrings) !== 1) {
56-
return $defaultReturnType;
53+
return null;
5754
}
5855
$argName = $argStrings[0]->getValue();
5956

@@ -79,7 +76,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7976
}
8077
}
8178

82-
return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : $defaultReturnType;
79+
return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : null;
8380
}
8481

8582
}

src/Type/Symfony/InputInterfaceGetOptionDynamicReturnTypeExtension.php

+5-8
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\Symfony\ConsoleApplicationResolver;
1110
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1211
use PHPStan\Type\Type;
@@ -39,22 +38,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3938
return $methodReflection->getName() === 'getOption';
4039
}
4140

42-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
41+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4342
{
44-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
45-
4643
if (!isset($methodCall->getArgs()[0])) {
47-
return $defaultReturnType;
44+
return null;
4845
}
4946

5047
$classReflection = $scope->getClassReflection();
5148
if ($classReflection === null) {
52-
return $defaultReturnType;
49+
return null;
5350
}
5451

5552
$optStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
5653
if (count($optStrings) !== 1) {
57-
return $defaultReturnType;
54+
return null;
5855
}
5956
$optName = $optStrings[0]->getValue();
6057

@@ -69,7 +66,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6966
}
7067
}
7168

72-
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : $defaultReturnType;
69+
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : null;
7370
}
7471

7572
}

src/Type/Symfony/InputInterfaceGetOptionsDynamicReturnTypeExtension.php

+3-5
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\Symfony\ConsoleApplicationResolver;
1110
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1211
use PHPStan\Type\Constant\ConstantStringType;
@@ -40,12 +39,11 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4039
return $methodReflection->getName() === 'getOptions';
4140
}
4241

43-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
42+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4443
{
45-
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
4644
$classReflection = $scope->getClassReflection();
4745
if ($classReflection === null) {
48-
return $defaultReturnType;
46+
return null;
4947
}
5048

5149
$optTypes = [];
@@ -65,7 +63,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6563
}
6664
}
6765

68-
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : $defaultReturnType;
66+
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : null;
6967
}
7068

7169
}

src/Type/Symfony/InputInterfaceHasArgumentDynamicReturnTypeExtension.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
99
use PHPStan\Symfony\ConsoleApplicationResolver;
10-
use PHPStan\Type\BooleanType;
1110
use PHPStan\Type\Constant\ConstantBooleanType;
1211
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1312
use PHPStan\Type\Type;
@@ -36,22 +35,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3635
return $methodReflection->getName() === 'hasArgument';
3736
}
3837

39-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
38+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4039
{
41-
$defaultReturnType = new BooleanType();
42-
4340
if (!isset($methodCall->getArgs()[0])) {
44-
return $defaultReturnType;
41+
return null;
4542
}
4643

4744
$classReflection = $scope->getClassReflection();
4845
if ($classReflection === null) {
49-
return $defaultReturnType;
46+
return null;
5047
}
5148

5249
$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
5350
if (count($argStrings) !== 1) {
54-
return $defaultReturnType;
51+
return null;
5552
}
5653
$argName = $argStrings[0]->getValue();
5754

@@ -67,11 +64,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6764
}
6865

6966
if (count($returnTypes) === 0) {
70-
return $defaultReturnType;
67+
return null;
7168
}
7269

7370
$returnTypes = array_unique($returnTypes);
74-
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : $defaultReturnType;
71+
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : null;
7572
}
7673

7774
}

src/Type/Symfony/InputInterfaceHasOptionDynamicReturnTypeExtension.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
99
use PHPStan\Symfony\ConsoleApplicationResolver;
10-
use PHPStan\Type\BooleanType;
1110
use PHPStan\Type\Constant\ConstantBooleanType;
1211
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1312
use PHPStan\Type\Type;
@@ -36,22 +35,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3635
return $methodReflection->getName() === 'hasOption';
3736
}
3837

39-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
38+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4039
{
41-
$defaultReturnType = new BooleanType();
42-
4340
if (!isset($methodCall->getArgs()[0])) {
44-
return $defaultReturnType;
41+
return null;
4542
}
4643

4744
$classReflection = $scope->getClassReflection();
4845
if ($classReflection === null) {
49-
return $defaultReturnType;
46+
return null;
5047
}
5148

5249
$optStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
5350
if (count($optStrings) !== 1) {
54-
return $defaultReturnType;
51+
return null;
5552
}
5653
$optName = $optStrings[0]->getValue();
5754

@@ -67,11 +64,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6764
}
6865

6966
if (count($returnTypes) === 0) {
70-
return $defaultReturnType;
67+
return null;
7168
}
7269

7370
$returnTypes = array_unique($returnTypes);
74-
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : $defaultReturnType;
71+
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : null;
7572
}
7673

7774
}

0 commit comments

Comments
 (0)