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 | 26 | protected function create (...$arguments) { |
|
| 30 | 26 | if (count($arguments) == 1 && !is_array(array_values($this->data_model)[1])) { |
|
| 31 | 20 | $arguments = $arguments[0]; |
|
| 32 | } |
||
| 33 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
| 34 | 26 | $this->db_prime()->transaction( |
|
| 35 | function () use (&$id, $arguments) { |
||
| 36 | $id = $this->create_internal($this->table, $this->data_model, $arguments); |
||
| 37 | 26 | } |
|
| 38 | ); |
||
| 39 | 26 | return $id; |
|
| 40 | } |
||
| 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 | 26 | private function create_internal ($table, $data_model, $arguments) { |
|
| 51 | 26 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
|
| 52 | 26 | $insert_id = count($data_model) == count($arguments) ? $arguments[0] : false; |
|
| 53 | 26 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation( |
|
| 54 | 26 | $insert_id !== false ? $data_model : array_slice($data_model, 1), |
|
| 55 | $arguments, |
||
| 56 | $insert_id, |
||
| 57 | $update_needed |
||
| 58 | ); |
||
| 59 | 26 | $columns = "`".implode("`,`", array_keys($prepared_arguments))."`"; |
|
| 60 | 26 | $values = implode(',', array_fill(0, count($prepared_arguments), "'%s'")); |
|
| 61 | 26 | $return = $this->db_prime()->q( |
|
| 62 | 26 | "INSERT IGNORE INTO `$table` |
|
| 63 | ( |
||
| 64 | 26 | $columns |
|
| 65 | ) VALUES ( |
||
| 66 | 26 | $values |
|
| 67 | 26 | )", |
|
| 68 | $prepared_arguments |
||
| 69 | ); |
||
| 70 | 26 | $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 | 26 | if (!$return || $id === 0) { |
|
| 75 | return false; |
||
| 76 | } |
||
| 77 | 26 | $this->update_joined_tables($id, $joined_tables); |
|
| 78 | 26 | $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 | 26 | if ($update_needed) { |
|
| 84 | 2 | $this->update_internal( |
|
| 85 | $table, |
||
| 86 | array_filter( |
||
| 87 | $data_model, |
||
| 88 | function ($item) { |
||
| 89 | return !is_array($item) || !isset($item['data_model']); |
||
| 90 | 2 | } |
|
| 91 | ), |
||
| 92 | 2 | array_merge([array_keys($data_model)[0] => $id], $prepared_arguments), |
|
| 93 | 2 | false |
|
| 94 | ); |
||
| 95 | } |
||
| 96 | 26 | return $id; |
|
| 97 | } |
||
| 98 | /** |
||
| 99 | * @param int|string $id |
||
| 100 | * @param array $joined_tables |
||
| 101 | */ |
||
| 102 | 28 | private function update_joined_tables ($id, $joined_tables) { |
|
| 151 | /** |
||
| 152 | * Read item |
||
| 153 | * |
||
| 154 | * @param int|int[]|string|string[] $id |
||
| 155 | * |
||
| 156 | * @return array|false |
||
| 157 | */ |
||
| 158 | 16 | protected function read ($id) { |
|
| 167 | /** |
||
| 168 | * Read item |
||
| 169 | * |
||
| 170 | * @param string $table |
||
| 171 | * @param callable[]|string[] $data_model |
||
| 172 | * @param int|int[]|string|string[] $id |
||
| 173 | * |
||
| 174 | * @return array|false |
||
| 175 | */ |
||
| 176 | 24 | private function read_internal ($table, $data_model, $id) { |
|
| 218 | /** |
||
| 219 | * @param false|string|string[] $value |
||
| 220 | * @param string $model |
||
| 221 | * |
||
| 222 | * @return array|false|float|int|string |
||
| 223 | */ |
||
| 224 | 24 | private function read_field_post_processing ($value, $model) { |
|
| 225 | 24 | if (is_callable($model)) { |
|
| 226 | 4 | return $model($value); |
|
| 227 | } |
||
| 228 | /** |
||
| 229 | * Decode JSON fields |
||
| 230 | */ |
||
| 231 | 24 | if (in_array($model, ['json', 'ml:json'])) { |
|
| 232 | 14 | return _json_decode($value); |
|
| 233 | } |
||
| 234 | 24 | if (strpos($model, 'int') === 0) { |
|
| 235 | 20 | return _int($value); |
|
| 236 | } |
||
| 237 | 24 | if (strpos($model, 'float') === 0) { |
|
| 238 | return _float($value); |
||
| 239 | } |
||
| 240 | 24 | return $value; |
|
| 241 | } |
||
| 242 | /** |
||
| 243 | * @param int|string $id |
||
| 244 | * @param string $table |
||
| 245 | * @param array $model |
||
| 246 | * @param null|string $force_clang |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 4 | private function read_joined_table ($id, $table, $model, $force_clang = null) { |
|
| 251 | 4 | $clang = $force_clang ?: $this->db()->s(Language::instance()->clang, false); |
|
| 252 | 4 | $id_field = array_keys($model['data_model'])[0]; |
|
| 253 | 4 | $language_field_condition = isset($model['language_field']) |
|
| 254 | 2 | ? "AND `$model[language_field]` = '$clang'" |
|
| 255 | 4 | : ''; |
|
| 256 | 4 | $fields = '`'.implode('`,`', array_keys($model['data_model'])).'`'; |
|
| 257 | 4 | $rows = $this->db_prime()->qfa( |
|
| 258 | 4 | "SELECT $fields |
|
| 259 | 4 | FROM `{$this->table}_$table` |
|
| 260 | WHERE |
||
| 261 | 4 | `$id_field` = '%s' |
|
| 262 | 4 | $language_field_condition", |
|
| 263 | $id |
||
| 264 | 4 | ) ?: []; |
|
| 265 | 4 | $language_field = isset($model['language_field']) ? $model['language_field'] : null; |
|
| 266 | /** |
||
| 267 | * If no rows found for current language - find another language that should contain some rows |
||
| 268 | */ |
||
| 269 | 4 | if (!$rows && $language_field !== null) { |
|
| 270 | 2 | $new_clang = $this->db_prime()->qfs( |
|
| 271 | 2 | "SELECT `$language_field` |
|
| 272 | 2 | FROM `{$this->table}_$table` |
|
| 273 | 2 | WHERE `$id_field` = '%s' |
|
| 274 | 2 | LIMIT 1", |
|
| 275 | $id |
||
| 276 | ); |
||
| 277 | 2 | if ($new_clang && $new_clang != $clang) { |
|
| 278 | 2 | return $this->read_joined_table($id, $table, $model, $new_clang); |
|
| 279 | } |
||
| 280 | return []; |
||
| 281 | } |
||
| 282 | 4 | foreach ($rows as &$row) { |
|
| 283 | /** |
||
| 284 | * Drop language and id field since they are used internally, not specified by user |
||
| 285 | */ |
||
| 286 | unset( |
||
| 287 | 4 | $row[$language_field], |
|
| 288 | 4 | $row[$id_field] |
|
| 289 | ); |
||
| 290 | 4 | foreach ($row as $field => &$value) { |
|
| 291 | 4 | $value = $this->read_field_post_processing($value, $model['data_model'][$field]); |
|
| 292 | } |
||
| 293 | /** |
||
| 294 | * If row is array that contains only one item - lets make resulting array flat |
||
| 295 | */ |
||
| 296 | 4 | if (count($row) == 1) { |
|
| 297 | 4 | $row = array_pop($row); |
|
| 298 | } |
||
| 299 | } |
||
| 300 | 4 | return $rows; |
|
| 301 | } |
||
| 302 | /** |
||
| 303 | * Update item |
||
| 304 | * |
||
| 305 | * @param array $arguments |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | 10 | protected function update (...$arguments) { |
|
| 321 | /** |
||
| 322 | * Update item |
||
| 323 | * |
||
| 324 | * @param string $table |
||
| 325 | * @param callable[]|string[] $data_model |
||
| 326 | * @param array $arguments |
||
| 327 | * @param bool $files_update |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 10 | private function update_internal ($table, $data_model, $arguments, $files_update = true) { |
|
| 332 | 10 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
|
| 333 | 10 | $prepared_arguments = $arguments; |
|
| 334 | 10 | $id = array_shift($prepared_arguments); |
|
| 335 | 10 | $data = $this->read_internal($table, $data_model, $id); |
|
| 336 | 10 | if (!$data) { |
|
| 337 | 2 | return false; |
|
| 338 | } |
||
| 339 | 10 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation(array_slice($data_model, 1), $prepared_arguments, $id); |
|
| 340 | 10 | $columns = implode( |
|
| 341 | 10 | ',', |
|
| 342 | array_map( |
||
| 343 | function ($column) { |
||
| 344 | return "`$column` = '%s'"; |
||
| 345 | 10 | }, |
|
| 346 | array_keys($prepared_arguments) |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | 10 | $prepared_arguments[] = $id; |
|
| 350 | 10 | $first_column = array_keys($data_model)[0]; |
|
| 351 | 10 | if (!$this->db_prime()->q( |
|
| 352 | 10 | "UPDATE `$table` |
|
| 353 | 10 | SET $columns |
|
| 354 | 10 | WHERE `$first_column` = '%s'", |
|
| 355 | $prepared_arguments |
||
| 356 | ) |
||
| 357 | ) { |
||
| 358 | return false; |
||
| 359 | } |
||
| 360 | 10 | if ($files_update) { |
|
| 361 | 10 | $this->update_joined_tables($id, $joined_tables); |
|
| 362 | 10 | $this->find_update_files_tags($id, $data, $arguments); |
|
| 363 | } |
||
| 364 | 10 | return true; |
|
| 365 | } |
||
| 366 | /** |
||
| 367 | * Delete item |
||
| 368 | * |
||
| 369 | * @param int|int[]|string|string[] $id |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | 12 | protected function delete ($id) { |
|
| 382 | /** |
||
| 383 | * Delete item |
||
| 384 | * |
||
| 385 | * @param string $table |
||
| 386 | * @param callable[]|string[] $data_model |
||
| 387 | * @param int|int[]|string|string[] $id |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | 12 | private function delete_internal ($table, $data_model, $id) { |
|
| 420 | } |
||
| 421 |