Skip to content

Commit 08a068d

Browse files
authored
Cleanup instanceof *Type checks (#692)
1 parent f559241 commit 08a068d

13 files changed

+79
-168
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ parameters:
55
count: 2
66
path: src/Analyzer/QueryPlanAnalyzerMysql.php
77

8-
-
9-
message: "#^Instanceof between mysqli_result\\<array\\<string, int\\<\\-128, 127\\>\\|string\\|null\\>\\> and mysqli_result will always evaluate to true\\.$#"
10-
count: 1
11-
path: src/DbSchema/SchemaHasherMysql.php
12-
13-
-
14-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
15-
count: 1
16-
path: src/DoctrineReflection/DoctrineReflection.php
17-
18-
-
19-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
20-
count: 1
21-
path: src/DoctrineReflection/DoctrineReflection.php
22-
238
-
249
message: """
2510
#^Call to deprecated method resolveQueryString\\(\\) of class staabm\\\\PHPStanDba\\\\QueryReflection\\\\QueryReflection\\:
@@ -28,51 +13,11 @@ parameters:
2813
count: 1
2914
path: src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php
3015

31-
-
32-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
33-
count: 1
34-
path: src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php
35-
3616
-
3717
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
3818
count: 2
3919
path: src/Extensions/DibiConnectionFetchDynamicReturnTypeExtension.php
4020

41-
-
42-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
43-
count: 1
44-
path: src/Extensions/DoctrineResultDynamicReturnTypeExtension.php
45-
46-
-
47-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
48-
count: 1
49-
path: src/Extensions/PdoStatementColumnCountDynamicReturnTypeExtension.php
50-
51-
-
52-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
53-
count: 1
54-
path: src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php
55-
56-
-
57-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
58-
count: 1
59-
path: src/Extensions/PdoStatementFetchObjectDynamicReturnTypeExtension.php
60-
61-
-
62-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
63-
count: 1
64-
path: src/PdoReflection/PdoStatementObjectType.php
65-
66-
-
67-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
68-
count: 1
69-
path: src/PdoReflection/PdoStatementObjectType.php
70-
71-
-
72-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
73-
count: 1
74-
path: src/PdoReflection/PdoStatementReflection.php
75-
7621
-
7722
message: "#^Strict comparison using \\=\\=\\= between 'dibi' and 'dibi' will always evaluate to true\\.$#"
7823
count: 1
@@ -91,56 +36,26 @@ parameters:
9136
count: 1
9237
path: src/QueryReflection/QueryReflection.php
9338

94-
-
95-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
96-
count: 3
97-
path: src/QueryReflection/QueryReflection.php
98-
99-
-
100-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
101-
count: 1
102-
path: src/QueryReflection/QueryReflection.php
103-
10439
-
10540
message: "#^Strict comparison using \\=\\=\\= between null and null will always evaluate to true\\.$#"
10641
count: 1
10742
path: src/QueryReflection/QueryReflection.php
10843

109-
-
110-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Accessory\\\\AccessoryType is error\\-prone and deprecated\\. Use methods on PHPStan\\\\Type\\\\Type instead\\.$#"
111-
count: 1
112-
path: src/QueryReflection/QuerySimulation.php
113-
114-
-
115-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ArrayType is error\\-prone and deprecated\\. Use Type\\:\\:isArray\\(\\) or Type\\:\\:getArrays\\(\\) instead\\.$#"
116-
count: 1
117-
path: src/QueryReflection/QuerySimulation.php
118-
11944
-
12045
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
12146
count: 1
12247
path: src/QueryReflection/QuerySimulation.php
12348

12449
-
12550
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
126-
count: 2
51+
count: 1
12752
path: src/QueryReflection/QuerySimulation.php
12853

12954
-
13055
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
13156
count: 2
13257
path: src/QueryReflection/QuerySimulation.php
13358

134-
-
135-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
136-
count: 1
137-
path: src/Rules/DoctrineKeyValueStyleRule.php
138-
139-
-
140-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
141-
count: 2
142-
path: src/Rules/DoctrineKeyValueStyleRule.php
143-
14459
-
14560
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
14661
count: 1
@@ -156,34 +71,9 @@ parameters:
15671

15772
-
15873
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
159-
count: 2
160-
path: src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php
161-
162-
-
163-
message: "#^Doing instanceof PHPStan\\\\Type\\\\StringType is error\\-prone and deprecated\\. Use Type\\:\\:isString\\(\\) instead\\.$#"
16474
count: 1
16575
path: src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php
16676

167-
-
168-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
169-
count: 1
170-
path: src/SchemaReflection/SchemaReflection.php
171-
172-
-
173-
message: "#^Call to an undefined method SqlFtw\\\\Sql\\\\Dml\\\\TableReference\\\\Join\\:\\:getCondition\\(\\)\\.$#"
174-
count: 2
175-
path: src/SqlAst/ParserInference.php
176-
177-
-
178-
message: "#^Call to an undefined method SqlFtw\\\\Sql\\\\Dml\\\\TableReference\\\\TableReferenceNode\\:\\:getTable\\(\\)\\.$#"
179-
count: 1
180-
path: src/SqlAst/ParserInference.php
181-
182-
-
183-
message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#"
184-
count: 2
185-
path: src/TypeMapping/MysqliTypeMapper.php
186-
18777
-
18878
message: "#^Only booleans are allowed in an if condition, int given\\.$#"
18979
count: 1

src/DoctrineReflection/DoctrineReflection.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Type\ArrayType;
9-
use PHPStan\Type\Constant\ConstantArrayType;
109
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1110
use PHPStan\Type\Constant\ConstantBooleanType;
12-
use PHPStan\Type\Constant\ConstantIntegerType;
13-
use PHPStan\Type\Constant\ConstantStringType;
1411
use PHPStan\Type\Generic\GenericObjectType;
1512
use PHPStan\Type\IntegerRangeType;
1613
use PHPStan\Type\IntegerType;
@@ -44,7 +41,6 @@ public function reduceResultType(MethodReflection $methodReflection, Type $resul
4441
return $this->reduceResultType($methodReflection, $resultType->getRowType());
4542
}
4643

47-
$resultRowType = $resultType;
4844
$usedMethod = strtolower($methodReflection->getName());
4945

5046
switch ($usedMethod) {
@@ -73,7 +69,9 @@ public function reduceResultType(MethodReflection $methodReflection, Type $resul
7369
$fetchType = QueryReflector::FETCH_TYPE_BOTH;
7470
}
7571

76-
if (QueryReflector::FETCH_TYPE_BOTH !== $fetchType && $resultRowType instanceof ConstantArrayType) {
72+
$resultRowType = $resultType->getConstantArrays();
73+
if (QueryReflector::FETCH_TYPE_BOTH !== $fetchType && count($resultRowType) === 1) {
74+
$resultRowType = $resultRowType[0];
7775
$builder = ConstantArrayTypeBuilder::createEmpty();
7876

7977
$keyTypes = $resultRowType->getKeyTypes();
@@ -104,9 +102,9 @@ public function reduceResultType(MethodReflection $methodReflection, Type $resul
104102
return new ArrayType(IntegerRangeType::fromInterval(0, null), $valueTypes[$i]);
105103
}
106104

107-
if (QueryReflector::FETCH_TYPE_NUMERIC === $fetchType && $keyType instanceof ConstantIntegerType) {
105+
if (QueryReflector::FETCH_TYPE_NUMERIC === $fetchType && $keyType->isInteger()->yes()) {
108106
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
109-
} elseif (QueryReflector::FETCH_TYPE_ASSOC === $fetchType && $keyType instanceof ConstantStringType) {
107+
} elseif (QueryReflector::FETCH_TYPE_ASSOC === $fetchType && $keyType->isString()->yes()) {
110108
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
111109
}
112110
}

src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\FunctionReflection;
1010
use PHPStan\Type\ArrayType;
11-
use PHPStan\Type\Constant\ConstantArrayType;
1211
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1312
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1413
use PHPStan\Type\IntegerType;
@@ -44,8 +43,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4443
return null;
4544
}
4645

47-
$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_NUMERIC);
48-
if ($resultType instanceof ConstantArrayType) {
46+
$resultTypes = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_NUMERIC);
47+
if (null === $resultTypes) {
48+
return null;
49+
}
50+
51+
$resultTypes = $resultTypes->getConstantArrays();
52+
if (count($resultTypes) === 1) {
53+
$resultType = $resultTypes[0];
54+
4955
$builder = ConstantArrayTypeBuilder::createEmpty();
5056
foreach ($resultType->getKeyTypes() as $keyType) {
5157
$builder->setOffsetValueType($keyType, new StringType());

src/Extensions/DoctrineResultDynamicReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Analyser\Scope;
1212
use PHPStan\Reflection\MethodReflection;
1313
use PHPStan\ShouldNotHappenException;
14-
use PHPStan\Type\Constant\ConstantArrayType;
1514
use PHPStan\Type\Constant\ConstantIntegerType;
1615
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1716
use PHPStan\Type\Type;
@@ -67,9 +66,10 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
6766
$resultType = $scope->getType($methodCall->var);
6867
if ('columncount' === strtolower($methodReflection->getName())) {
6968
if ($resultType instanceof DoctrineResultObjectType) {
70-
$resultRowType = $resultType->getRowType();
69+
$arrays = $resultType->getRowType()->getConstantArrays();
70+
if (count($arrays) === 1) {
71+
$resultRowType = $arrays[0];
7172

72-
if ($resultRowType instanceof ConstantArrayType) {
7373
$columnCount = \count($resultRowType->getKeyTypes()) / 2;
7474
if (! \is_int($columnCount)) {
7575
throw new ShouldNotHappenException();

src/Extensions/PdoStatementColumnCountDynamicReturnTypeExtension.php

Lines changed: 3 additions & 3 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\Type\Constant\ConstantArrayType;
1211
use PHPStan\Type\Constant\ConstantIntegerType;
1312
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1413
use PHPStan\Type\Type;
@@ -32,8 +31,9 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3231
$statementType = $scope->getType($methodCall->var);
3332

3433
if ($statementType instanceof PdoStatementObjectType) {
35-
$rowType = $statementType->newWithFetchType(QueryReflector::FETCH_TYPE_NUMERIC)->getRowType();
36-
if ($rowType instanceof ConstantArrayType) {
34+
$arrays = $statementType->newWithFetchType(QueryReflector::FETCH_TYPE_NUMERIC)->getRowType()->getConstantArrays();
35+
if (count($arrays) === 1) {
36+
$rowType = $arrays[0];
3737
return new ConstantIntegerType(\count($rowType->getKeyTypes()));
3838
}
3939
}

src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\ArrayType;
1515
use PHPStan\Type\Constant\ConstantBooleanType;
1616
use PHPStan\Type\Constant\ConstantIntegerType;
17-
use PHPStan\Type\Constant\ConstantStringType;
1817
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1918
use PHPStan\Type\IntegerType;
2019
use PHPStan\Type\MixedType;
@@ -113,8 +112,9 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
113112

114113
if (\count($args) > 1) {
115114
$classStringType = $scope->getType($args[1]->value);
116-
if ($classStringType instanceof ConstantStringType) {
117-
$className = $classStringType->getValue();
115+
$strings = $classStringType->getConstantStrings();
116+
if (count($strings) === 1) {
117+
$className = $strings[0]->getValue();
118118
} else {
119119
return null;
120120
}

src/Extensions/PdoStatementFetchObjectDynamicReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Reflection\MethodReflection;
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Type\Constant\ConstantBooleanType;
13-
use PHPStan\Type\Constant\ConstantStringType;
1413
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1514
use PHPStan\Type\ObjectType;
1615
use PHPStan\Type\Type;
@@ -58,8 +57,9 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
5857

5958
if (\count($args) >= 1) {
6059
$classStringType = $scope->getType($args[0]->value);
61-
if ($classStringType instanceof ConstantStringType) {
62-
$className = $classStringType->getValue();
60+
$strings = $classStringType->getConstantStrings();
61+
if (count($strings) === 1) {
62+
$className = $strings[0]->getValue();
6363
} else {
6464
return null;
6565
}

src/PdoReflection/PdoStatementObjectType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
use PDOStatement;
88
use PHPStan\Type\ArrayType;
99
use PHPStan\Type\BenevolentUnionType;
10-
use PHPStan\Type\Constant\ConstantArrayType;
1110
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1211
use PHPStan\Type\Constant\ConstantIntegerType;
13-
use PHPStan\Type\Constant\ConstantStringType;
1412
use PHPStan\Type\FloatType;
1513
use PHPStan\Type\Generic\GenericObjectType;
1614
use PHPStan\Type\IntegerRangeType;
@@ -62,9 +60,11 @@ public function newWithFetchType(int $fetchType): self
6260
*/
6361
private function reduceBothType(Type $bothType, int $fetchType): Type
6462
{
65-
if (! $bothType instanceof ConstantArrayType) {
63+
$arrays = $bothType->getConstantArrays();
64+
if (count($arrays) !== 1) {
6665
return $bothType;
6766
}
67+
$bothType = $arrays[0];
6868

6969
if (\count($bothType->getValueTypes()) <= 0) {
7070
return $bothType;
@@ -80,9 +80,9 @@ private function reduceBothType(Type $bothType, int $fetchType): Type
8080
$valueTypes = $bothType->getValueTypes();
8181

8282
foreach ($keyTypes as $i => $keyType) {
83-
if (QueryReflector::FETCH_TYPE_NUMERIC === $fetchType && $keyType instanceof ConstantIntegerType) {
83+
if (QueryReflector::FETCH_TYPE_NUMERIC === $fetchType && $keyType->isInteger()->yes()) {
8484
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
85-
} elseif (QueryReflector::FETCH_TYPE_ASSOC === $fetchType && $keyType instanceof ConstantStringType) {
85+
} elseif (QueryReflector::FETCH_TYPE_ASSOC === $fetchType && $keyType->isString()->yes()) {
8686
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
8787
}
8888
}

src/PdoReflection/PdoStatementReflection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PDO;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\MethodCall;
10-
use PHPStan\Type\Constant\ConstantArrayType;
1110
use PHPStan\Type\Constant\ConstantIntegerType;
1211
use PHPStan\Type\ObjectType;
1312
use PHPStan\Type\Type;
@@ -131,8 +130,14 @@ public function getRowType(Type $statementType, int $fetchType): ?Type
131130
public function getColumnRowType(Type $statementType, int $columnIndex): ?Type
132131
{
133132
$statementType = $this->getRowType($statementType, QueryReflector::FETCH_TYPE_NUMERIC);
133+
if ($statementType === null) {
134+
return null;
135+
}
136+
137+
$arrays = $statementType->getConstantArrays();
138+
if (count($arrays) === 1) {
139+
$statementType = $arrays[0];
134140

135-
if ($statementType instanceof ConstantArrayType) {
136141
$valueTypes = $statementType->getValueTypes();
137142
if (\array_key_exists($columnIndex, $valueTypes)) {
138143
return $valueTypes[$columnIndex];

0 commit comments

Comments
 (0)