| Total Complexity | 45 |
| Total Lines | 380 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SelectFacade 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 SelectFacade, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class SelectFacade extends AbstractFacade |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var SelectQuery |
||
| 32 | */ |
||
| 33 | private SelectQuery $selectQuery; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var SelectEntity|null |
||
| 37 | */ |
||
| 38 | private SelectEntity|null $selectEntity = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param AbstractFacade $dbFacade |
||
| 42 | */ |
||
| 43 | public function __construct(AbstractFacade $dbFacade) |
||
| 44 | { |
||
| 45 | parent::__construct($dbFacade); |
||
| 46 | |||
| 47 | $this->selectQuery = new SelectQuery($dbFacade); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $table The table name |
||
| 52 | * @param array $queryOptions The query options |
||
| 53 | * |
||
| 54 | * @return void |
||
| 55 | * @throws Exception |
||
| 56 | */ |
||
| 57 | private function setSelectEntity(string $table, array $queryOptions = []): void |
||
| 58 | { |
||
| 59 | $tableStatus = $this->driver->tableStatusOrName($table); |
||
| 60 | $tableName = $this->admin->tableName($tableStatus); |
||
| 61 | $this->selectEntity = new SelectEntity($table, |
||
| 62 | $tableName, $tableStatus, $queryOptions); |
||
| 63 | $this->selectQuery->prepareSelect($this->selectEntity); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get required data for create/update on tables |
||
| 68 | * |
||
| 69 | * @param string $table The table name |
||
| 70 | * @param array $queryOptions The query options |
||
| 71 | * |
||
| 72 | * @return SelectEntity |
||
| 73 | * @throws Exception |
||
| 74 | */ |
||
| 75 | public function getSelectData(string $table, array $queryOptions = []): SelectEntity |
||
| 76 | { |
||
| 77 | $this->setSelectEntity($table, $queryOptions); |
||
| 78 | return $this->selectEntity; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | private function executeSelect(): void |
||
| 85 | { |
||
| 86 | // From driver.inc.php |
||
| 87 | $startTimestamp = microtime(true); |
||
| 88 | $statement = $this->driver->execute($this->selectEntity->query); |
||
| 89 | $this->selectEntity->duration = max(0, microtime(true) - $startTimestamp); |
||
| 90 | |||
| 91 | // From adminer.inc.php |
||
| 92 | if (!$statement) { |
||
| 93 | $this->selectEntity = $this->driver->error(); |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | // From select.inc.php |
||
| 98 | $this->selectEntity->rows = []; |
||
| 99 | while (($row = $statement->fetchAssoc())) { |
||
| 100 | if ($this->selectEntity->page && $this->driver->jush() == "oracle") { |
||
| 101 | unset($row["RNUM"]); |
||
| 102 | } |
||
| 103 | $this->selectEntity->rows[] = $row; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $key |
||
| 109 | * @param int $rank |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | private function getResultHeaderItem(string $key, int $rank): array |
||
| 114 | { |
||
| 115 | $valueKey = key($this->selectEntity->select); |
||
| 116 | $value = $this->selectEntity->queryOptions["columns"][$valueKey] ?? []; |
||
| 117 | |||
| 118 | $fun = $value["fun"] ?? ''; |
||
| 119 | $fieldKey = !$this->selectEntity->select ? $key : |
||
| 120 | ($value["col"] ?? current($this->selectEntity->select)); |
||
| 121 | $field = $this->selectEntity->fields[$fieldKey]; |
||
| 122 | $name = !$field ? ($fun ? "*" : $key) : |
||
| 123 | $this->admin->fieldName($field, $rank); |
||
| 124 | |||
| 125 | return [$fun, $name, $field]; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $key |
||
| 130 | * @param mixed $value |
||
| 131 | * @param int $rank |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | private function getResultHeader(string $key, $value, int $rank): array |
||
| 136 | { |
||
| 137 | if (isset($this->selectEntity->unselected[$key])) { |
||
| 138 | return []; |
||
| 139 | } |
||
| 140 | |||
| 141 | [$fun, $name, $field] = $this->getResultHeaderItem($key, $rank); |
||
| 142 | $header = compact('field', 'name'); |
||
| 143 | if ($name != "") { |
||
| 144 | $this->selectEntity->names[$key] = $name; |
||
| 145 | // $href = remove_from_uri('(order|desc)[^=]*|page') . '&order%5B0%5D=' . urlencode($key); |
||
| 146 | // $desc = "&desc%5B0%5D=1"; |
||
| 147 | $header['column'] = $this->driver->escapeId($key); |
||
| 148 | // $header['key'] = $this->utils->str |
||
| 149 | // ->html($this->driver->bracketEscape($key)); |
||
| 150 | //! columns looking like functions |
||
| 151 | $header['title'] = $this->selectQuery->applySqlFunction($fun, $name); |
||
| 152 | } |
||
| 153 | // $functions[$key] = $fun; |
||
| 154 | next($this->selectEntity->select); |
||
| 155 | return $header; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the result headers from the first result row |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | private function getResultHeaders(): void |
||
| 163 | { |
||
| 164 | // Results headers |
||
| 165 | $this->selectEntity->headers = [ |
||
| 166 | '', // !$group && $select ? '' : lang('Modify'); |
||
| 167 | ]; |
||
| 168 | $this->selectEntity->names = []; |
||
| 169 | // $this->selectEntity->functions = []; |
||
| 170 | reset($this->selectEntity->select); |
||
| 171 | |||
| 172 | $rank = 1; |
||
| 173 | $firstResultRow = $this->selectEntity->rows[0]; |
||
| 174 | foreach ($firstResultRow as $key => $value) { |
||
| 175 | $header = $this->getResultHeader($key, $value, $rank); |
||
| 176 | if ($header['name'] ?? '' !== '') { |
||
| 177 | $rank++; |
||
| 178 | } |
||
| 179 | $this->selectEntity->headers[] = $header; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param array $rows |
||
| 185 | * @param array $queryOptions |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | /*private function getValuesLengths(array $rows, array $queryOptions): array |
||
| 190 | { |
||
| 191 | $lengths = []; |
||
| 192 | if($queryOptions["modify"]) |
||
| 193 | { |
||
| 194 | foreach($rows as $row) |
||
| 195 | { |
||
| 196 | foreach($row as $key => $value) |
||
| 197 | { |
||
| 198 | $lengths[$key] = \max($lengths[$key], \min(40, strlen(\utf8_decode($value)))); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | return $lengths; |
||
| 203 | }*/ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param array $row |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | private function getUniqueIds(array $row): array |
||
| 211 | { |
||
| 212 | $uniqueIds = $this->admin->uniqueIds($row, $this->selectEntity->indexes); |
||
| 213 | if (empty($uniqueIds)) { |
||
| 214 | $pattern = '~^(COUNT\((\*|(DISTINCT )?`(?:[^`]|``)+`)\)' . |
||
| 215 | '|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\(`(?:[^`]|``)+`\))$~'; |
||
| 216 | foreach ($row as $key => $value) { |
||
| 217 | if (!preg_match($pattern, $key)) { |
||
| 218 | //! columns looking like functions |
||
| 219 | $uniqueIds[$key] = $value; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | return $uniqueIds; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $type |
||
| 228 | * @param string $value |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | private function shouldEncodeRowId(string $type, string $value): bool |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $key |
||
| 242 | * @param string $collation |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | private function getRowIdMd5Key(string $key, string $collation): string |
||
| 247 | { |
||
| 248 | return $this->driver->jush() != 'sql' || |
||
| 249 | preg_match("~^utf8~", $collation) ? $key : |
||
| 250 | "CONVERT($key USING " . $this->driver->charset() . ")"; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $key |
||
| 255 | * @param string $value |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | private function getRowIdValue(string $key, string $value): array |
||
| 260 | { |
||
| 261 | $key = trim($key); |
||
| 262 | $type = ''; |
||
| 263 | $collation = ''; |
||
| 264 | if (isset($this->selectEntity->fields[$key])) { |
||
| 265 | $type = $this->selectEntity->fields[$key]->type; |
||
| 266 | $collation = $this->selectEntity->fields[$key]->collation; |
||
| 267 | } |
||
| 268 | if ($this->shouldEncodeRowId($type, $value)) { |
||
| 269 | if (!strpos($key, '(')) { |
||
| 270 | //! columns looking like functions |
||
| 271 | $key = $this->driver->escapeId($key); |
||
| 272 | } |
||
| 273 | $key = "MD5(" . $this->getRowIdMd5Key($key, $collation) . ")"; |
||
| 274 | $value = md5($value); |
||
| 275 | } |
||
| 276 | return [$key, $value]; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $row |
||
| 281 | * |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | private function getRowIds(array $row): array |
||
| 285 | { |
||
| 286 | $uniqueIds = $this->getUniqueIds($row); |
||
| 287 | // Unique identifier to edit returned data. |
||
| 288 | // $unique_idf = ""; |
||
| 289 | $rowIds = ['where' => [], 'null' => []]; |
||
| 290 | foreach ($uniqueIds as $key => $value) { |
||
| 291 | [$key, $value] = $this->getRowIdValue($key, $value); |
||
| 292 | // $unique_idf .= "&" . ($value !== null ? \urlencode("where[" . |
||
| 293 | // $this->driver->bracketEscape($key) . "]") . "=" . |
||
| 294 | // \urlencode($value) : \urlencode("null[]") . "=" . \urlencode($key)); |
||
| 295 | if ($value === null) { |
||
| 296 | $rowIds['null'][] = $this->driver->bracketEscape($key); |
||
| 297 | continue; |
||
| 298 | } |
||
| 299 | $rowIds['where'][$this->driver->bracketEscape($key)] = $value; |
||
| 300 | } |
||
| 301 | return $rowIds; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $key |
||
| 306 | * @param mixed $value |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private function getRowColumn(string $key, $value): array |
||
| 311 | { |
||
| 312 | $field = $this->selectEntity->fields[$key] ?? new TableFieldEntity(); |
||
| 313 | $value = $this->driver->value($value, $field); |
||
| 314 | /*if ($value != "" && (!isset($email_fields[$key]) || $email_fields[$key] != "")) { |
||
| 315 | //! filled e-mails can be contained on other pages |
||
| 316 | $email_fields[$key] = ($this->admin->isMail($value) ? $names[$key] : ""); |
||
| 317 | }*/ |
||
| 318 | $length = $this->selectEntity->textLength; |
||
| 319 | $value = $this->admin->selectValue($field, $value, $length); |
||
| 320 | return [ |
||
| 321 | // 'id', |
||
| 322 | 'text' => preg_match('~text|lob~', $field->type), |
||
| 323 | 'value' => $value, |
||
| 324 | // 'editable' => false, |
||
| 325 | ]; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param array $row |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | private function getRowColumns(array $row): array |
||
| 334 | { |
||
| 335 | $cols = []; |
||
| 336 | foreach ($row as $key => $value) { |
||
| 337 | if (isset($this->selectEntity->names[$key])) { |
||
| 338 | $cols[] = $this->getRowColumn($key, $value); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | return $cols; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | private function hasGroupsInFields(): bool |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get required data for create/update on tables |
||
| 355 | * |
||
| 356 | * @param string $table The table name |
||
| 357 | * @param array $queryOptions The query options |
||
| 358 | * |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | public function countSelect(string $table, array $queryOptions): int |
||
| 362 | { |
||
| 363 | $this->setSelectEntity($table, $queryOptions); |
||
| 364 | |||
| 365 | try { |
||
| 366 | $query = $this->driver->getRowCountQuery($table, |
||
| 367 | $this->selectEntity->where, $this->hasGroupsInFields(), |
||
| 368 | $this->selectEntity->group); |
||
| 369 | return (int)$this->driver->result($query); |
||
| 370 | } catch(Exception $_) { |
||
| 371 | return -1; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get required data for create/update on tables |
||
| 377 | * |
||
| 378 | * @param string $table The table name |
||
| 379 | * @param array $queryOptions The query options |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | * @throws Exception |
||
| 383 | */ |
||
| 384 | public function execSelect(string $table, array $queryOptions): array |
||
| 408 | ]; |
||
| 409 | } |
||
| 410 | } |
||
| 411 |
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