Complex classes like CRUD 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CRUD, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | trait CRUD { |
||
| 19 | use |
||
| 20 | Accessor, |
||
| 21 | Data_model_processing; |
||
| 22 | /** |
||
| 23 | * Create item |
||
| 24 | * |
||
| 25 | * @param array $arguments First element `id` can be omitted if it is autoincrement field |
||
| 26 | * |
||
| 27 | * @return false|int|string Id of created item on success, `false` otherwise |
||
| 28 | */ |
||
| 29 | 44 | protected function create (...$arguments) { |
|
| 41 | /** |
||
| 42 | * Create item |
||
| 43 | * |
||
| 44 | * @param string $table |
||
| 45 | * @param callable[]|string[] $data_model |
||
| 46 | * @param array $arguments First element `id` can be omitted if it is autoincrement field |
||
| 47 | * |
||
| 48 | * @return false|int|string Id of created item on success (or specified primary key), `false` otherwise |
||
| 49 | */ |
||
| 50 | 44 | private function create_internal ($table, $data_model, $arguments) { |
|
| 51 | 44 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
|
| 52 | 44 | $insert_id = count($data_model) == count($arguments) ? $arguments[0] : false; |
|
| 53 | 44 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation( |
|
| 54 | 44 | $insert_id !== false ? $data_model : array_slice($data_model, 1), |
|
| 55 | $arguments, |
||
| 56 | $insert_id, |
||
| 57 | $update_needed |
||
| 58 | ); |
||
| 59 | 44 | $columns = '`'.implode('`,`', array_keys($prepared_arguments)).'`'; |
|
| 60 | 44 | $values = implode(',', array_fill(0, count($prepared_arguments), '?')); |
|
| 61 | 44 | $return = $this->db_prime()->q( |
|
| 62 | 44 | "INSERT IGNORE INTO `$table` |
|
| 63 | ( |
||
| 64 | 44 | $columns |
|
| 65 | ) VALUES ( |
||
| 66 | 44 | $values |
|
| 67 | )", |
||
| 68 | $prepared_arguments |
||
| 69 | ); |
||
| 70 | 44 | $id = $insert_id !== false ? $insert_id : $this->db_prime()->id(); |
|
| 71 | /** |
||
| 72 | * Id might be 0 if insertion failed or if we insert duplicate entry (which is fine since we use 'INSERT IGNORE' |
||
| 73 | */ |
||
| 74 | 44 | if (!$return || $id === 0) { |
|
| 75 | return false; |
||
| 76 | } |
||
| 77 | 44 | $this->update_joined_tables($id, $joined_tables); |
|
| 78 | 44 | $this->find_update_files_tags($id, [], $arguments); |
|
| 79 | /** |
||
| 80 | * If on creation request without specified primary key and multilingual fields present - update needed |
||
| 81 | * after creation (there is no id before creation) |
||
| 82 | */ |
||
| 83 | 44 | if ($update_needed) { |
|
| 84 | 4 | $this->update_internal( |
|
| 85 | $table, |
||
| 86 | 4 | array_filter($data_model, [$this, 'format_without_data_model']), |
|
| 87 | 4 | array_merge([array_keys($data_model)[0] => $id], $prepared_arguments), |
|
| 88 | 4 | false |
|
| 89 | ); |
||
| 90 | } |
||
| 91 | 44 | return $id; |
|
| 92 | } |
||
| 93 | /** |
||
| 94 | * @param mixed $format |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | 52 | private function format_without_data_model ($format) { |
|
| 101 | /** |
||
| 102 | * @param mixed $format |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | 52 | private function format_with_data_model ($format) { |
|
| 109 | /** |
||
| 110 | * @param int|string $id |
||
| 111 | * @param array $joined_tables |
||
| 112 | */ |
||
| 113 | 48 | private function update_joined_tables ($id, $joined_tables) { |
|
| 114 | 48 | $clang = $this->db_prime()->s(Language::instance()->clang, false); |
|
| 115 | /** |
||
| 116 | * At first we remove all old data |
||
| 117 | */ |
||
| 118 | 48 | foreach ($this->data_model as $table => $model) { |
|
| 119 | 48 | if ($this->format_without_data_model($model)) { |
|
| 120 | 48 | continue; |
|
| 121 | } |
||
| 122 | 8 | $id_field = array_keys($model['data_model'])[0]; |
|
| 123 | 8 | $language_field_condition = isset($model['language_field']) |
|
| 124 | 4 | ? "AND `$model[language_field]` = '$clang'" |
|
| 125 | 8 | : ''; |
|
| 126 | 8 | $this->db_prime()->q( |
|
| 127 | 8 | "DELETE FROM `{$this->table}_$table` |
|
| 128 | WHERE |
||
| 129 | 8 | `$id_field` = ? |
|
| 130 | 8 | $language_field_condition", |
|
| 131 | $id |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | 48 | $id = $this->db_prime()->s($id, false); |
|
| 135 | /** |
||
| 136 | * Now walk through all tables and insert new valued |
||
| 137 | */ |
||
| 138 | 48 | foreach ($joined_tables as $table => $model) { |
|
| 139 | 8 | if (!@$model['data']) { |
|
| 140 | 6 | continue; |
|
| 141 | } |
||
| 142 | 8 | $fields = "`$model[id_field]`, "; |
|
| 143 | 8 | $values = "'$id'"; |
|
| 144 | 8 | if (isset($model['language_field'])) { |
|
| 145 | 4 | $fields .= "`$model[language_field]`, "; |
|
| 146 | 4 | $values .= ",'$clang'"; |
|
| 147 | } |
||
| 148 | 8 | $fields .= '`'.implode('`,`', array_keys($model['fields'])).'`'; |
|
| 149 | 8 | $values .= str_repeat(',?', count($model['fields'])); |
|
| 150 | 8 | $this->db_prime()->insert( |
|
| 151 | 8 | "INSERT INTO `{$this->table}_$table` |
|
| 152 | ( |
||
| 153 | 8 | $fields |
|
| 154 | ) VALUES ( |
||
| 155 | 8 | $values |
|
| 156 | )", |
||
| 157 | 8 | $model['data'] |
|
| 158 | ); |
||
| 159 | } |
||
| 160 | 48 | } |
|
| 161 | /** |
||
| 162 | * Read item |
||
| 163 | * |
||
| 164 | * @param int|int[]|string|string[] $id |
||
| 165 | * |
||
| 166 | * @return array|false |
||
| 167 | */ |
||
| 168 | 34 | protected function read ($id) { |
|
| 177 | /** |
||
| 178 | * Read item |
||
| 179 | * |
||
| 180 | * @param string $table |
||
| 181 | * @param callable[]|string[] $data_model |
||
| 182 | * @param int|int[]|string|string[] $id |
||
| 183 | * |
||
| 184 | * @return array|false |
||
| 185 | */ |
||
| 186 | 46 | private function read_internal ($table, $data_model, $id) { |
|
| 187 | 46 | if (is_array($id)) { |
|
| 188 | 8 | foreach ($id as &$i) { |
|
| 189 | 8 | $i = $this->read_internal($table, $data_model, $i); |
|
| 190 | } |
||
| 191 | 8 | return $id; |
|
| 192 | } |
||
| 193 | 46 | $columns = array_filter($data_model, [$this, 'format_without_data_model']); |
|
| 194 | 46 | $columns = '`'.implode('`,`', array_keys($columns)).'`'; |
|
| 195 | 46 | $first_column = array_keys($data_model)[0]; |
|
| 196 | 46 | $data = $this->db()->qf( |
|
| 197 | 46 | "SELECT $columns |
|
| 198 | 46 | FROM `$table` |
|
| 199 | 46 | WHERE `$first_column` = ? |
|
| 200 | LIMIT 1", |
||
| 201 | $id |
||
| 202 | ); |
||
| 203 | 46 | if (!$data) { |
|
| 204 | 10 | return false; |
|
| 205 | } |
||
| 206 | 46 | foreach ($this->data_model as $field => $model) { |
|
| 207 | 46 | if ($this->format_with_data_model($model)) { |
|
| 208 | 6 | $data[$field] = $this->read_joined_table($id, $field, $model); |
|
| 209 | } else { |
||
| 210 | 46 | if (is_string($model)) { |
|
| 211 | /** |
||
| 212 | * Handle multilingual fields automatically |
||
| 213 | */ |
||
| 214 | 46 | if (strpos($model, 'ml:') === 0) { |
|
| 215 | 4 | $data[$field] = Text::instance()->process($this->cdb(), $data[$field], true); |
|
| 216 | } |
||
| 217 | } |
||
| 218 | 46 | $data[$field] = $this->read_field_post_processing($data[$field], $model); |
|
| 219 | } |
||
| 220 | } |
||
| 221 | 46 | return $data; |
|
| 222 | } |
||
| 223 | /** |
||
| 224 | * @param false|string|string[] $value |
||
| 225 | * @param callable|string $model |
||
| 226 | * |
||
| 227 | * @return array|false|float|int|string |
||
| 228 | */ |
||
| 229 | 48 | private function read_field_post_processing ($value, $model) { |
|
| 253 | /** |
||
| 254 | * @param int|string $id |
||
| 255 | * @param string $table |
||
| 256 | * @param array $model |
||
| 257 | * @param null|string $force_clang |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 6 | private function read_joined_table ($id, $table, $model, $force_clang = null) { |
|
| 262 | 6 | $clang = $force_clang ?: $this->db()->s(Language::instance()->clang, false); |
|
| 263 | 6 | $id_field = array_keys($model['data_model'])[0]; |
|
| 264 | 6 | $language_field_condition = isset($model['language_field']) |
|
| 265 | 4 | ? "AND `$model[language_field]` = '$clang'" |
|
| 266 | 6 | : ''; |
|
| 267 | 6 | $fields = '`'.implode('`,`', array_keys($model['data_model'])).'`'; |
|
| 268 | 6 | $rows = $this->db_prime()->qfa( |
|
| 269 | 6 | "SELECT $fields |
|
| 270 | 6 | FROM `{$this->table}_$table` |
|
| 271 | WHERE |
||
| 272 | 6 | `$id_field` = ? |
|
| 273 | 6 | $language_field_condition", |
|
| 274 | $id |
||
| 275 | 6 | ) ?: []; |
|
| 276 | 6 | $language_field = isset($model['language_field']) ? $model['language_field'] : null; |
|
| 277 | /** |
||
| 278 | * If no rows found for current language - find another language that should contain some rows |
||
| 279 | */ |
||
| 280 | 6 | if (!$rows && $language_field !== null) { |
|
| 281 | 4 | $new_clang = $this->db_prime()->qfs( |
|
| 282 | 4 | "SELECT `$language_field` |
|
| 283 | 4 | FROM `{$this->table}_$table` |
|
| 284 | 4 | WHERE `$id_field` = ? |
|
| 285 | LIMIT 1", |
||
| 286 | $id |
||
| 287 | ); |
||
| 288 | 4 | if ($new_clang && $new_clang != $clang) { |
|
| 289 | 4 | return $this->read_joined_table($id, $table, $model, $new_clang); |
|
| 290 | } |
||
| 291 | return []; |
||
| 292 | } |
||
| 293 | 6 | foreach ($rows as &$row) { |
|
| 294 | /** |
||
| 295 | * Drop language and id field since they are used internally, not specified by user |
||
| 296 | */ |
||
| 297 | unset( |
||
| 298 | 6 | $row[$language_field], |
|
| 299 | 6 | $row[$id_field] |
|
| 300 | ); |
||
| 301 | 6 | foreach ($row as $field => &$value) { |
|
| 302 | 6 | $value = $this->read_field_post_processing($value, $model['data_model'][$field]); |
|
| 303 | } |
||
| 304 | /** |
||
| 305 | * If row is array that contains only one item - lets make resulting array flat |
||
| 306 | */ |
||
| 307 | 6 | if (count($row) == 1) { |
|
| 308 | 6 | $row = array_pop($row); |
|
| 309 | } |
||
| 310 | } |
||
| 311 | 6 | return $rows; |
|
| 312 | } |
||
| 313 | /** |
||
| 314 | * Update item |
||
| 315 | * |
||
| 316 | * @param array $arguments |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 18 | protected function update (...$arguments) { |
|
| 332 | /** |
||
| 333 | * Update item |
||
| 334 | * |
||
| 335 | * @param string $table |
||
| 336 | * @param callable[]|string[] $data_model |
||
| 337 | * @param array $arguments |
||
| 338 | * @param bool $files_update |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 18 | private function update_internal ($table, $data_model, $arguments, $files_update = true) { |
|
| 377 | /** |
||
| 378 | * Delete item |
||
| 379 | * |
||
| 380 | * @param int|int[]|string|string[] $id |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | 20 | protected function delete ($id) { |
|
| 393 | /** |
||
| 394 | * Delete item |
||
| 395 | * |
||
| 396 | * @param string $table |
||
| 397 | * @param callable[]|string[] $data_model |
||
| 398 | * @param int|int[]|string|string[] $id |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | 20 | private function delete_internal ($table, $data_model, $id) { |
|
| 431 | } |
||
| 432 |