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 | 34 | protected function create (...$arguments) { |
|
30 | 34 | if (count($arguments) == 1 && !is_array(array_values($this->data_model)[1])) { |
|
31 | 26 | $arguments = $arguments[0]; |
|
32 | } |
||
33 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
34 | 34 | $this->db_prime()->transaction( |
|
35 | function () use (&$id, $arguments) { |
||
36 | 34 | $id = $this->create_internal($this->table, $this->data_model, $arguments); |
|
37 | 34 | } |
|
38 | ); |
||
39 | 34 | 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 | 34 | private function create_internal ($table, $data_model, $arguments) { |
|
51 | 34 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
|
52 | 34 | $insert_id = count($data_model) == count($arguments) ? $arguments[0] : false; |
|
53 | 34 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation( |
|
54 | 34 | $insert_id !== false ? $data_model : array_slice($data_model, 1), |
|
55 | $arguments, |
||
56 | $insert_id, |
||
57 | $update_needed |
||
58 | ); |
||
59 | 34 | $columns = '`'.implode('`,`', array_keys($prepared_arguments)).'`'; |
|
60 | 34 | $values = implode(',', array_fill(0, count($prepared_arguments), "'%s'")); |
|
61 | 34 | $return = $this->db_prime()->q( |
|
62 | 34 | "INSERT IGNORE INTO `$table` |
|
63 | ( |
||
64 | 34 | $columns |
|
65 | ) VALUES ( |
||
66 | 34 | $values |
|
67 | 34 | )", |
|
68 | $prepared_arguments |
||
69 | ); |
||
70 | 34 | $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 | 34 | if (!$return || $id === 0) { |
|
75 | return false; |
||
76 | } |
||
77 | 34 | $this->update_joined_tables($id, $joined_tables); |
|
78 | 34 | $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 | 34 | if ($update_needed) { |
|
84 | 4 | $this->update_internal( |
|
85 | $table, |
||
86 | array_filter( |
||
87 | $data_model, |
||
88 | function ($item) { |
||
89 | 4 | return !is_array($item) || !isset($item['data_model']); |
|
90 | 4 | } |
|
91 | ), |
||
92 | 4 | array_merge([array_keys($data_model)[0] => $id], $prepared_arguments), |
|
93 | 4 | false |
|
94 | ); |
||
95 | } |
||
96 | 34 | return $id; |
|
97 | } |
||
98 | /** |
||
99 | * @param int|string $id |
||
100 | * @param array $joined_tables |
||
101 | */ |
||
102 | 36 | 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 | 20 | 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 | 30 | private function read_internal ($table, $data_model, $id) { |
|
218 | /** |
||
219 | * @param false|string|string[] $value |
||
220 | * @param callable|string $model |
||
221 | * |
||
222 | * @return array|false|float|int|string |
||
223 | */ |
||
224 | 32 | private function read_field_post_processing ($value, $model) { |
|
248 | /** |
||
249 | * @param int|string $id |
||
250 | * @param string $table |
||
251 | * @param array $model |
||
252 | * @param null|string $force_clang |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 6 | private function read_joined_table ($id, $table, $model, $force_clang = null) { |
|
308 | /** |
||
309 | * Update item |
||
310 | * |
||
311 | * @param array $arguments |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | 16 | protected function update (...$arguments) { |
|
327 | /** |
||
328 | * Update item |
||
329 | * |
||
330 | * @param string $table |
||
331 | * @param callable[]|string[] $data_model |
||
332 | * @param array $arguments |
||
333 | * @param bool $files_update |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 16 | private function update_internal ($table, $data_model, $arguments, $files_update = true) { |
|
372 | /** |
||
373 | * Delete item |
||
374 | * |
||
375 | * @param int|int[]|string|string[] $id |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | 14 | protected function delete ($id) { |
|
388 | /** |
||
389 | * Delete item |
||
390 | * |
||
391 | * @param string $table |
||
392 | * @param callable[]|string[] $data_model |
||
393 | * @param int|int[]|string|string[] $id |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | 14 | private function delete_internal ($table, $data_model, $id) { |
|
426 | } |
||
427 |