Total Complexity | 90 |
Total Lines | 469 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SelectQuery 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 SelectQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class SelectQuery extends AbstractFacade |
||
23 | { |
||
24 | use InputFieldTrait; |
||
25 | use SelectTrait; |
||
26 | |||
27 | /** |
||
28 | * Get required data for select on tables |
||
29 | * |
||
30 | * @param SelectEntity $selectEntity |
||
31 | * |
||
32 | * @return SelectEntity |
||
33 | * @throws Exception |
||
34 | */ |
||
35 | public function prepareSelect(SelectEntity $selectEntity): SelectEntity |
||
36 | { |
||
37 | $this->setDefaultOptions($selectEntity); |
||
38 | |||
39 | // From select.inc.php |
||
40 | $selectEntity->fields = $this->driver->fields($selectEntity->table); |
||
41 | $this->setFieldsOptions($selectEntity); |
||
42 | if (!$selectEntity->columns && $this->driver->support("table")) { |
||
43 | throw new Exception($this->utils->trans->lang('Unable to select the table') . |
||
44 | ($selectEntity->fields ? "." : ": " . $this->driver->error())); |
||
45 | } |
||
46 | |||
47 | $selectEntity->indexes = $this->driver->indexes($selectEntity->table); |
||
48 | $this->setForeignKeys($selectEntity); |
||
49 | $this->setSelectColumns($selectEntity); |
||
50 | |||
51 | $this->setSelectWhere($selectEntity); |
||
52 | $this->setSelectOrder($selectEntity); |
||
53 | $this->setSelectLimit($selectEntity); |
||
54 | $this->setPrimaryKey($selectEntity); |
||
55 | |||
56 | // $set = null; |
||
57 | // if(isset($rights["insert"]) || !this->driver->support("table")) { |
||
58 | // $set = ""; |
||
59 | // foreach((array) $queryOptions["where"] as $val) { |
||
60 | // if($foreignKeys[$val["col"]] && count($foreignKeys[$val["col"]]) == 1 && ($val["op"] == "=" |
||
61 | // || (!$val["op"] && !preg_match('~[_%]~', $val["val"])) // LIKE in Editor |
||
62 | // )) { |
||
63 | // $set .= "&set" . urlencode("[" . $this->driver->bracketEscape($val["col"]) . "]") . "=" . urlencode($val["val"]); |
||
64 | // } |
||
65 | // } |
||
66 | // } |
||
67 | // $this->admin->selectLinks($tableStatus, $set); |
||
68 | |||
69 | // if($page == "last") |
||
70 | // { |
||
71 | // $isGroup = count($group) < count($select); |
||
72 | // $found_rows = $this->driver->result($this->driver->getRowCountQuery($table, $where, $isGroup, $group)); |
||
73 | // $page = \floor(\max(0, $found_rows - 1) / $limit); |
||
74 | // } |
||
75 | |||
76 | $this->setSelectOptions($selectEntity); |
||
77 | $this->setSelectEntity($selectEntity); |
||
78 | $this->setSelectQuery($selectEntity); |
||
79 | |||
80 | return $selectEntity; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param SelectEntity $selectEntity |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | private function setDefaultOptions(SelectEntity $selectEntity): void |
||
89 | { |
||
90 | $defaultOptions = [ |
||
91 | 'columns' => [], |
||
92 | 'where' => [], |
||
93 | 'order' => [], |
||
94 | 'desc' => [], |
||
95 | 'fulltext' => [], |
||
96 | 'limit' => '50', |
||
97 | 'text_length' => '100', |
||
98 | 'page' => '1', |
||
99 | ]; |
||
100 | foreach ($defaultOptions as $name => $value) { |
||
101 | if (!isset($this->utils->input->values[$name])) { |
||
102 | $this->utils->input->values[$name] = $value; |
||
103 | } |
||
104 | if (!isset($selectEntity->queryOptions[$name])) { |
||
105 | $selectEntity->queryOptions[$name] = $value; |
||
106 | } |
||
107 | } |
||
108 | $page = intval($selectEntity->queryOptions['page']); |
||
109 | if ($page > 0) { |
||
110 | $page -= 1; // Page numbers start at 0 here, instead of 1. |
||
111 | } |
||
112 | $selectEntity->queryOptions['page'] = $page; |
||
113 | $selectEntity->page = $page; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param SelectEntity $selectEntity |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | private function setFieldsOptions(SelectEntity $selectEntity): void |
||
122 | { |
||
123 | $selectEntity->rights = []; // privilege => 0 |
||
124 | $selectEntity->columns = []; // selectable columns |
||
125 | $selectEntity->textLength = 0; |
||
126 | foreach ($selectEntity->fields as $key => $field) { |
||
127 | $name = $this->admin->fieldName($field); |
||
128 | if (isset($field->privileges["select"]) && $name != "") { |
||
129 | $selectEntity->columns[$key] = html_entity_decode(strip_tags($name), ENT_QUOTES); |
||
130 | if ($this->admin->isShortable($field)) { |
||
131 | $this->setSelectTextLength($selectEntity); |
||
132 | } |
||
133 | } |
||
134 | $selectEntity->rights[] = $field->privileges; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Find out foreign keys for each column |
||
140 | * |
||
141 | * @param SelectEntity $selectEntity |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | private function setForeignKeys(SelectEntity $selectEntity): void |
||
146 | { |
||
147 | $selectEntity->foreignKeys = []; |
||
148 | foreach ($this->driver->foreignKeys($selectEntity->table) as $foreignKey) { |
||
149 | foreach ($foreignKey->source as $val) { |
||
150 | $selectEntity->foreignKeys[$val][] = $foreignKey; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param array $value |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | private function colHasValidValue(array $value): bool |
||
161 | { |
||
162 | return $value['fun'] === 'count' || |
||
163 | ($value['col'] !== '' && (!$value['fun'] || |
||
164 | in_array($value['fun'], $this->driver->functions()) || |
||
165 | in_array($value['fun'], $this->driver->grouping()))); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param array $where AND conditions |
||
170 | * @param array $foreignKeys |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | // private function setSelectEmail(array $where, array $foreignKeys) |
||
175 | // { |
||
176 | // return false; |
||
177 | // } |
||
178 | |||
179 | /** |
||
180 | * Apply SQL function |
||
181 | * |
||
182 | * @param string $function |
||
183 | * @param string $column escaped column identifier |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | public function applySqlFunction(string $function, string $column): string |
||
188 | { |
||
189 | if (!$function) { |
||
190 | return $column; |
||
191 | } |
||
192 | if ($function === 'unixepoch') { |
||
193 | return "DATETIME($column, '$function')"; |
||
194 | } |
||
195 | if ($function === 'count distinct') { |
||
196 | return "COUNT(DISTINCT $column)"; |
||
197 | } |
||
198 | return strtoupper($function) . "($column)"; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param SelectEntity $selectEntity |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | private function setSelectColumns(SelectEntity $selectEntity): void |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param array $value |
||
227 | * @param array $fields |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | private function getWhereCondition(array $value, array $fields): string |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param TableFieldEntity $field |
||
260 | * @param array $value |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | private function selectFieldIsValid(TableFieldEntity $field, array $value): bool |
||
265 | { |
||
266 | $op = $value['op']; |
||
267 | $val = $value['val']; |
||
268 | $in = preg_match('~IN$~', $op) ? ',' : ''; |
||
269 | return (preg_match('~^[-\d.' . $in . ']+$~', $val) || |
||
270 | !preg_match('~' . $this->driver->numberRegex() . '|bit~', $field->type)) && |
||
271 | (!preg_match("~[\x80-\xFF]~", $val) || |
||
272 | preg_match('~char|text|enum|set~', $field->type)) && |
||
273 | (!preg_match('~date|timestamp~', $field->type) || |
||
274 | preg_match('~^\d+-\d+-\d+~', $val)); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param array $value |
||
279 | * @param array $fields |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | private function getSelectExpression(array $value, array $fields): string |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param IndexEntity $index |
||
312 | * @param int $i |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | private function getMatchExpression(IndexEntity $index, int $i): string |
||
317 | { |
||
318 | $columns = array_map(function ($column) { |
||
319 | return $this->driver->escapeId($column); |
||
320 | }, $index->columns); |
||
321 | $match = $this->driver->quote($this->utils->input->values['fulltext'][$i]); |
||
322 | if (isset($this->utils->input->values['boolean'][$i])) { |
||
323 | $match .= ' IN BOOLEAN MODE'; |
||
324 | } |
||
325 | return 'MATCH (' . implode(', ', $columns) . ') AGAINST (' . $match . ')'; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param SelectEntity $selectEntity |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | private function setSelectWhere(SelectEntity $selectEntity): void |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param SelectEntity $selectEntity |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | private function setSelectOrder(SelectEntity $selectEntity): void |
||
356 | { |
||
357 | $values = $this->utils->input->values; |
||
358 | $selectEntity->order = []; |
||
359 | foreach ($values['order'] as $key => $value) { |
||
360 | if ($value !== '') { |
||
361 | $regexp = '~^((COUNT\(DISTINCT |[A-Z0-9_]+\()(`(?:[^`]|``)+`|"(?:[^"]|"")+")\)|COUNT\(\*\))$~'; |
||
362 | if (preg_match($regexp, $value) !== false) { |
||
363 | $value = $this->driver->escapeId($value); |
||
364 | } |
||
365 | if (isset($values['desc'][$key]) && intval($values['desc'][$key]) !== 0) { |
||
366 | $value .= ' DESC'; |
||
367 | } |
||
368 | $selectEntity->order[] = $value; |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param SelectEntity $selectEntity |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | private function setSelectLimit(SelectEntity $selectEntity): void |
||
379 | { |
||
380 | $selectEntity->limit = intval($this->utils->input->values['limit'] ?? 50); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param SelectEntity $selectEntity |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | private function setSelectTextLength(SelectEntity $selectEntity): void |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param SelectEntity $selectEntity |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | private function setPrimaryKey(SelectEntity $selectEntity): void |
||
419 | } |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param SelectEntity $selectEntity |
||
424 | * |
||
425 | * @return void |
||
426 | */ |
||
427 | public function setSelectQuery(SelectEntity $selectEntity): void |
||
428 | { |
||
429 | $query = $this->driver->buildSelectQuery($selectEntity->tableSelect); |
||
430 | // From adminer.inc.php |
||
431 | $selectEntity->query = str_replace("\n", " ", $query); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param SelectEntity $selectEntity |
||
436 | * |
||
437 | * @return void |
||
438 | */ |
||
439 | private function setSelectOptions(SelectEntity $selectEntity): void |
||
440 | { |
||
441 | $selectEntity->options = [ |
||
442 | 'columns' => $this->getColumnsOptions($selectEntity->select, |
||
443 | $selectEntity->columns, $selectEntity->queryOptions), |
||
444 | 'filters' => $this->getFiltersOptions($selectEntity->columns, |
||
445 | $selectEntity->indexes, $selectEntity->queryOptions), |
||
446 | 'sorting' => $this->getSortingOptions($selectEntity->columns, |
||
447 | $selectEntity->queryOptions), |
||
448 | 'limit' => $this->getLimitOptions($selectEntity->limit), |
||
449 | 'length' => $this->getLengthOptions($selectEntity->textLength), |
||
450 | // 'action' => $this->getActionOptions($selectEntity->indexes), |
||
451 | ]; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * @param SelectEntity $selectEntity |
||
456 | * |
||
457 | * @return void |
||
458 | */ |
||
459 | private function setSelectEntity(SelectEntity $selectEntity): void |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Print action box in select |
||
495 | * |
||
545 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths