1 | <?php |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
2 | |||
3 | namespace ClickHouseDB\Query\Degeneration; |
||
4 | |||
5 | use ClickHouseDB\Exception\QueryException; |
||
6 | use ClickHouseDB\Query\Degeneration; |
||
7 | |||
8 | class Conditions implements Degeneration |
||
9 | { |
||
10 | /** |
||
0 ignored issues
–
show
|
|||
11 | * @var array |
||
0 ignored issues
–
show
|
|||
12 | */ |
||
13 | protected $bindings = []; |
||
14 | |||
15 | /** |
||
16 | * @param array $bindings |
||
0 ignored issues
–
show
|
|||
17 | */ |
||
18 | 3 | public function bindParams(array $bindings) |
|
0 ignored issues
–
show
|
|||
19 | { |
||
20 | 3 | foreach ($bindings as $column => $value) { |
|
21 | 3 | $this->bindings[$column] = $value; |
|
22 | } |
||
23 | 3 | } |
|
24 | |||
25 | public function getBind(): array |
||
0 ignored issues
–
show
|
|||
26 | 3 | { |
|
27 | return $this->bindings; |
||
28 | 3 | } |
|
29 | 3 | ||
30 | 3 | static function __ifsets($matches, $markers) |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
__ifsets .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
31 | { |
||
32 | 3 | $content_false = ''; |
|
0 ignored issues
–
show
|
|||
33 | 3 | $condition = ''; |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
34 | 3 | $flag_else = ''; |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
35 | 3 | //print_r($matches); |
|
36 | if (sizeof($matches) == 4) { |
||
0 ignored issues
–
show
|
|||
37 | list($condition, $preset, $variable, $content_true) = $matches; |
||
0 ignored issues
–
show
|
|||
38 | } elseif (sizeof($matches) == 6) { |
||
0 ignored issues
–
show
|
|||
39 | 3 | list($condition, $preset, $variable, $content_true, $flag_else, $content_false) = $matches; |
|
0 ignored issues
–
show
|
|||
40 | 3 | } else { |
|
41 | throw new QueryException('Error in parse Conditions' . json_encode($matches)); |
||
0 ignored issues
–
show
|
|||
42 | 3 | } |
|
43 | 3 | $variable = trim($variable); |
|
0 ignored issues
–
show
|
|||
44 | 3 | $preset = strtolower(trim($preset)); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
45 | 3 | ||
46 | if ($preset == '') { |
||
0 ignored issues
–
show
|
|||
47 | 2 | return (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) |
|
0 ignored issues
–
show
|
|||
48 | 2 | ? $content_true |
|
0 ignored issues
–
show
|
|||
49 | : $content_false; |
||
0 ignored issues
–
show
|
|||
50 | 2 | } |
|
51 | 2 | if ($preset == 'set') { |
|
0 ignored issues
–
show
|
|||
52 | 2 | return (isset($markers[$variable]) && !empty($markers[$variable])) ? $content_true : $content_false; |
|
0 ignored issues
–
show
|
|||
53 | 2 | } |
|
54 | if ($preset == 'bool') { |
||
0 ignored issues
–
show
|
|||
55 | 2 | return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable] == true) |
|
0 ignored issues
–
show
|
|||
56 | 2 | ? $content_true |
|
0 ignored issues
–
show
|
|||
57 | 2 | : $content_false; |
|
0 ignored issues
–
show
|
|||
58 | 2 | } |
|
59 | if ($preset == 'string') { |
||
0 ignored issues
–
show
|
|||
60 | 2 | return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable])) |
|
0 ignored issues
–
show
|
|||
61 | 2 | ? $content_true |
|
0 ignored issues
–
show
|
|||
62 | 2 | : $content_false; |
|
0 ignored issues
–
show
|
|||
63 | 2 | } |
|
64 | if ($preset == 'int') { |
||
0 ignored issues
–
show
|
|||
65 | return (isset($markers[$variable]) && intval($markers[$variable]) <> 0) |
||
0 ignored issues
–
show
|
|||
66 | ? $content_true |
||
0 ignored issues
–
show
|
|||
67 | : $content_false; |
||
0 ignored issues
–
show
|
|||
68 | } |
||
69 | |||
70 | return ''; |
||
71 | } |
||
72 | |||
73 | 3 | /** |
|
74 | * @param string $sql |
||
0 ignored issues
–
show
|
|||
75 | 3 | * @return mixed |
|
76 | */ |
||
77 | public function process($sql) |
||
0 ignored issues
–
show
|
|||
78 | 3 | { |
|
79 | 3 | $markers = $this->bindings; |
|
80 | 3 | ||
81 | // ------ if/else conditions & if[set|int]/else conditions ----- |
||
82 | $sql = preg_replace_callback('#\{if(.{0,}?)\s+([^\}]+?)\}(.+?)(\{else\}([^\{]+?)?)?\s*\{\/if}#sui', function ($matches) use ($markers) { |
||
0 ignored issues
–
show
|
|||
83 | 3 | return self::__ifsets($matches, $markers); |
|
84 | } |
||
85 | , $sql); |
||
0 ignored issues
–
show
|
|||
86 | |||
87 | return $sql; |
||
88 | |||
89 | /* |
||
90 | * $ifint var ELSE {ENDIF} |
||
91 | * |
||
92 | */ |
||
93 | |||
94 | // stackoverflow |
||
95 | // if(whatever) { } else { adsffdsa } else if() { } |
||
96 | // /^if\s*\((.*?)\)\s*{(.*?)}(\s*(else|else\s+if\s*\((.*?)\))\s*{(.*?)})*/ |
||
97 | // if (condition_function(params)) { |
||
98 | // statements; |
||
99 | //} |
||
100 | // if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*; |
||
101 | // @if\s*\(\s*([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif |
||
102 | // @if \s* \( \s* ([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif |
||
103 | // [^}] |
||
104 | |||
105 | // // 3. process if conditions |
||
106 | // $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { |
||
107 | // list($condition, $variable, $content) = $matches; |
||
108 | // if (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) { |
||
109 | // return $content; |
||
110 | // } |
||
111 | // }, $sql); |
||
112 | |||
113 | // 1. process if[set|int]/else conditions |
||
114 | // $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {return self::__ifsets($matches, $markers, true); }, $sql); |
||
115 | // $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { return self::__ifsets($matches, $markers, false); }, $sql); |
||
116 | } |
||
0 ignored issues
–
show
|
|||
117 | |||
118 | } |
||
0 ignored issues
–
show
|
|||
119 |