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 | protected function create ($arguments) { |
||
32 | /** |
||
33 | * Create item |
||
34 | * |
||
35 | * @param string $table |
||
36 | * @param callable[]|string[] $data_model |
||
37 | * @param array $arguments First element `id` can be omitted if it is autoincrement field |
||
38 | * |
||
39 | * @return false|int|string Id of created item on success (or specified primary key), `false` otherwise |
||
40 | */ |
||
41 | private function create_internal ($table, $data_model, $arguments) { |
||
42 | $arguments = $this->fix_arguments_order($data_model, $arguments); |
||
43 | $insert_id = count($data_model) == count($arguments) ? $arguments[0] : false; |
||
44 | list($prepared_arguments, $joined_tables) = $this->crud_arguments_preparation( |
||
45 | $insert_id !== false ? $data_model : array_slice($data_model, 1), |
||
46 | $arguments, |
||
47 | $insert_id, |
||
48 | $update_needed |
||
49 | ); |
||
50 | $columns = "`".implode("`,`", array_keys($prepared_arguments))."`"; |
||
51 | $values = implode(',', array_fill(0, count($prepared_arguments), "'%s'")); |
||
52 | $return = $this->db_prime()->q( |
||
53 | "INSERT IGNORE INTO `$table` |
||
54 | ( |
||
55 | $columns |
||
56 | ) VALUES ( |
||
57 | $values |
||
58 | )", |
||
59 | $prepared_arguments |
||
60 | ); |
||
61 | $id = $insert_id !== false ? $insert_id : $this->db_prime()->id(); |
||
62 | /** |
||
63 | * Id might be 0 if insertion failed or if we insert duplicate entry (which is fine since we use 'INSERT IGNORE' |
||
64 | */ |
||
65 | if (!$return || $id === 0) { |
||
66 | return false; |
||
67 | } |
||
68 | $this->update_joined_tables($id, $joined_tables); |
||
69 | $this->find_update_files_tags($id, [], $arguments); |
||
70 | /** |
||
71 | * If on creation request without specified primary key and multilingual fields present - update needed |
||
72 | * after creation (there is no id before creation) |
||
73 | */ |
||
74 | if ($update_needed) { |
||
75 | $this->update_internal($table, $data_model, array_merge([array_keys($data_model)[0] => $id], $prepared_arguments), false); |
||
76 | } |
||
77 | return $id; |
||
78 | } |
||
79 | /** |
||
80 | * @param int|string $id |
||
81 | * @param array $joined_tables |
||
82 | */ |
||
83 | private function update_joined_tables ($id, $joined_tables) { |
||
84 | $clang = $this->db_prime()->s(Language::instance()->clang, false); |
||
85 | /** |
||
86 | * At first we remove all old data |
||
87 | */ |
||
88 | foreach ($this->data_model as $table => $model) { |
||
89 | if (!is_array($model) || !isset($model['data_model'])) { |
||
90 | continue; |
||
91 | } |
||
92 | $id_field = array_keys($model['data_model'])[0]; |
||
93 | $language_field_condition = isset($model['language_field']) |
||
94 | ? "AND `$model[language_field]` = '$clang'" |
||
95 | : ''; |
||
96 | $this->db_prime()->q( |
||
97 | "DELETE FROM `{$this->table}_$table` |
||
98 | WHERE |
||
99 | `$id_field` = '%s' |
||
100 | $language_field_condition", |
||
101 | $id |
||
102 | ); |
||
103 | } |
||
104 | $id = $this->db_prime()->s($id, false); |
||
105 | /** |
||
106 | * Now walk through all tables and insert new valued |
||
107 | */ |
||
108 | foreach ($joined_tables as $table => $model) { |
||
109 | if (!@$model['data']) { |
||
110 | continue; |
||
111 | } |
||
112 | $fields = "`$model[id_field]`, "; |
||
113 | /** @noinspection DisconnectedForeachInstructionInspection */ |
||
114 | $values = "'$id'"; |
||
115 | if (isset($model['language_field'])) { |
||
116 | $fields .= "`$model[language_field]`, "; |
||
117 | $values .= ",'$clang'"; |
||
118 | } |
||
119 | $fields .= '`'.implode('`,`', array_keys($model['fields'])).'`'; |
||
120 | $values .= str_repeat(",'%s'", count($model['fields'])); |
||
121 | $this->db_prime()->insert( |
||
122 | "INSERT INTO `{$this->table}_$table` |
||
123 | ( |
||
124 | $fields |
||
125 | ) VALUES ( |
||
126 | $values |
||
127 | )", |
||
128 | $model['data'] |
||
129 | ); |
||
130 | } |
||
131 | } |
||
132 | /** |
||
133 | * Read item |
||
134 | * |
||
135 | * @param int|int[]|string|string[] $id |
||
136 | * |
||
137 | * @return array|false |
||
1 ignored issue
–
show
|
|||
138 | */ |
||
139 | protected function read ($id) { |
||
142 | /** |
||
143 | * Read item |
||
144 | * |
||
145 | * @param string $table |
||
146 | * @param callable[]|string[] $data_model |
||
147 | * @param int|int[]|string|string[] $id |
||
148 | * |
||
149 | * @return array|false |
||
1 ignored issue
–
show
|
|||
150 | */ |
||
151 | private function read_internal ($table, $data_model, $id) { |
||
191 | /** |
||
192 | * @param mixed $value |
||
193 | * @param string $model |
||
194 | * |
||
195 | * @return mixed |
||
196 | */ |
||
197 | private function read_field_post_processing ($value, $model) { |
||
212 | /** |
||
213 | * @param int|string $id |
||
214 | * @param string $table |
||
215 | * @param array $model |
||
216 | * @param null|string $force_clang |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | private function read_joined_table ($id, $table, $model, $force_clang = null) { |
||
274 | /** |
||
275 | * Update item |
||
276 | * |
||
277 | * @param array $arguments |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function update ($arguments) { |
||
284 | /** |
||
285 | * Update item |
||
286 | * |
||
287 | * @param string $table |
||
288 | * @param callable[]|string[] $data_model |
||
289 | * @param array $arguments |
||
290 | * @param bool $files_update |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | private function update_internal ($table, $data_model, $arguments, $files_update = true) { |
||
330 | /** |
||
331 | * Delete item |
||
332 | * |
||
333 | * @param int|int[]|string|string[] $id |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function delete ($id) { |
||
340 | /** |
||
341 | * Delete item |
||
342 | * |
||
343 | * @param string $table |
||
344 | * @param callable[]|string[] $data_model |
||
345 | * @param int|int[]|string|string[] $id |
||
346 | * |
||
347 | * @return bool |
||
348 | */ |
||
349 | private function delete_internal ($table, $data_model, $id) { |
||
379 | } |
||
380 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.