-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDibiReflection.php
36 lines (29 loc) · 1.36 KB
/
DibiReflection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
declare(strict_types=1);
namespace staabm\PHPStanDba\DibiReflection;
final class DibiReflection
{
public function rewriteQuery(string $queryString): ?string
{
// see https://dibiphp.com/en/documentation#toc-modifiers
$queryString = str_replace('%lmt', 'LIMIT 1', $queryString);
$queryString = str_replace('%ofs', ', 1', $queryString);
$queryString = str_replace('%in', '(1)', $queryString);
$queryString = str_replace('%l', '(1)', $queryString);
$queryString = preg_replace('#%(bin|sN|iN|f|i|s)#', "'1'", $queryString) ?? '';
$queryString = preg_replace('#%(t|d|dt)#', '"2000-1-1"', $queryString) ?? '';
$queryString = preg_replace('#%(and|or)#', '(1 = 1)', $queryString) ?? '';
$queryString = preg_replace('#%~?like~?#', '"%1%"', $queryString) ?? '';
$queryString = preg_replace('#date_format\([\w.]+?,.+?\)#', '"2000-1-1"', $queryString) ?? '';
$unsupportedPlaceholders = ['%ex', '%n', '%by', '%sql', '%m', '%N'];
foreach ($unsupportedPlaceholders as $unsupportedPlaceholder) {
if (strpos($queryString, $unsupportedPlaceholder) > 0) {
return null;
}
}
if (0 !== preg_match('#^\s*(START|ROLLBACK|SET|SAVEPOINT|SHOW)#i', $queryString)) {
$queryString = null;
}
return $queryString;
}
}