Skip to content

Commit eb80741

Browse files
staabmclxmstaab
andauthored
Detect syntax errors in Doctrine\DBAL\Connection::execute*() methods (#116)
Co-authored-by: Markus Staab <[email protected]>
1 parent 2eaf50b commit eb80741

File tree

7 files changed

+167
-54
lines changed

7 files changed

+167
-54
lines changed

.editorconfig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ charset = utf-8
77
trim_trailing_whitespace = true
88
indent_style = space
99

10-
[*.{php,phpt}]
11-
indent_size = 4
12-
13-
[*.neon]
10+
[*.{neon, md, php,phpt}]
1411
indent_size = 4
1512

1613
[*.{yaml,yml}]

.phpstan-dba.cache

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
'result' =>
214214
array (
215215
1 => NULL,
216+
2 => NULL,
216217
3 => NULL,
217218
),
218219
),
@@ -1075,6 +1076,90 @@
10751076
array (
10761077
),
10771078
)),
1079+
2 =>
1080+
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
1081+
'keyType' =>
1082+
PHPStan\Type\UnionType::__set_state(array(
1083+
'types' =>
1084+
array (
1085+
0 =>
1086+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1087+
'value' => 0,
1088+
)),
1089+
1 =>
1090+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1091+
'value' => 1,
1092+
)),
1093+
2 =>
1094+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1095+
'value' => 2,
1096+
)),
1097+
3 =>
1098+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1099+
'value' => 3,
1100+
)),
1101+
),
1102+
)),
1103+
'itemType' =>
1104+
PHPStan\Type\UnionType::__set_state(array(
1105+
'types' =>
1106+
array (
1107+
0 =>
1108+
PHPStan\Type\IntegerRangeType::__set_state(array(
1109+
'min' => -128,
1110+
'max' => 4294967295,
1111+
)),
1112+
1 =>
1113+
PHPStan\Type\StringType::__set_state(array(
1114+
)),
1115+
),
1116+
)),
1117+
'allArrays' => NULL,
1118+
'keyTypes' =>
1119+
array (
1120+
0 =>
1121+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1122+
'value' => 0,
1123+
)),
1124+
1 =>
1125+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1126+
'value' => 1,
1127+
)),
1128+
2 =>
1129+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1130+
'value' => 2,
1131+
)),
1132+
3 =>
1133+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
1134+
'value' => 3,
1135+
)),
1136+
),
1137+
'valueTypes' =>
1138+
array (
1139+
0 =>
1140+
PHPStan\Type\StringType::__set_state(array(
1141+
)),
1142+
1 =>
1143+
PHPStan\Type\IntegerRangeType::__set_state(array(
1144+
'min' => 0,
1145+
'max' => 4294967295,
1146+
)),
1147+
2 =>
1148+
PHPStan\Type\IntegerRangeType::__set_state(array(
1149+
'min' => -128,
1150+
'max' => 127,
1151+
)),
1152+
3 =>
1153+
PHPStan\Type\IntegerRangeType::__set_state(array(
1154+
'min' => -128,
1155+
'max' => 127,
1156+
)),
1157+
),
1158+
'nextAutoIndex' => 4,
1159+
'optionalKeys' =>
1160+
array (
1161+
),
1162+
)),
10781163
),
10791164
),
10801165
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada LIMIT 1' =>

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ Reuse the `SyntaxErrorInPreparedStatementMethodRule` within your PHPStan configu
137137

138138
```
139139
services:
140-
-
141-
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
142-
tags: [phpstan.rules.rule]
143-
arguments:
144-
classMethods:
145-
- 'My\Connection::preparedQuery'
146-
- 'My\PreparedStatement::__construct'
140+
-
141+
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
142+
tags: [phpstan.rules.rule]
143+
arguments:
144+
classMethods:
145+
- 'My\Connection::preparedQuery'
146+
- 'My\PreparedStatement::__construct'
147147
```
148148

149149
__the callable format is `class::method`. phpstan-dba assumes the method takes a query-string as a 1st and the parameter-values as a 2nd argument.__
@@ -154,13 +154,13 @@ Reuse the `SyntaxErrorInQueryMethodRule` within your PHPStan configuration to de
154154

155155
```
156156
services:
157-
-
158-
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
159-
tags: [phpstan.rules.rule]
160-
arguments:
161-
classMethods:
162-
- 'myClass::query#0'
163-
- 'anotherClass::takesAQuery#2'
157+
-
158+
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
159+
tags: [phpstan.rules.rule]
160+
arguments:
161+
classMethods:
162+
- 'myClass::query#0'
163+
- 'anotherClass::takesAQuery#2'
164164
```
165165

166166
__the callable format is `class::method#parameterIndex`, while the parameter-index defines the position of the query-string argument.__
@@ -171,12 +171,12 @@ Reuse the `SyntaxErrorInQueryFunctionRule` within your PHPStan configuration to
171171

172172
```
173173
services:
174-
-
175-
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryFunctionRule
176-
tags: [phpstan.rules.rule]
177-
arguments:
178-
functionNames:
179-
- 'Deployer\runMysqlQuery#0'
174+
-
175+
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryFunctionRule
176+
tags: [phpstan.rules.rule]
177+
arguments:
178+
functionNames:
179+
- 'Deployer\runMysqlQuery#0'
180180
```
181181

182182
__the callable format is `funtionName#parameterIndex`, while the parameter-index defines the position of the query-string argument.__

config/dba.neon

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
includes:
2-
- stubFiles.neon
3-
- extensions.neon
2+
- stubFiles.neon
3+
- extensions.neon
44

55
services:
6-
-
7-
class: staabm\PHPStanDba\Rules\PdoStatementExecuteMethodRule
8-
tags: [phpstan.rules.rule]
6+
-
7+
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
8+
tags: [phpstan.rules.rule]
9+
arguments:
10+
classMethods:
11+
- 'Doctrine\DBAL\Connection::executeQuery'
12+
- 'Doctrine\DBAL\Connection::executeCacheQuery'
13+
- 'Doctrine\DBAL\Connection::executeStatement'
914

10-
-
11-
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
12-
tags: [phpstan.rules.rule]
13-
arguments:
14-
classMethods:
15-
- 'PDO::query#0'
16-
- 'PDO::prepare#0'
17-
- 'mysqli::query#0'
18-
- 'Doctrine\DBAL\Connection::query#0'
15+
-
16+
class: staabm\PHPStanDba\Rules\PdoStatementExecuteMethodRule
17+
tags: [phpstan.rules.rule]
1918

20-
-
21-
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryFunctionRule
22-
tags: [phpstan.rules.rule]
23-
arguments:
24-
functionNames:
25-
- 'Deployer\runMysqlQuery#0'
26-
- 'mysqli_query#1'
19+
-
20+
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
21+
tags: [phpstan.rules.rule]
22+
arguments:
23+
classMethods:
24+
- 'PDO::query#0'
25+
- 'PDO::prepare#0'
26+
- 'mysqli::query#0'
27+
- 'Doctrine\DBAL\Connection::query#0'
28+
29+
-
30+
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryFunctionRule
31+
tags: [phpstan.rules.rule]
32+
arguments:
33+
functionNames:
34+
- 'Deployer\runMysqlQuery#0'
35+
- 'mysqli_query#1'

tests/SyntaxErrorInPreparedStatementMethodRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function testSyntaxErrorInQueryRule(): void
3737
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'FROM ada LIMIT 0' at line 3 (1064).",
3838
29,
3939
],
40+
[
41+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
42+
105,
43+
],
44+
[
45+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
46+
106,
47+
],
48+
[
49+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
50+
107,
51+
],
4052
]);
4153
}
4254
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
includes:
2-
- ../../config/stubFiles.neon
3-
- ../../config/extensions.neon
2+
- ../../config/stubFiles.neon
3+
- ../../config/extensions.neon
44

55
services:
6-
-
7-
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
8-
tags: [phpstan.rules.rule]
9-
arguments:
10-
classMethods:
11-
- 'staabm\PHPStanDba\Tests\Fixture\Connection::preparedQuery'
12-
- 'staabm\PHPStanDba\Tests\Fixture\PreparedStatement::__construct'
6+
-
7+
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
8+
tags: [phpstan.rules.rule]
9+
arguments:
10+
classMethods:
11+
- 'staabm\PHPStanDba\Tests\Fixture\Connection::preparedQuery'
12+
- 'staabm\PHPStanDba\Tests\Fixture\PreparedStatement::__construct'
13+
- 'Doctrine\DBAL\Connection::executeQuery'
14+
- 'Doctrine\DBAL\Connection::executeCacheQuery'
15+
- 'Doctrine\DBAL\Connection::executeStatement'

tests/data/syntax-error-in-prepared-statement.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,11 @@ public function camelCase(Connection $connection)
9999
{
100100
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = :myGesperrt', ['myGesperrt' => 1]);
101101
}
102+
103+
public function syntaxErrorInDoctrineDbal(\Doctrine\DBAL\Connection $conn, $types, \Doctrine\DBAL\Cache\QueryCacheProfile $qcp)
104+
{
105+
$conn->executeQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
106+
$conn->executeCacheQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', [], $types, $qcp);
107+
$conn->executeStatement('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
108+
}
102109
}

0 commit comments

Comments
 (0)