Total Complexity | 47 |
Total Lines | 349 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like HandlesAqlGrammar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HandlesAqlGrammar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait HandlesAqlGrammar |
||
11 | { |
||
12 | /** |
||
13 | * Available predicate operators. |
||
14 | * |
||
15 | * @var array<string, int> |
||
16 | */ |
||
17 | protected array $comparisonOperators = [ |
||
18 | '==' => 1, |
||
19 | '!=' => 1, |
||
20 | '<' => 1, |
||
21 | '>' => 1, |
||
22 | '<=' => 1, |
||
23 | '>=' => 1, |
||
24 | 'IN' => 1, |
||
25 | 'NOT IN' => 1, |
||
26 | 'LIKE' => 1, |
||
27 | '~' => 1, |
||
28 | '!~' => 1, |
||
29 | 'ALL ==' => 1, |
||
30 | 'ALL !=' => 1, |
||
31 | 'ALL <' => 1, |
||
32 | 'ALL >' => 1, |
||
33 | 'ALL <=' => 1, |
||
34 | 'ALL >=' => 1, |
||
35 | 'ALL IN' => 1, |
||
36 | 'ANY ==' => 1, |
||
37 | 'ANY !=' => 1, |
||
38 | 'ANY <' => 1, |
||
39 | 'ANY >' => 1, |
||
40 | 'ANY <=' => 1, |
||
41 | 'ANY >=' => 1, |
||
42 | 'ANY IN' => 1, |
||
43 | 'NONE ==' => 1, |
||
44 | 'NONE !=' => 1, |
||
45 | 'NONE <' => 1, |
||
46 | 'NONE >' => 1, |
||
47 | 'NONE <=' => 1, |
||
48 | 'NONE >=' => 1, |
||
49 | 'NONE IN' => 1, |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var array<string, int> |
||
54 | */ |
||
55 | protected array $arithmeticOperators = [ |
||
56 | '+' => 1, |
||
57 | '-' => 1, |
||
58 | '*' => 1, |
||
59 | '/' => 1, |
||
60 | '%' => 1, |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * @var array|int[] |
||
65 | */ |
||
66 | protected array $logicalOperators = [ |
||
67 | 'AND' => 1, |
||
68 | '&&' => 1, |
||
69 | 'OR' => 1, |
||
70 | '||' => 1, |
||
71 | 'NOT' => 1, |
||
72 | '!' => 1, |
||
73 | ]; |
||
74 | |||
75 | protected string $rangeOperator = '..'; |
||
76 | |||
77 | public function isBind(mixed $value): bool |
||
78 | { |
||
79 | if (is_string($value) && preg_match('/^@?[0-9]{4}_[a-zA-Z0-9_$]_[0-9_]+$/', $value)) { |
||
80 | return true; |
||
81 | } |
||
82 | |||
83 | return false; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get the appropriate query parameter place-holder for a value. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | */ |
||
91 | public function parameter($value): string |
||
92 | { |
||
93 | return $this->isExpression($value) ? $this->getValue($value) : (string) $value; |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Quote the given string literal. |
||
99 | * |
||
100 | * @param string|array<string> $value |
||
101 | * @return string |
||
102 | */ |
||
103 | public function quoteString($value) |
||
110 | } |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Wrap a value in keyword identifiers. |
||
115 | * |
||
116 | * @param Array<mixed>|Expression|string $value |
||
117 | * @return array<mixed>|float|int|string |
||
118 | * |
||
119 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
120 | */ |
||
121 | public function wrap($value) |
||
122 | { |
||
123 | if ($value instanceof Expression) { |
||
124 | return $value->getValue($this); |
||
125 | } |
||
126 | |||
127 | if (is_array($value)) { |
||
128 | foreach ($value as $key => $subvalue) { |
||
129 | $value[$key] = $this->wrap($subvalue); |
||
130 | } |
||
131 | return $value; |
||
132 | } |
||
133 | |||
134 | // If the value being wrapped has a column alias we will need to separate out |
||
135 | // the pieces so we can wrap each of the segments of the expression on its |
||
136 | // own, and then join these both back together using the "as" connector. |
||
137 | if (is_string($value) && stripos($value, ' as ') !== false) { |
||
138 | return $this->wrapAliasedValue($value); |
||
139 | } |
||
140 | |||
141 | return $this->wrapSegments(explode('.', $value)); |
||
142 | } |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Wrap a table in keyword identifiers. |
||
147 | * |
||
148 | * @param Expression|string $table |
||
149 | * @return float|int|string |
||
150 | */ |
||
151 | public function wrapTable($table, $prefix = null) |
||
152 | { |
||
153 | if (!$table instanceof Expression) { |
||
154 | $wrappedTable = $this->wrap(($prefix ?? $this->tablePrefix) . $table); |
||
155 | |||
156 | assert(!is_array($wrappedTable)); |
||
157 | |||
158 | return $wrappedTable; |
||
159 | } |
||
160 | |||
161 | return $table->getValue($this); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Wrap a single string in keyword identifiers. |
||
166 | * |
||
167 | * @param string $value |
||
168 | * @return string |
||
169 | */ |
||
170 | protected function wrapValue($value) |
||
171 | { |
||
172 | $postfix = ''; |
||
173 | if ($value === 'groupsVariable') { |
||
174 | $postfix = '[*]'; |
||
175 | } |
||
176 | |||
177 | if ($value === '*') { |
||
178 | return $value; |
||
179 | } |
||
180 | |||
181 | return '`' . str_replace('`', '``', $value) . '`' . $postfix; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Wrap a single string in keyword identifiers. |
||
186 | * |
||
187 | * @param string $value |
||
188 | * @return string |
||
189 | */ |
||
190 | protected function wrapAttribute($value) |
||
191 | { |
||
192 | if (!is_string($value)) { |
||
193 | return $value; |
||
194 | } |
||
195 | return '`' . str_replace('`', '``', $value) . '`'; |
||
196 | } |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Wrap a subquery single string in braces. |
||
201 | */ |
||
202 | public function wrapSubquery(string $subquery): string |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param array<mixed> $data |
||
209 | * @return string |
||
210 | */ |
||
211 | public function generateAqlObject(array $data): string |
||
212 | { |
||
213 | $data = Arr::undot($data); |
||
214 | return $this->generateAqlObjectString($data); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param array<mixed> $data |
||
219 | * @return string |
||
220 | */ |
||
221 | protected function generateAqlObjectString(array $data): string |
||
222 | { |
||
223 | foreach ($data as $key => $value) { |
||
224 | $prefix = $this->wrapAttribute($key) . ': '; |
||
225 | |||
226 | if (is_numeric($key)) { |
||
227 | $prefix = ''; |
||
228 | } |
||
229 | |||
230 | if (is_bool($value)) { |
||
231 | $booleanString = ($value) ? 'true' : 'false'; |
||
232 | $data[$key] = $prefix . $booleanString; |
||
233 | |||
234 | continue; |
||
235 | } |
||
236 | |||
237 | if (is_array($value)) { |
||
238 | $data[$key] = $prefix . $this->generateAqlObjectString($value); |
||
239 | continue; |
||
240 | } |
||
241 | |||
242 | if ($value instanceof Expression) { |
||
243 | $data[$key] = $prefix . $value->getValue($this); |
||
244 | continue; |
||
245 | } |
||
246 | |||
247 | $data[$key] = $prefix . $value; |
||
248 | } |
||
249 | |||
250 | $returnString = implode(', ', $data); |
||
251 | |||
252 | if (array_is_list($data)) { |
||
253 | return '[' . $returnString . ']'; |
||
254 | } |
||
255 | |||
256 | return '{' . $returnString . '}'; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Substitute the given bindings into the given raw AQL query. |
||
261 | * |
||
262 | * @param string $aql |
||
263 | * @param array<mixed> $bindings |
||
264 | * @return string |
||
265 | */ |
||
266 | public function substituteBindingsIntoRawSql($aql, $bindings) |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Determine if the given string is a JSON selector. |
||
286 | * |
||
287 | * @param string $value |
||
288 | * @return bool |
||
289 | */ |
||
290 | public function isJsonSelector($value) |
||
297 | } |
||
298 | |||
299 | public function convertJsonFields(mixed $data): mixed |
||
300 | { |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param array<string> $fields |
||
318 | * @return array<string> |
||
319 | */ |
||
320 | public function convertJsonValuesToDotNotation(array $fields): array |
||
321 | { |
||
322 | foreach ($fields as $key => $value) { |
||
323 | if ($this->isJsonSelector($value)) { |
||
324 | $fields[$key] = str_replace('->', '.', $value); |
||
325 | } |
||
326 | } |
||
327 | return $fields; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @param array<string> $fields |
||
332 | * @return array<string> |
||
333 | */ |
||
334 | public function convertJsonKeysToDotNotation(array $fields): array |
||
335 | { |
||
336 | foreach ($fields as $key => $value) { |
||
337 | if ($this->isJsonSelector($key)) { |
||
338 | $fields[str_replace('->', '.', $key)] = $value; |
||
339 | unset($fields[$key]); |
||
340 | } |
||
341 | } |
||
342 | return $fields; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Translate sql operators to their AQL equivalent where possible. |
||
347 | * |
||
348 | * @param string $operator |
||
349 | * |
||
350 | * @return mixed|string |
||
351 | */ |
||
352 | protected function translateOperator(string $operator) |
||
359 | } |
||
360 | |||
361 | } |
||
362 |