| Total Complexity | 41 |
| Total Lines | 146 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like QueryTrait 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 QueryTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait QueryTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Get the table fields |
||
| 16 | * |
||
| 17 | * @param string $table The table name |
||
| 18 | * @param array $queryOptions The query options |
||
| 19 | * |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | private function getFields(string $table, array $queryOptions): array |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array $fields |
||
| 44 | * @param array $queryOptions |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | private function getQuerySelect(array $fields, array $queryOptions): array |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $table |
||
| 71 | * @param string $where |
||
| 72 | * @param array $fields |
||
| 73 | * @param array $queryOptions |
||
| 74 | * |
||
| 75 | * @return array|null |
||
| 76 | */ |
||
| 77 | private function getQueryFirstRow(string $table, string $where, array $fields, array $queryOptions): ?array |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param TableFieldEntity $field |
||
| 104 | * @param string $name |
||
| 105 | * @param array|null $row |
||
| 106 | * @param string $update |
||
| 107 | * @param array $queryOptions |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | private function getRowFieldValue(TableFieldEntity $field, string $name, ?array $row, string $update, array $queryOptions) |
||
| 112 | { |
||
| 113 | // $default = $queryOptions["set"][$this->driver->bracketEscape($name)] ?? null; |
||
| 114 | // if($default === null) |
||
| 115 | // { |
||
| 116 | $default = $field->default; |
||
| 117 | if ($field->type == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) { |
||
| 118 | $default = $regs[1]; |
||
| 119 | } |
||
| 120 | // } |
||
| 121 | // Todo: use match |
||
| 122 | if ($row === null) { |
||
|
|
|||
| 123 | return !$update && $field->autoIncrement ? "" : (isset($queryOptions["select"]) ? false : $default); |
||
| 124 | } |
||
| 125 | if ($row[$name] != "" && $this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type) ) { |
||
| 126 | return is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name]; |
||
| 127 | } |
||
| 128 | return is_bool($row[$name]) ? +$row[$name] : $row[$name]; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param TableFieldEntity $field |
||
| 133 | * @param string $name |
||
| 134 | * @param mixed $value |
||
| 135 | * @param string $update |
||
| 136 | * @param array $queryOptions |
||
| 137 | * |
||
| 138 | * @return string|null |
||
| 139 | */ |
||
| 140 | private function getRowFieldFunction(TableFieldEntity $field, string $name, $value, string $update, array $queryOptions): ?string |
||
| 158 | } |
||
| 159 | } |
||
| 160 |