Skip to content

Commit d09d427

Browse files
staabmclxmstaab
andauthored
added configurable SyntaxErrorInPreparedStatementMethodRule (#78)
Co-authored-by: Markus Staab <[email protected]>
1 parent 135690e commit d09d427

10 files changed

+201
-182
lines changed

.phpstan-dba.cache

Lines changed: 4 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
'message' => 'Unknown column \'doesNotExist\' in \'group statement\'',
1010
'code' => 1054,
1111
)),
12-
'result' =>
13-
array (
14-
1 => NULL,
15-
),
1612
),
1713
'SELECT * FROM ada ORDER BY doesNotExist' =>
1814
array (
@@ -21,10 +17,6 @@
2117
'message' => 'Unknown column \'doesNotExist\' in \'order clause\'',
2218
'code' => 1054,
2319
)),
24-
'result' =>
25-
array (
26-
1 => NULL,
27-
),
2820
),
2921
'SELECT * FROM ada WHERE doesNotExist=1' =>
3022
array (
@@ -33,10 +25,6 @@
3325
'message' => 'Unknown column \'doesNotExist\' in \'where clause\'',
3426
'code' => 1054,
3527
)),
36-
'result' =>
37-
array (
38-
1 => NULL,
39-
),
4028
),
4129
'SELECT * FROM unknownTable' =>
4230
array (
@@ -45,10 +33,6 @@
4533
'message' => 'Table \'phpstan_dba.unknownTable\' doesn\'t exist',
4634
'code' => 1146,
4735
)),
48-
'result' =>
49-
array (
50-
1 => NULL,
51-
),
5236
),
5337
'SELECT akid FROM ak WHERE eadavk>1.0' =>
5438
array (
@@ -139,10 +123,6 @@
139123
'message' => 'Unknown column \'doesNotExist\' in \'field list\'',
140124
'code' => 1054,
141125
)),
142-
'result' =>
143-
array (
144-
1 => NULL,
145-
),
146126
),
147127
'SELECT eladaid FROM ak' =>
148128
array (
@@ -213,8 +193,6 @@
213193
'result' =>
214194
array (
215195
1 => NULL,
216-
2 => NULL,
217-
3 => NULL,
218196
),
219197
),
220198
'SELECT email, adaid FROM ada' =>
@@ -616,30 +594,6 @@
616594
)),
617595
),
618596
),
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-
),
643597
'SELECT email, adaid FROM ada WHERE adaid = :adaid' =>
644598
array (
645599
'error' =>
@@ -652,30 +606,6 @@
652606
3 => NULL,
653607
),
654608
),
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-
),
679609
'SELECT email, adaid FROM ada WHERE adaid = ?' =>
680610
array (
681611
'error' =>
@@ -979,6 +909,10 @@
979909
3 => NULL,
980910
),
981911
),
912+
'SELECT email, adaid FROM ada WHERE gesperrt = 1' =>
913+
array (
914+
'error' => NULL,
915+
),
982916
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada' =>
983917
array (
984918
'error' => NULL,
@@ -1076,90 +1010,6 @@
10761010
array (
10771011
),
10781012
)),
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-
)),
11631013
),
11641014
),
11651015
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada LIMIT 1' =>
@@ -1459,30 +1309,6 @@
14591309
)),
14601310
),
14611311
),
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-
),
14861312
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'foo\'' =>
14871313
array (
14881314
'error' => NULL,

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ The GitHubActions setup of `phpstan-dba` is [using this technique to replay the
101101

102102
### use `SyntaxErrorInQueryMethodRule` for your custom classes
103103

104+
Reuse the `SyntaxErrorInPreparedStatementMethodRule` within your PHPStan configuration to detect syntax errors in prepared queries, by registering a service:
105+
106+
```
107+
services:
108+
-
109+
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
110+
tags: [phpstan.rules.rule]
111+
arguments:
112+
classMethods:
113+
- 'My\Connection::preparedQuery'
114+
```
115+
116+
__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.__
117+
118+
### use `SyntaxErrorInQueryMethodRule` for your custom classes
119+
104120
Reuse the `SyntaxErrorInQueryMethodRule` within your PHPStan configuration to detect syntax errors in queries, by registering a service:
105121

106122
```

bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
}
2020

2121
$reflector = new MysqliQueryReflector($mysqli);
22-
// record only while phpstan is running - since this file might also be used as phpunit bootscript
23-
if (defined('__PHPSTAN_RUNNING__')) {
22+
// record only while phpunit is running - since this file might also be used as phpstan bootscript
23+
if (!defined('__PHPSTAN_RUNNING__')) {
2424
$reflector = new RecordingQueryReflector(
2525
ReflectionCache::create(
2626
$cacheFile

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
},
2424
"autoload-dev": {
2525
"psr-4": {
26-
"staabm\\PHPStanDba\\tests\\": "tests/"
26+
"staabm\\PHPStanDba\\Tests\\Fixture\\": "tests/Fixture/",
27+
"staabm\\PHPStanDba\\Tests\\": "tests/"
2728
}
2829
},
2930
"scripts": {

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ parameters:
2525

2626
excludePaths:
2727
analyseAndScan:
28-
- *fixtures/**
28+
- *Fixture/**

0 commit comments

Comments
 (0)