Complex classes like Items 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 Items, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Items { |
||
50 | use CRUD_helpers { |
||
51 | search as crud_search; |
||
52 | } |
||
53 | use |
||
54 | Singleton; |
||
55 | |||
56 | const DEFAULT_IMAGE = 'components/modules/Shop/includes/img/no-image.svg'; |
||
57 | |||
58 | protected $data_model = [ |
||
59 | 'id' => 'int:0', |
||
60 | 'date' => 'int:0', |
||
61 | 'category' => 'int:0', |
||
62 | 'price' => 'float', |
||
63 | 'in_stock' => 'int:0', |
||
64 | 'soon' => 'int:0..1', |
||
65 | 'listed' => 'int:0..1', |
||
66 | 'attributes' => [ |
||
67 | 'data_model' => [ |
||
68 | 'id' => 'int:0', |
||
69 | 'attribute' => 'int:0', |
||
70 | 'numeric_value' => 'float', |
||
71 | 'string_value' => 'text', |
||
72 | 'text_value' => 'html', |
||
73 | 'lang' => 'text' // Some attributes are language-dependent, some aren't, so we'll handle that manually |
||
74 | ] |
||
75 | ], |
||
76 | 'images' => [ |
||
77 | 'data_model' => [ |
||
78 | 'id' => 'int:0', |
||
79 | 'image' => 'text' |
||
80 | ] |
||
81 | ], |
||
82 | 'videos' => [ |
||
83 | 'data_model' => [ |
||
84 | 'id' => 'int:0', |
||
85 | 'video' => 'text', |
||
86 | 'poster' => 'text', |
||
87 | 'type' => 'text' |
||
88 | ] |
||
89 | ], |
||
90 | 'tags' => [ |
||
91 | 'data_model' => [ |
||
92 | 'id' => 'int:0', |
||
93 | 'tag' => 'int:0' |
||
94 | ], |
||
95 | 'language_field' => 'lang' |
||
96 | ] |
||
97 | ]; |
||
98 | protected $table = '[prefix]shop_items'; |
||
99 | protected $data_model_files_tag_prefix = 'Shop/items'; |
||
100 | /** |
||
101 | * @var Prefix |
||
102 | */ |
||
103 | protected $cache; |
||
104 | |||
105 | protected function construct () { |
||
108 | /** |
||
109 | * Returns database index |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | protected function cdb () { |
||
116 | /** |
||
117 | * Get item |
||
118 | * |
||
119 | * @param int|int[] $id |
||
120 | * |
||
121 | * @return array|false |
||
122 | */ |
||
123 | function get ($id) { |
||
124 | if (is_array($id)) { |
||
125 | foreach ($id as &$i) { |
||
126 | $i = $this->get($i); |
||
127 | } |
||
128 | return $id; |
||
129 | } |
||
130 | $L = Language::instance(); |
||
131 | $id = (int)$id; |
||
132 | $data = $this->cache->get( |
||
133 | "$id/$L->clang", |
||
134 | function () use ($id, $L) { |
||
135 | $data = $this->read($id); |
||
136 | if (!$data) { |
||
137 | return false; |
||
138 | } |
||
139 | $data['attributes'] = $this->read_attributes_processing($data['attributes'], $L->clang); |
||
140 | $category = Categories::instance()->get($data['category']); |
||
141 | $data['title'] = $data['attributes'][$category['title_attribute']]; |
||
142 | $data['description'] = @$data['attributes'][$category['description_attribute']] ?: ''; |
||
143 | $data['tags'] = $this->read_tags_processing($data['tags']); |
||
144 | return $data; |
||
145 | } |
||
146 | ); |
||
147 | if (!Event::instance()->fire( |
||
148 | 'Shop/Items/get', |
||
149 | [ |
||
150 | 'data' => &$data |
||
151 | ] |
||
152 | ) |
||
153 | ) { |
||
154 | return false; |
||
155 | } |
||
156 | return $data; |
||
157 | } |
||
158 | /** |
||
159 | * Transform normalized attributes structure back into simple initial structure |
||
160 | * |
||
161 | * @param array $attributes |
||
162 | * @param string $clang |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | protected function read_attributes_processing ($attributes, $clang) { |
||
167 | /** |
||
168 | * Select language-independent attributes and ones that are set for current language |
||
169 | */ |
||
170 | $filtered_attributes = array_filter( |
||
171 | $attributes, |
||
172 | function ($attribute) use ($clang) { |
||
173 | return !$attribute['lang'] || $attribute['lang'] == $clang; |
||
174 | } |
||
175 | ); |
||
176 | $existing_attributes = array_column($filtered_attributes, 'attribute'); |
||
177 | /** |
||
178 | * Now fill other existing attributes that are missing for current language |
||
179 | */ |
||
180 | foreach ($attributes as $attribute) { |
||
181 | if (!in_array($attribute['attribute'], $existing_attributes)) { |
||
182 | $existing_attributes[] = $attribute['attribute']; |
||
183 | $filtered_attributes[] = $attribute; |
||
184 | } |
||
185 | } |
||
186 | /** |
||
187 | * We have attributes of different types, so, here is normalization for that |
||
188 | */ |
||
189 | $Attributes = Attributes::instance(); |
||
190 | foreach ($filtered_attributes as &$value) { |
||
191 | $attribute = $Attributes->get($value['attribute']); |
||
192 | if ($attribute) { |
||
193 | $value['value'] = $value[$this->attribute_type_to_value_field($attribute['type'])]; |
||
194 | } else { |
||
195 | $value['value'] = $value['text_value']; |
||
196 | if (!strlen($value['value'])) { |
||
197 | $value['value'] = $value['string_value']; |
||
198 | } |
||
199 | if (!strlen($value['value'])) { |
||
200 | $value['value'] = $value['numeric_value']; |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | return array_column($filtered_attributes, 'value', 'attribute'); |
||
205 | } |
||
206 | /** |
||
207 | * Transform tags ids back into array of strings |
||
208 | * |
||
209 | * @param int[] $tags |
||
210 | * |
||
211 | * @return string[] |
||
212 | */ |
||
213 | protected function read_tags_processing ($tags) { |
||
216 | /** |
||
217 | * Get item data for specific user (price might be adjusted, some items may be restricted and so on) |
||
218 | * |
||
219 | * @param int|int[] $id |
||
220 | * @param bool|int $user |
||
221 | * |
||
222 | * @return array|false |
||
223 | */ |
||
224 | function get_for_user ($id, $user = false) { |
||
248 | /** |
||
249 | * Get array of all items |
||
250 | * |
||
251 | * @return int[] Array of items ids |
||
252 | */ |
||
253 | function get_all () { |
||
261 | /** |
||
262 | * Items search |
||
263 | * |
||
264 | * @param mixed[] $search_parameters Array in form [attribute => value], [attribute => [value, value]], [attribute => [from => value, to => value]], |
||
265 | * [property => value], [tag] or mixed; if `total_count => 1` element is present - total number of found rows will be |
||
266 | * returned instead of rows themselves |
||
267 | * @param int $page |
||
268 | * @param int $count |
||
269 | * @param string $order_by |
||
270 | * @param bool $asc |
||
271 | * |
||
272 | * @return array|false|string |
||
273 | */ |
||
274 | function search ($search_parameters = [], $page = 1, $count = 20, $order_by = 'id', $asc = false) { |
||
351 | /** |
||
352 | * @param int $type |
||
353 | * |
||
354 | * @return string |
||
|
|||
355 | */ |
||
356 | protected function attribute_type_to_value_field ($type) { |
||
378 | /** |
||
379 | * Add new item |
||
380 | * |
||
381 | * @param int $category |
||
382 | * @param float $price |
||
383 | * @param int $in_stock |
||
384 | * @param int $soon |
||
385 | * @param int $listed |
||
386 | * @param array $attributes |
||
387 | * @param string[] $images |
||
388 | * @param array[] $videos |
||
389 | * @param string[] $tags |
||
390 | * |
||
391 | * @return false|int Id of created item on success of <b>false</> on failure |
||
1 ignored issue
–
show
|
|||
392 | */ |
||
393 | function add ($category, $price, $in_stock, $soon, $listed, $attributes, $images, $videos, $tags) { |
||
418 | /** |
||
419 | * Normalize attributes array structure |
||
420 | * |
||
421 | * @param array $attributes |
||
422 | * @param int $category |
||
423 | * @param string $clang |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | protected function prepare_attributes ($attributes, $category, $clang) { |
||
470 | /** |
||
471 | * Filter images to remove non-URL elements |
||
472 | * |
||
473 | * @param array $images |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | protected function prepare_images ($images) { |
||
485 | /** |
||
486 | * Normalize videos array structure |
||
487 | * |
||
488 | * @param array[] $videos |
||
489 | * |
||
490 | * @return array[] |
||
491 | */ |
||
492 | protected function prepare_videos ($videos) { |
||
515 | /** |
||
516 | * Transform array of string tags into array of their ids |
||
517 | * |
||
518 | * @param string[] $tags |
||
519 | * |
||
520 | * @return int[] |
||
521 | */ |
||
522 | protected function prepare_tags ($tags) { |
||
525 | /** |
||
526 | * Set data of specified item |
||
527 | * |
||
528 | * @param int $id |
||
529 | * @param int $category |
||
530 | * @param float $price |
||
531 | * @param int $in_stock |
||
532 | * @param int $soon |
||
533 | * @param int $listed |
||
534 | * @param array $attributes |
||
535 | * @param string[] $images |
||
536 | * @param array[] $videos |
||
537 | * @param string[] $tags |
||
538 | * |
||
539 | * @return bool |
||
540 | */ |
||
541 | function set ($id, $category, $price, $in_stock, $soon, $listed, $attributes, $images, $videos, $tags) { |
||
578 | /** |
||
579 | * Delete specified item |
||
580 | * |
||
581 | * @param int $id |
||
582 | * |
||
583 | * @return bool |
||
584 | */ |
||
585 | function del ($id) { |
||
602 | } |
||
603 |
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.