Skip to content

Commit 88cdf9d

Browse files
staabmclxmstaab
andauthored
Don't hard error on incomplete query containing union-type (#27)
Co-authored-by: Markus Staab <[email protected]>
1 parent 9f896b7 commit 88cdf9d

File tree

3 files changed

+13
-64
lines changed

3 files changed

+13
-64
lines changed

.phpstan-dba.cache

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -132,45 +132,6 @@
132132
)),
133133
),
134134
),
135-
'SELECT cmsdomainid FROM cmsdomain WHERE url ="" and standard=1' =>
136-
array (
137-
'error' => NULL,
138-
'result' =>
139-
array (
140-
2 =>
141-
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
142-
'keyType' =>
143-
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
144-
'value' => 0,
145-
)),
146-
'itemType' =>
147-
PHPStan\Type\IntegerRangeType::__set_state(array(
148-
'min' => -2147483648,
149-
'max' => 2147483647,
150-
)),
151-
'keyTypes' =>
152-
array (
153-
0 =>
154-
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
155-
'value' => 0,
156-
)),
157-
),
158-
'valueTypes' =>
159-
array (
160-
0 =>
161-
PHPStan\Type\IntegerRangeType::__set_state(array(
162-
'min' => -2147483648,
163-
'max' => 2147483647,
164-
)),
165-
),
166-
'nextAutoIndex' => 1,
167-
'optionalKeys' =>
168-
array (
169-
),
170-
'allArrays' => NULL,
171-
)),
172-
),
173-
),
174135
'SELECT doesNotExist, adaid, gesperrt, freigabe1u1 FROM ada' =>
175136
array (
176137
'error' =>
@@ -468,18 +429,6 @@
468429
)),
469430
),
470431
),
471-
'SELECT email, adaid, gesperrt, freigabe1u1 FROM LIMIT 1' =>
472-
array (
473-
'error' =>
474-
staabm\PHPStanDba\Error::__set_state(array(
475-
'message' => 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'LIMIT 0\' at line 1',
476-
'code' => 1064,
477-
)),
478-
'result' =>
479-
array (
480-
1 => NULL,
481-
),
482-
),
483432
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada' =>
484433
array (
485434
'error' => NULL,
@@ -861,18 +810,6 @@
861810
)),
862811
),
863812
),
864-
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE ' =>
865-
array (
866-
'error' =>
867-
staabm\PHPStanDba\Error::__set_state(array(
868-
'message' => 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'LIMIT 0\' at line 1',
869-
'code' => 1064,
870-
)),
871-
'result' =>
872-
array (
873-
1 => NULL,
874-
),
875-
),
876813
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=1' =>
877814
array (
878815
'error' => NULL,

src/QueryReflection/QueryReflection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Type\MixedType;
1616
use PHPStan\Type\StringType;
1717
use PHPStan\Type\Type;
18+
use PHPStan\Type\UnionType;
1819
use staabm\PHPStanDba\DbaException;
1920
use staabm\PHPStanDba\Error;
2021

@@ -117,7 +118,7 @@ private function resolveQueryString(Expr $expr, Scope $scope): ?string
117118
return '1.0';
118119
}
119120

120-
if ($type instanceof MixedType || $type instanceof StringType || $type instanceof IntersectionType) {
121+
if ($type instanceof MixedType || $type instanceof StringType || $type instanceof IntersectionType || $type instanceof UnionType) {
121122
return null;
122123
}
123124

tests/data/syntax-error-in-query.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public function incompleteQuery(PDO $pdo, string $tableName)
4646
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM '.$tableName.' LIMIT 1', PDO::FETCH_ASSOC);
4747
}
4848

49+
public function incompleteQueryUnion(PDO $pdo)
50+
{
51+
$add = '';
52+
if (rand(0, 1)) {
53+
$add .= 'my_other_table';
54+
}
55+
56+
// XXX we might get smarter in query parsing and resolve this query at analysis time
57+
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada .'.$add.' LIMIT 1', PDO::FETCH_ASSOC);
58+
}
59+
4960
public function validQuery(PDO $pdo)
5061
{
5162
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada', PDO::FETCH_ASSOC);

0 commit comments

Comments
 (0)