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 | 10 | 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 | 10 | private function create_internal ($table, $data_model, $arguments) { |
|
51 | 10 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
|
52 | 10 | $insert_id = count($data_model) == count($arguments) ? $arguments[0] : false; |
|
53 | 10 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation( |
|
54 | 10 | $insert_id !== false ? $data_model : array_slice($data_model, 1), |
|
55 | $arguments, |
||
56 | $insert_id, |
||
57 | $update_needed |
||
58 | ); |
||
59 | 10 | $columns = "`".implode("`,`", array_keys($prepared_arguments))."`"; |
|
60 | 10 | $values = implode(',', array_fill(0, count($prepared_arguments), "'%s'")); |
|
61 | 10 | $return = $this->db_prime()->q( |
|
62 | 10 | "INSERT IGNORE INTO `$table` |
|
63 | ( |
||
64 | 10 | $columns |
|
65 | ) VALUES ( |
||
66 | 10 | $values |
|
67 | 10 | )", |
|
68 | $prepared_arguments |
||
69 | ); |
||
70 | 10 | $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 | 10 | if (!$return || $id === 0) { |
|
75 | return false; |
||
76 | } |
||
77 | 10 | $this->update_joined_tables($id, $joined_tables); |
|
78 | 10 | $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 | 10 | 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 | 10 | return $id; |
|
97 | } |
||
98 | /** |
||
99 | * @param int|string $id |
||
100 | * @param array $joined_tables |
||
101 | */ |
||
102 | 10 | 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 | 12 | 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 | 12 | 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 | 12 | private function read_field_post_processing ($value, $model) { |
|
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) { |
|
302 | /** |
||
303 | * Update item |
||
304 | * |
||
305 | * @param array $arguments |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 8 | 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 | 8 | private function update_internal ($table, $data_model, $arguments, $files_update = true) { |
|
366 | /** |
||
367 | * Delete item |
||
368 | * |
||
369 | * @param int|int[]|string|string[] $id |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | 4 | 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 | 4 | private function delete_internal ($table, $data_model, $id) { |
|
420 | } |
||
421 |