Skip to content

Commit 135690e

Browse files
staabmclxmstaab
andauthored
fix RecordingQueryReflector for prepared statements (#79)
Co-authored-by: Markus Staab <[email protected]>
1 parent e6d6d6f commit 135690e

File tree

4 files changed

+133
-16
lines changed

4 files changed

+133
-16
lines changed

.phpstan-dba.cache

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,90 @@
616616
)),
617617
),
618618
),
619+
'SELECT email, adaid FROM ada WHERE adaid = 1 and email = :email' =>
620+
array (
621+
'error' =>
622+
staabm\PHPStanDba\Error::__set_state(array(
623+
'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 \':email LIMIT 0\' at line 1',
624+
'code' => 1064,
625+
)),
626+
'result' =>
627+
array (
628+
3 => NULL,
629+
),
630+
),
631+
'SELECT email, adaid FROM ada WHERE adaid = 1 and email = ?' =>
632+
array (
633+
'error' =>
634+
staabm\PHPStanDba\Error::__set_state(array(
635+
'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',
636+
'code' => 1064,
637+
)),
638+
'result' =>
639+
array (
640+
3 => NULL,
641+
),
642+
),
643+
'SELECT email, adaid FROM ada WHERE adaid = :adaid' =>
644+
array (
645+
'error' =>
646+
staabm\PHPStanDba\Error::__set_state(array(
647+
'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 \':adaid LIMIT 0\' at line 1',
648+
'code' => 1064,
649+
)),
650+
'result' =>
651+
array (
652+
3 => NULL,
653+
),
654+
),
655+
'SELECT email, adaid FROM ada WHERE adaid = :adaid and email = \'[email protected]\'' =>
656+
array (
657+
'error' =>
658+
staabm\PHPStanDba\Error::__set_state(array(
659+
'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 \':adaid and email = \'[email protected]\' LIMIT 0\' at line 1',
660+
'code' => 1064,
661+
)),
662+
'result' =>
663+
array (
664+
3 => NULL,
665+
),
666+
),
667+
'SELECT email, adaid FROM ada WHERE adaid = :adaid and email = :email' =>
668+
array (
669+
'error' =>
670+
staabm\PHPStanDba\Error::__set_state(array(
671+
'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 \':adaid and email = :email LIMIT 0\' at line 1',
672+
'code' => 1064,
673+
)),
674+
'result' =>
675+
array (
676+
3 => NULL,
677+
),
678+
),
679+
'SELECT email, adaid FROM ada WHERE adaid = ?' =>
680+
array (
681+
'error' =>
682+
staabm\PHPStanDba\Error::__set_state(array(
683+
'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',
684+
'code' => 1064,
685+
)),
686+
'result' =>
687+
array (
688+
3 => NULL,
689+
),
690+
),
691+
'SELECT email, adaid FROM ada WHERE adaid = ? and email = ?' =>
692+
array (
693+
'error' =>
694+
staabm\PHPStanDba\Error::__set_state(array(
695+
'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 \'? and email = ? LIMIT 0\' at line 1',
696+
'code' => 1064,
697+
)),
698+
'result' =>
699+
array (
700+
3 => NULL,
701+
),
702+
),
619703
'SELECT email, adaid FROM ada WHERE adaid=1' =>
620704
array (
621705
'error' => NULL,
@@ -685,6 +769,18 @@
685769
)),
686770
),
687771
),
772+
'SELECT email, adaid FROM ada WHERE email <=> :email' =>
773+
array (
774+
'error' =>
775+
staabm\PHPStanDba\Error::__set_state(array(
776+
'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 \':email LIMIT 0\' at line 1',
777+
'code' => 1064,
778+
)),
779+
'result' =>
780+
array (
781+
3 => NULL,
782+
),
783+
),
688784
'SELECT email, adaid FROM ada WHERE email <=> NULL' =>
689785
array (
690786
'error' => NULL,
@@ -871,6 +967,18 @@
871967
)),
872968
),
873969
),
970+
'SELECT email, adaid FROM ada WHERE email = :email' =>
971+
array (
972+
'error' =>
973+
staabm\PHPStanDba\Error::__set_state(array(
974+
'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 \':email LIMIT 0\' at line 1',
975+
'code' => 1064,
976+
)),
977+
'result' =>
978+
array (
979+
3 => NULL,
980+
),
981+
),
874982
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada' =>
875983
array (
876984
'error' => NULL,
@@ -1351,6 +1459,30 @@
13511459
)),
13521460
),
13531461
),
1462+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=:adaid' =>
1463+
array (
1464+
'error' =>
1465+
staabm\PHPStanDba\Error::__set_state(array(
1466+
'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 \':adaid LIMIT 0\' at line 1',
1467+
'code' => 1064,
1468+
)),
1469+
'result' =>
1470+
array (
1471+
1 => NULL,
1472+
),
1473+
),
1474+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?' =>
1475+
array (
1476+
'error' =>
1477+
staabm\PHPStanDba\Error::__set_state(array(
1478+
'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',
1479+
'code' => 1064,
1480+
)),
1481+
'result' =>
1482+
array (
1483+
1 => NULL,
1484+
),
1485+
),
13541486
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'foo\'' =>
13551487
array (
13561488
'error' => NULL,

src/QueryReflection/QueryReflection.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
final class QueryReflection
2626
{
27-
private const NAMED_PLACEHOLDER_REGEX = '/(:[a-z])+/i';
28-
2927
/**
3028
* @var QueryReflector|null
3129
*/
@@ -72,11 +70,6 @@ private function builtSimulatedQuery(string $queryString): ?string
7270
return null;
7371
}
7472

75-
// skip queries which contain placeholders for now
76-
if (str_contains($queryString, '?') || preg_match(self::NAMED_PLACEHOLDER_REGEX, $queryString) > 0) {
77-
return null;
78-
}
79-
8073
return $queryString;
8174
}
8275

tests/SyntaxErrorInQueryMethodRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testSyntaxErrorInQueryRule(): void
5151
],
5252
[
5353
"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,
54+
67,
5555
],
5656
]);
5757
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ public function validQuery(PDO $pdo)
6262
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada', PDO::FETCH_ASSOC);
6363
}
6464

65-
public function queryWithPlaceholder(PDO $pdo)
66-
{
67-
// atm we just make sure, this is not detected as a syntax error.
68-
// a proper return type check should be added in the future.
69-
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?', PDO::FETCH_ASSOC);
70-
$pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=:adaid', PDO::FETCH_ASSOC);
71-
}
72-
7365
public function syntaxErrorPdoPrepare(PDO $pdo)
7466
{
7567
$pdo->prepare('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada');

0 commit comments

Comments
 (0)