Skip to content

Commit 09be3ee

Browse files
staabmclxmstaab
andauthored
detect syntax errors in PDO->prepare() (#35)
Co-authored-by: Markus Staab <[email protected]>
1 parent 52f5f8e commit 09be3ee

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This extension provides following features:
55
* `PDO->query` knows the array shape of the returned results and therefore can return a generic `PDOStatement`
66
* `mysqli->query` knows the array shape of the returned results and therefore can return a generic `mysqli_result`
77
* `SyntaxErrorInQueryMethodRule` can inspect sql queries and detect syntax errors - `SyntaxErrorInQueryFunctionRule` can do the same for functions
8+
* builtin is query syntax error detection for `mysqli_query`, `mysqli->query`, `PDO->query` and `PDO->prepare`
89
* `mysqli_real_escape_string` and `mysqli->real_escape_string` dynamic return type extensions
910
* `pdo->quote` dynamic return type extension
1011

config/dba.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
arguments:
1010
classMethods:
1111
- 'PDO::query#0'
12+
- 'PDO::prepare#0'
1213
- 'mysqli::query#0'
1314

1415
-

tests/SyntaxErrorInQueryMethodRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public function testSyntaxErrorInQueryRule(): void
4949
"Query error: Table 'phpstan_dba.unknownTable' doesn't exist (1146).",
5050
41,
5151
],
52+
[
53+
"Query error: 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 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
54+
75,
55+
],
5256
]);
5357
}
5458
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Foo
88
{
9-
public function syntaxErrorPdo(PDO $pdo)
9+
public function syntaxErrorPdoQuery(PDO $pdo)
1010
{
1111
$pdo->query('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', PDO::FETCH_ASSOC);
1212
}
@@ -69,4 +69,9 @@ public function queryWithPlaceholder(PDO $pdo)
6969
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?', PDO::FETCH_ASSOC);
7070
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=:adaid', PDO::FETCH_ASSOC);
7171
}
72+
73+
public function syntaxErrorPdoPrepare(PDO $pdo)
74+
{
75+
$pdo->prepare('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada');
76+
}
7277
}

0 commit comments

Comments
 (0)