Skip to content

Commit cd7fb67

Browse files
authored
simplify return type extensions (#467)
1 parent d70602f commit cd7fb67

13 files changed

+98
-152
lines changed

src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node\Expr\FuncCall;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\FunctionReflection;
10-
use PHPStan\Reflection\ParametersAcceptorSelector;
1110
use PHPStan\Type\ArrayType;
1211
use PHPStan\Type\Constant\ConstantArrayType;
1312
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
@@ -27,23 +26,22 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
2726
return 'deployer\runmysqlquery' === strtolower($functionReflection->getName());
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();
33-
$defaultReturn = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
3432

3533
if (\count($args) < 2) {
36-
return $defaultReturn;
34+
return null;
3735
}
3836

3937
if ($scope->getType($args[0]->value) instanceof MixedType) {
40-
return $defaultReturn;
38+
return null;
4139
}
4240

4341
$queryReflection = new QueryReflection();
4442
$queryString = $queryReflection->resolveQueryString($args[0]->value, $scope);
4543
if (null === $queryString) {
46-
return $defaultReturn;
44+
return null;
4745
}
4846

4947
$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_NUMERIC);
@@ -56,6 +54,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5654
return TypeCombinator::addNull(new ArrayType(new IntegerType(), $builder->getArray()));
5755
}
5856

59-
return $defaultReturn;
57+
return null;
6058
}
6159
}

src/Extensions/DoctrineConnectionExecuteQueryDynamicReturnTypeExtension.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1514
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1615
use PHPStan\Type\MixedType;
1716
use PHPStan\Type\Type;
@@ -32,26 +31,21 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3231
return \in_array(strtolower($methodReflection->getName()), ['executequery', 'executecachequery'], true);
3332
}
3433

35-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
34+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3635
{
3736
$args = $methodCall->getArgs();
38-
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
39-
$scope,
40-
$methodCall->getArgs(),
41-
$methodReflection->getVariants(),
42-
)->getReturnType();
4337

4438
if (\count($args) < 1) {
45-
return $defaultReturn;
39+
return null;
4640
}
4741

4842
if ($scope->getType($args[0]->value) instanceof MixedType) {
49-
return $defaultReturn;
43+
return null;
5044
}
5145

5246
// make sure we don't report wrong types in doctrine 2.x
5347
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
54-
return $defaultReturn;
48+
return null;
5549
}
5650

5751
$params = null;
@@ -60,15 +54,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6054
}
6155

6256
try {
63-
$resultType = $this->inferType($args[0]->value, $params, $scope);
64-
if (null !== $resultType) {
65-
return $resultType;
66-
}
57+
return $this->inferType($args[0]->value, $params, $scope);
6758
} catch (UnresolvableQueryException $exception) {
6859
// simulation not possible.. use default value
6960
}
7061

71-
return $defaultReturn;
62+
return null;
7263
}
7364

7465
private function inferType(Expr $queryExpr, ?Expr $paramsExpr, Scope $scope): ?Type

src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1514
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1615
use PHPStan\Type\MixedType;
1716
use PHPStan\Type\Type;
@@ -45,26 +44,21 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4544
return \in_array(strtolower($methodReflection->getName()), self::METHODS, true);
4645
}
4746

48-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
47+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4948
{
5049
$args = $methodCall->getArgs();
51-
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
52-
$scope,
53-
$methodCall->getArgs(),
54-
$methodReflection->getVariants(),
55-
)->getReturnType();
5650

5751
if (\count($args) < 1) {
58-
return $defaultReturn;
52+
return null;
5953
}
6054

6155
if ($scope->getType($args[0]->value) instanceof MixedType) {
62-
return $defaultReturn;
56+
return null;
6357
}
6458

6559
// make sure we don't report wrong types in doctrine 2.x
6660
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
67-
return $defaultReturn;
61+
return null;
6862
}
6963

7064
$params = null;
@@ -73,15 +67,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7367
}
7468

7569
try {
76-
$resultType = $this->inferType($methodReflection, $args[0]->value, $params, $scope);
77-
if (null !== $resultType) {
78-
return $resultType;
79-
}
70+
return $this->inferType($methodReflection, $args[0]->value, $params, $scope);
8071
} catch (UnresolvableQueryException $exception) {
8172
// simulation not possible.. use default value
8273
}
8374

84-
return $defaultReturn;
75+
return null;
8576
}
8677

8778
private function inferType(MethodReflection $methodReflection, Expr $queryExpr, ?Expr $paramsExpr, Scope $scope): ?Type

src/Extensions/DoctrineConnectionPrepareDynamicReturnTypeExtension.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1514
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1615
use PHPStan\Type\MixedType;
1716
use PHPStan\Type\Type;
@@ -32,38 +31,30 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3231
return \in_array(strtolower($methodReflection->getName()), ['prepare'], true);
3332
}
3433

35-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
34+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3635
{
3736
$args = $methodCall->getArgs();
38-
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
39-
$scope,
40-
$methodCall->getArgs(),
41-
$methodReflection->getVariants(),
42-
)->getReturnType();
4337

4438
if (\count($args) < 1) {
45-
return $defaultReturn;
39+
return null;
4640
}
4741

4842
if ($scope->getType($args[0]->value) instanceof MixedType) {
49-
return $defaultReturn;
43+
return null;
5044
}
5145

5246
// make sure we don't report wrong types in doctrine 2.x
5347
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
54-
return $defaultReturn;
48+
return null;
5549
}
5650

5751
try {
58-
$resultType = $this->inferType($args[0]->value, $scope);
59-
if (null !== $resultType) {
60-
return $resultType;
61-
}
52+
return $this->inferType($args[0]->value, $scope);
6253
} catch (UnresolvableQueryException $exception) {
6354
// simulation not possible.. use default value
6455
}
6556

66-
return $defaultReturn;
57+
return null;
6758
}
6859

6960
private function inferType(Expr $queryExpr, Scope $scope): ?Type

src/Extensions/DoctrineConnectionQueryDynamicReturnTypeExtension.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1514
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1615
use PHPStan\Type\MixedType;
1716
use PHPStan\Type\Type;
@@ -32,38 +31,30 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3231
return \in_array(strtolower($methodReflection->getName()), ['query'], true);
3332
}
3433

35-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
34+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3635
{
3736
$args = $methodCall->getArgs();
38-
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
39-
$scope,
40-
$methodCall->getArgs(),
41-
$methodReflection->getVariants(),
42-
)->getReturnType();
4337

4438
if (\count($args) < 1) {
45-
return $defaultReturn;
39+
return null;
4640
}
4741

4842
if ($scope->getType($args[0]->value) instanceof MixedType) {
49-
return $defaultReturn;
43+
return null;
5044
}
5145

5246
// make sure we don't report wrong types in doctrine 2.x
5347
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
54-
return $defaultReturn;
48+
return null;
5549
}
5650

5751
try {
58-
$resultType = $this->inferType($args[0]->value, $scope);
59-
if (null !== $resultType) {
60-
return $resultType;
61-
}
52+
return $this->inferType($args[0]->value, $scope);
6253
} catch (UnresolvableQueryException $exception) {
6354
// simulation not possible.. use default value
6455
}
6556

66-
return $defaultReturn;
57+
return null;
6758
}
6859

6960
private function inferType(Expr $queryExpr, Scope $scope): ?Type

src/Extensions/DoctrineResultDynamicReturnTypeExtension.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Expr\MethodCall;
1111
use PHPStan\Analyser\Scope;
1212
use PHPStan\Reflection\MethodReflection;
13-
use PHPStan\Reflection\ParametersAcceptorSelector;
1413
use PHPStan\ShouldNotHappenException;
1514
use PHPStan\Type\Constant\ConstantArrayType;
1615
use PHPStan\Type\Constant\ConstantIntegerType;
@@ -47,25 +46,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4746
return \in_array(strtolower($methodReflection->getName()), self::METHODS, true);
4847
}
4948

50-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
49+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
5150
{
52-
$defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
53-
5451
// make sure we don't report wrong types in doctrine 2.x
5552
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
56-
return $defaultReturn;
53+
return null;
5754
}
5855

5956
try {
60-
$resultType = $this->inferType($methodReflection, $methodCall, $scope);
61-
if (null !== $resultType) {
62-
return $resultType;
63-
}
57+
return $this->inferType($methodReflection, $methodCall, $scope);
6458
} catch (UnresolvableQueryException $exception) {
6559
// simulation not possible.. use default value
6660
}
6761

68-
return $defaultReturn;
62+
return null;
6963
}
7064

7165
private function inferType(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type

src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\MethodReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1514
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1615
use PHPStan\Type\Type;
1716
use staabm\PHPStanDba\DoctrineReflection\DoctrineReflection;
@@ -32,34 +31,26 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3231
return \in_array(strtolower($methodReflection->getName()), ['executequery', 'execute'], true);
3332
}
3433

35-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
34+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3635
{
3736
$args = $methodCall->getArgs();
38-
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
39-
$scope,
40-
$methodCall->getArgs(),
41-
$methodReflection->getVariants(),
42-
)->getReturnType();
4337

4438
if (\count($args) < 1) {
45-
return $defaultReturn;
39+
return null;
4640
}
4741

4842
// make sure we don't report wrong types in doctrine 2.x
4943
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
50-
return $defaultReturn;
44+
return null;
5145
}
5246

5347
try {
54-
$resultType = $this->inferType($methodReflection, $methodCall, $args[0]->value, $scope);
55-
if (null !== $resultType) {
56-
return $resultType;
57-
}
48+
return $this->inferType($methodReflection, $methodCall, $args[0]->value, $scope);
5849
} catch (UnresolvableQueryException $exception) {
5950
// simulation not possible.. use default value
6051
}
6152

62-
return $defaultReturn;
53+
return null;
6354
}
6455

6556
private function inferType(MethodReflection $methodReflection, MethodCall $methodCall, Expr $paramsExpr, Scope $scope): ?Type

src/Extensions/PdoStatementColumnCountDynamicReturnTypeExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Expr\MethodCall;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\MethodReflection;
11-
use PHPStan\Reflection\ParametersAcceptorSelector;
1211
use PHPStan\Type\Constant\ConstantArrayType;
1312
use PHPStan\Type\Constant\ConstantIntegerType;
1413
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -28,10 +27,8 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2827
return \in_array($methodReflection->getName(), ['columnCount'], true);
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
{
33-
$defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
34-
3532
$statementType = $scope->getType($methodCall->var);
3633

3734
if ($statementType instanceof PdoStatementObjectType) {
@@ -41,6 +38,6 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4138
}
4239
}
4340

44-
return $defaultReturn;
41+
return null;
4542
}
4643
}

0 commit comments

Comments
 (0)