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 |
||
47 | class Items { |
||
48 | use CRUD_helpers { |
||
49 | search as crud_search; |
||
50 | } |
||
51 | use |
||
52 | Singleton; |
||
53 | |||
54 | const DEFAULT_IMAGE = 'components/modules/Shop/includes/img/no-image.svg'; |
||
55 | |||
56 | protected $data_model = [ |
||
57 | 'id' => 'int', |
||
58 | 'date' => 'int', |
||
59 | 'category' => 'int', |
||
60 | 'price' => 'float', |
||
61 | 'in_stock' => 'int', |
||
62 | 'soon' => 'int:0..1', |
||
63 | 'listed' => 'int:0..1', |
||
64 | 'attributes' => [ |
||
65 | 'data_model' => [ |
||
66 | 'id' => 'int', |
||
67 | 'attribute' => 'int', |
||
68 | 'numeric_value' => 'float', |
||
69 | 'string_value' => 'text', |
||
70 | 'text_value' => 'html', |
||
71 | 'lang' => 'text' // Some attributes are language-dependent, some aren't, so we'll handle that manually |
||
72 | ] |
||
73 | ], |
||
74 | 'images' => [ |
||
75 | 'data_model' => [ |
||
76 | 'id' => 'int', |
||
77 | 'image' => 'text' |
||
78 | ] |
||
79 | ], |
||
80 | 'videos' => [ |
||
81 | 'data_model' => [ |
||
82 | 'id' => 'int', |
||
83 | 'video' => 'text', |
||
84 | 'poster' => 'text', |
||
85 | 'type' => 'text' |
||
86 | ] |
||
87 | ], |
||
88 | 'tags' => [ |
||
89 | 'data_model' => [ |
||
90 | 'id' => 'int', |
||
91 | 'tag' => 'html' |
||
92 | ], |
||
93 | 'language_field' => 'lang' |
||
94 | ] |
||
95 | ]; |
||
96 | protected $table = '[prefix]shop_items'; |
||
97 | protected $data_model_files_tag_prefix = 'Shop/items'; |
||
98 | /** |
||
99 | * @var Prefix |
||
100 | */ |
||
101 | protected $cache; |
||
102 | |||
103 | protected function construct () { |
||
106 | /** |
||
107 | * Returns database index |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | protected function cdb () { |
||
114 | /** |
||
115 | * Get item |
||
116 | * |
||
117 | * @param int|int[] $id |
||
118 | * |
||
119 | * @return array|false |
||
120 | */ |
||
121 | function get ($id) { |
||
156 | /** |
||
157 | * Transform normalized attributes structure back into simple initial structure |
||
158 | * |
||
159 | * @param array $attributes |
||
160 | * @param string $clang |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | protected function read_attributes_processing ($attributes, $clang) { |
||
204 | /** |
||
205 | * Transform tags ids back into array of strings |
||
206 | * |
||
207 | * @param int[] $tags |
||
208 | * |
||
209 | * @return string[] |
||
210 | */ |
||
211 | protected function read_tags_processing ($tags) { |
||
214 | /** |
||
215 | * Get item data for specific user (price might be adjusted, some items may be restricted and so on) |
||
216 | * |
||
217 | * @param int|int[] $id |
||
218 | * @param bool|int $user |
||
219 | * |
||
220 | * @return array|false |
||
221 | */ |
||
222 | function get_for_user ($id, $user = false) { |
||
246 | /** |
||
247 | * Get array of all items |
||
248 | * |
||
249 | * @return int[] Array of items ids |
||
250 | */ |
||
251 | function get_all () { |
||
262 | /** |
||
263 | * Items search |
||
264 | * |
||
265 | * @param mixed[] $search_parameters Array in form [attribute => value], [attribute => [value, value]], [attribute => [from => value, to => value]], |
||
266 | * [property => value], [tag] or mixed; if `total_count => 1` element is present - total number of found rows will be |
||
267 | * returned instead of rows themselves |
||
268 | * @param int $page |
||
269 | * @param int $count |
||
270 | * @param string $order_by |
||
271 | * @param bool $asc |
||
272 | * |
||
273 | * @return array|false|string |
||
|
|||
274 | */ |
||
275 | function search ($search_parameters = [], $page = 1, $count = 20, $order_by = 'id', $asc = false) { |
||
352 | /** |
||
353 | * @param int $type |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function attribute_type_to_value_field ($type) { |
||
379 | /** |
||
380 | * Add new item |
||
381 | * |
||
382 | * @param int $category |
||
383 | * @param float $price |
||
384 | * @param int $in_stock |
||
385 | * @param int $soon |
||
386 | * @param int $listed |
||
387 | * @param array $attributes |
||
388 | * @param string[] $images |
||
389 | * @param array[] $videos |
||
390 | * @param string[] $tags |
||
391 | * |
||
392 | * @return false|int Id of created item on success of <b>false</> on failure |
||
1 ignored issue
–
show
|
|||
393 | */ |
||
394 | function add ($category, $price, $in_stock, $soon, $listed, $attributes, $images, $videos, $tags) { |
||
421 | /** |
||
422 | * Normalize attributes array structure |
||
423 | * |
||
424 | * @param array $attributes |
||
425 | * @param int $category |
||
426 | * @param string $clang |
||
427 | * |
||
428 | * @return array |
||
429 | */ |
||
430 | protected function prepare_attributes ($attributes, $category, $clang) { |
||
473 | /** |
||
474 | * Filter images to remove non-URL elements |
||
475 | * |
||
476 | * @param array $images |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | protected function prepare_images ($images) { |
||
488 | /** |
||
489 | * Normalize videos array structure |
||
490 | * |
||
491 | * @param array[] $videos |
||
492 | * |
||
493 | * @return array[] |
||
494 | */ |
||
495 | protected function prepare_videos ($videos) { |
||
518 | /** |
||
519 | * Transform array of string tags into array of their ids |
||
520 | * |
||
521 | * @param string[] $tags |
||
522 | * |
||
523 | * @return int[] |
||
524 | */ |
||
525 | protected function prepare_tags ($tags) { |
||
528 | /** |
||
529 | * Set data of specified item |
||
530 | * |
||
531 | * @param int $id |
||
532 | * @param int $category |
||
533 | * @param float $price |
||
534 | * @param int $in_stock |
||
535 | * @param int $soon |
||
536 | * @param int $listed |
||
537 | * @param array $attributes |
||
538 | * @param string[] $images |
||
539 | * @param array[] $videos |
||
540 | * @param string[] $tags |
||
541 | * |
||
542 | * @return bool |
||
543 | */ |
||
544 | function set ($id, $category, $price, $in_stock, $soon, $listed, $attributes, $images, $videos, $tags) { |
||
583 | /** |
||
584 | * Delete specified item |
||
585 | * |
||
586 | * @param int $id |
||
587 | * |
||
588 | * @return bool |
||
589 | */ |
||
590 | function del ($id) { |
||
607 | } |
||
608 |
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.