Skip to content

Commit 8aba84f

Browse files
clxmstaabstaabm
authored andcommitted
support conditional errors in prepared statements
1 parent adf089d commit 8aba84f

File tree

5 files changed

+349
-10
lines changed

5 files changed

+349
-10
lines changed

.phpstan-dba.cache

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@
10721072
'result' =>
10731073
array (
10741074
1 => NULL,
1075+
2 => NULL,
10751076
3 => NULL,
10761077
),
10771078
),
@@ -2507,6 +2508,148 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
25072508
'error' => NULL,
25082509
'result' =>
25092510
array (
2511+
3 =>
2512+
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
2513+
'keyType' =>
2514+
PHPStan\Type\UnionType::__set_state(array(
2515+
'types' =>
2516+
array (
2517+
0 =>
2518+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2519+
'value' => 0,
2520+
)),
2521+
1 =>
2522+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2523+
'value' => 1,
2524+
)),
2525+
2 =>
2526+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2527+
'value' => 2,
2528+
)),
2529+
3 =>
2530+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2531+
'value' => 3,
2532+
)),
2533+
4 =>
2534+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2535+
'value' => 'adaid',
2536+
'isClassString' => false,
2537+
)),
2538+
5 =>
2539+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2540+
'value' => 'email',
2541+
'isClassString' => false,
2542+
)),
2543+
6 =>
2544+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2545+
'value' => 'freigabe1u1',
2546+
'isClassString' => false,
2547+
)),
2548+
7 =>
2549+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2550+
'value' => 'gesperrt',
2551+
'isClassString' => false,
2552+
)),
2553+
),
2554+
)),
2555+
'itemType' =>
2556+
PHPStan\Type\UnionType::__set_state(array(
2557+
'types' =>
2558+
array (
2559+
0 =>
2560+
PHPStan\Type\IntegerRangeType::__set_state(array(
2561+
'min' => -128,
2562+
'max' => 4294967295,
2563+
)),
2564+
1 =>
2565+
PHPStan\Type\StringType::__set_state(array(
2566+
)),
2567+
),
2568+
)),
2569+
'allArrays' => NULL,
2570+
'keyTypes' =>
2571+
array (
2572+
0 =>
2573+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2574+
'value' => 'email',
2575+
'isClassString' => false,
2576+
)),
2577+
1 =>
2578+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2579+
'value' => 0,
2580+
)),
2581+
2 =>
2582+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2583+
'value' => 'adaid',
2584+
'isClassString' => false,
2585+
)),
2586+
3 =>
2587+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2588+
'value' => 1,
2589+
)),
2590+
4 =>
2591+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2592+
'value' => 'gesperrt',
2593+
'isClassString' => false,
2594+
)),
2595+
5 =>
2596+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2597+
'value' => 2,
2598+
)),
2599+
6 =>
2600+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
2601+
'value' => 'freigabe1u1',
2602+
'isClassString' => false,
2603+
)),
2604+
7 =>
2605+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2606+
'value' => 3,
2607+
)),
2608+
),
2609+
'valueTypes' =>
2610+
array (
2611+
0 =>
2612+
PHPStan\Type\StringType::__set_state(array(
2613+
)),
2614+
1 =>
2615+
PHPStan\Type\StringType::__set_state(array(
2616+
)),
2617+
2 =>
2618+
PHPStan\Type\IntegerRangeType::__set_state(array(
2619+
'min' => 0,
2620+
'max' => 4294967295,
2621+
)),
2622+
3 =>
2623+
PHPStan\Type\IntegerRangeType::__set_state(array(
2624+
'min' => 0,
2625+
'max' => 4294967295,
2626+
)),
2627+
4 =>
2628+
PHPStan\Type\IntegerRangeType::__set_state(array(
2629+
'min' => -128,
2630+
'max' => 127,
2631+
)),
2632+
5 =>
2633+
PHPStan\Type\IntegerRangeType::__set_state(array(
2634+
'min' => -128,
2635+
'max' => 127,
2636+
)),
2637+
6 =>
2638+
PHPStan\Type\IntegerRangeType::__set_state(array(
2639+
'min' => -128,
2640+
'max' => 127,
2641+
)),
2642+
7 =>
2643+
PHPStan\Type\IntegerRangeType::__set_state(array(
2644+
'min' => -128,
2645+
'max' => 127,
2646+
)),
2647+
),
2648+
'nextAutoIndex' => 4,
2649+
'optionalKeys' =>
2650+
array (
2651+
),
2652+
)),
25102653
1 =>
25112654
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
25122655
'keyType' =>
@@ -2599,6 +2742,90 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
25992742
array (
26002743
),
26012744
)),
2745+
2 =>
2746+
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
2747+
'keyType' =>
2748+
PHPStan\Type\UnionType::__set_state(array(
2749+
'types' =>
2750+
array (
2751+
0 =>
2752+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2753+
'value' => 0,
2754+
)),
2755+
1 =>
2756+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2757+
'value' => 1,
2758+
)),
2759+
2 =>
2760+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2761+
'value' => 2,
2762+
)),
2763+
3 =>
2764+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2765+
'value' => 3,
2766+
)),
2767+
),
2768+
)),
2769+
'itemType' =>
2770+
PHPStan\Type\UnionType::__set_state(array(
2771+
'types' =>
2772+
array (
2773+
0 =>
2774+
PHPStan\Type\IntegerRangeType::__set_state(array(
2775+
'min' => -128,
2776+
'max' => 4294967295,
2777+
)),
2778+
1 =>
2779+
PHPStan\Type\StringType::__set_state(array(
2780+
)),
2781+
),
2782+
)),
2783+
'allArrays' => NULL,
2784+
'keyTypes' =>
2785+
array (
2786+
0 =>
2787+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2788+
'value' => 0,
2789+
)),
2790+
1 =>
2791+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2792+
'value' => 1,
2793+
)),
2794+
2 =>
2795+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2796+
'value' => 2,
2797+
)),
2798+
3 =>
2799+
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
2800+
'value' => 3,
2801+
)),
2802+
),
2803+
'valueTypes' =>
2804+
array (
2805+
0 =>
2806+
PHPStan\Type\StringType::__set_state(array(
2807+
)),
2808+
1 =>
2809+
PHPStan\Type\IntegerRangeType::__set_state(array(
2810+
'min' => 0,
2811+
'max' => 4294967295,
2812+
)),
2813+
2 =>
2814+
PHPStan\Type\IntegerRangeType::__set_state(array(
2815+
'min' => -128,
2816+
'max' => 127,
2817+
)),
2818+
3 =>
2819+
PHPStan\Type\IntegerRangeType::__set_state(array(
2820+
'min' => -128,
2821+
'max' => 127,
2822+
)),
2823+
),
2824+
'nextAutoIndex' => 4,
2825+
'optionalKeys' =>
2826+
array (
2827+
),
2828+
)),
26022829
),
26032830
),
26042831
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'my_other_table\' LIMIT 1' =>
@@ -3611,6 +3838,98 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
36113838
'error' => NULL,
36123839
'result' =>
36133840
array (
3841+
1 =>
3842+
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
3843+
'keyType' =>
3844+
PHPStan\Type\UnionType::__set_state(array(
3845+
'types' =>
3846+
array (
3847+
0 =>
3848+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3849+
'value' => 'adaid',
3850+
'isClassString' => false,
3851+
)),
3852+
1 =>
3853+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3854+
'value' => 'email',
3855+
'isClassString' => false,
3856+
)),
3857+
2 =>
3858+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3859+
'value' => 'freigabe1u1',
3860+
'isClassString' => false,
3861+
)),
3862+
3 =>
3863+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3864+
'value' => 'gesperrt',
3865+
'isClassString' => false,
3866+
)),
3867+
),
3868+
)),
3869+
'itemType' =>
3870+
PHPStan\Type\UnionType::__set_state(array(
3871+
'types' =>
3872+
array (
3873+
0 =>
3874+
PHPStan\Type\IntegerRangeType::__set_state(array(
3875+
'min' => -128,
3876+
'max' => 4294967295,
3877+
)),
3878+
1 =>
3879+
PHPStan\Type\StringType::__set_state(array(
3880+
)),
3881+
),
3882+
)),
3883+
'allArrays' => NULL,
3884+
'keyTypes' =>
3885+
array (
3886+
0 =>
3887+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3888+
'value' => 'email',
3889+
'isClassString' => false,
3890+
)),
3891+
1 =>
3892+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3893+
'value' => 'adaid',
3894+
'isClassString' => false,
3895+
)),
3896+
2 =>
3897+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3898+
'value' => 'gesperrt',
3899+
'isClassString' => false,
3900+
)),
3901+
3 =>
3902+
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
3903+
'value' => 'freigabe1u1',
3904+
'isClassString' => false,
3905+
)),
3906+
),
3907+
'valueTypes' =>
3908+
array (
3909+
0 =>
3910+
PHPStan\Type\StringType::__set_state(array(
3911+
)),
3912+
1 =>
3913+
PHPStan\Type\IntegerRangeType::__set_state(array(
3914+
'min' => 0,
3915+
'max' => 4294967295,
3916+
)),
3917+
2 =>
3918+
PHPStan\Type\IntegerRangeType::__set_state(array(
3919+
'min' => -128,
3920+
'max' => 127,
3921+
)),
3922+
3 =>
3923+
PHPStan\Type\IntegerRangeType::__set_state(array(
3924+
'min' => -128,
3925+
'max' => 127,
3926+
)),
3927+
),
3928+
'nextAutoIndex' => 0,
3929+
'optionalKeys' =>
3930+
array (
3931+
),
3932+
)),
36143933
3 =>
36153934
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
36163935
'keyType' =>

.phpunit-phpstan-dba.cache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,5 +3013,9 @@ Simulated query: SELECT email, adaid, gesperrt, freigabe1u1 FROM ada . WHERE ema
30133013
array (
30143014
'error' => NULL,
30153015
),
3016+
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE gesperrt=1' =>
3017+
array (
3018+
'error' => NULL,
3019+
),
30163020
),
30173021
);

src/Rules/SyntaxErrorInQueryFunctionRule.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,13 @@ public function processNode(Node $node, Scope $scope): array
7777
}
7878

7979
$queryReflection = new QueryReflection();
80-
$queryString = $queryReflection->resolveQueryString($args[$queryArgPosition]->value, $scope);
81-
if (null === $queryString) {
82-
return [];
83-
}
84-
85-
$error = $queryReflection->validateQueryString($queryString);
86-
if (null !== $error) {
87-
return [
88-
RuleErrorBuilder::message('Query error: '.$error->getMessage().' ('.$error->getCode().').')->line($node->getLine())->build(),
89-
];
80+
foreach ($queryReflection->resolveQueryStrings($args[$queryArgPosition]->value, $scope) as $queryString) {
81+
$error = $queryReflection->validateQueryString($queryString);
82+
if (null !== $error) {
83+
return [
84+
RuleErrorBuilder::message('Query error: '.$error->getMessage().' ('.$error->getCode().').')->line($node->getLine())->build(),
85+
];
86+
}
9087
}
9188

9289
return [];

tests/SyntaxErrorInQueryFunctionRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function testSyntaxErrorInQueryRule(): void
2929
"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).",
3030
21,
3131
],
32+
[
33+
"Query error: Unknown column 'asdsa' in 'where clause' (1054).",
34+
41,
35+
],
3236
]);
3337
}
3438
}

0 commit comments

Comments
 (0)