Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Item extends \Model { |
||
46 | |||
47 | public static $categoryModel = 'Ecommerce\Category'; |
||
48 | public static $objectName = 'Товар'; |
||
49 | public static $labels = [ |
||
50 | 'name' => 'Название', |
||
51 | 'title' => 'Торговое название', |
||
52 | 'subtitle' => 'Подзаголовок', |
||
53 | 'alias' => 'Алиас', |
||
54 | 'item_badge_id' => 'Наклейка', |
||
55 | 'category_id' => 'Раздел', |
||
56 | 'description' => 'Описание', |
||
57 | 'item_type_id' => 'Тип товара', |
||
58 | 'image_file_id' => 'Изображение', |
||
59 | 'best' => 'Лучшее предложение', |
||
60 | 'options' => 'Параметры', |
||
61 | 'offers' => 'Торговые предложения', |
||
62 | 'widget' => 'Виджет для отображения в каталоге', |
||
63 | 'view' => 'Шаблон для отображения полной информации', |
||
64 | 'deleted' => 'Удален', |
||
65 | 'imgs' => 'Фото', |
||
66 | 'date_create' => 'Дата создания' |
||
67 | ]; |
||
68 | public static $cols = [ |
||
69 | //Основные параметры |
||
70 | 'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'], |
||
71 | 'image_file_id' => ['type' => 'image'], |
||
72 | 'name' => ['type' => 'text'], |
||
73 | 'title' => ['type' => 'text'], |
||
74 | 'subtitle' => ['type' => 'text'], |
||
75 | 'alias' => ['type' => 'text'], |
||
76 | 'description' => ['type' => 'html'], |
||
77 | 'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'], |
||
78 | 'best' => ['type' => 'bool'], |
||
79 | 'item_badge_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'badge'], |
||
80 | 'deleted' => ['type' => 'bool'], |
||
81 | //Системные |
||
82 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
83 | 'weight' => ['type' => 'number'], |
||
84 | 'sales' => ['type' => 'number', 'logging' => false], |
||
85 | 'visible' => ['type' => 'number', 'logging' => false], |
||
86 | 'imported' => ['type' => 'text'], |
||
87 | 'tree_path' => ['type' => 'text'], |
||
88 | 'search_index' => ['type' => 'text', 'logging' => false], |
||
89 | 'date_create' => ['type' => 'dateTime'], |
||
90 | 'widget' => ['type' => 'text'], |
||
91 | 'view' => ['type' => 'text', 'logging' => false], |
||
92 | //Менеджеры |
||
93 | 'options' => ['type' => 'dataManager', 'relation' => 'options'], |
||
94 | 'offers' => ['type' => 'dataManager', 'relation' => 'offers'], |
||
95 | 'imgs' => ['type' => 'dataManager', 'relation' => 'images'], |
||
96 | ]; |
||
97 | |||
98 | public static function simpleItemHandler($request) { |
||
149 | |||
150 | public static $dataManagers = [ |
||
151 | 'manager' => [ |
||
152 | 'name' => 'Товары', |
||
153 | 'cols' => [ |
||
154 | 'name', |
||
155 | 'imgs', |
||
156 | 'category_id', |
||
157 | 'item_type_id', |
||
158 | 'best', |
||
159 | 'deleted', |
||
160 | 'options', |
||
161 | 'offers', |
||
162 | 'date_create' |
||
163 | ], |
||
164 | 'categorys' => [ |
||
165 | 'model' => 'Ecommerce\Category', |
||
166 | ], |
||
167 | 'sortable' => ['date_create'], |
||
168 | 'preSort' => [ |
||
169 | 'date_create' => 'desc' |
||
170 | ], |
||
171 | 'filters' => [ |
||
172 | 'id', 'name', 'best', 'deleted', 'date_create' |
||
173 | ], |
||
174 | 'sortMode' => true |
||
175 | ] |
||
176 | ]; |
||
177 | public static $forms = [ |
||
178 | 'manager' => [ |
||
179 | 'map' => [ |
||
180 | ['name', 'alias'], |
||
181 | ['title', 'subtitle'], |
||
182 | ['category_id', 'item_type_id', 'deleted'], |
||
183 | ['widget', 'view'], |
||
184 | ['best', 'item_badge_id', 'image_file_id'], |
||
185 | ['description'], |
||
186 | ['imgs'], |
||
187 | ['options'], |
||
188 | ['offers'], |
||
189 | ] |
||
190 | ], |
||
191 | 'simpleItem' => [ |
||
192 | 'options' => [ |
||
193 | 'access' => [ |
||
194 | 'groups' => [ |
||
195 | 3 |
||
196 | ] |
||
197 | ], |
||
198 | ], |
||
199 | 'name' => 'Простой товар с ценой', |
||
200 | 'inputs' => [ |
||
201 | 'name' => ['type' => 'text'], |
||
202 | 'description' => ['type' => 'html'], |
||
203 | 'category' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Category', 'label' => 'Категория'], |
||
204 | 'image' => ['type' => 'image', 'label' => 'Изображение'], |
||
205 | 'price' => ['type' => 'text', 'label' => 'Цена'], |
||
206 | 'currency' => ['type' => 'select', 'source' => 'model', 'model' => 'Money\Currency', 'label' => 'Валюта'], |
||
207 | 'options' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [ |
||
208 | 'inputs' => [ |
||
209 | 'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Option', 'label' => 'Свойство'], |
||
210 | 'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'], |
||
211 | ] |
||
212 | ] |
||
213 | ], |
||
214 | 'offerOptions' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [ |
||
215 | 'inputs' => [ |
||
216 | 'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Offer\Option', 'label' => 'Свойство предложения'], |
||
217 | 'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'], |
||
218 | ] |
||
219 | ], 'label' => 'Параметры предложения' |
||
220 | ] |
||
221 | ], |
||
222 | 'map' => [ |
||
223 | ['name', 'category'], |
||
224 | ['description'], |
||
225 | ['image'], |
||
226 | ['price', 'currency'], |
||
227 | ['options'], |
||
228 | ['offerOptions'], |
||
229 | ], |
||
230 | 'handler' => 'simpleItemHandler' |
||
231 | ] |
||
232 | ]; |
||
233 | |||
234 | View Code Duplication | public function realType() { |
|
235 | if ($this->option && $this->option->type) { |
||
236 | $type = $this->option->type; |
||
237 | |||
238 | if ($type == 'select') { |
||
239 | return [ |
||
240 | 'type' => 'select', |
||
241 | 'source' => 'relation', |
||
242 | 'relation' => 'option:items', |
||
243 | ]; |
||
244 | } |
||
245 | return $type; |
||
246 | } |
||
247 | return 'text'; |
||
248 | } |
||
249 | |||
250 | public static function indexes() { |
||
272 | |||
273 | public function isVisible() { |
||
274 | if ($this->deleted) { |
||
275 | return false; |
||
276 | } |
||
277 | if (empty(\App::$cur->Ecommerce->config['view_empty_image']) && !$this->image_file_id) { |
||
278 | return false; |
||
279 | } |
||
280 | if (empty(\App::$cur->Ecommerce->config['view_empty_warehouse'])) { |
||
281 | $warehouseIds = \Ecommerce\OptionsParser::getWarehouses(); |
||
282 | $selectOptions = ['where' => [['item_offer_item_id', $this->id]]]; |
||
283 | $selectOptions['where'][] = [ |
||
284 | '( |
||
285 | (SELECT COALESCE(sum(`' . Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
286 | FROM ' . \App::$cur->db->table_prefix . Item\Offer\Warehouse::table() . ' iciw |
||
287 | WHERE iciw.' . Item\Offer\Warehouse::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ' |
||
288 | ' . ($warehouseIds ? ' AND iciw.' . Item\Offer\Warehouse::colPrefix() . Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' |
||
289 | ) |
||
290 | - |
||
291 | (SELECT COALESCE(sum(' . Warehouse\Block::colPrefix() . 'count) ,0) |
||
292 | FROM ' . \App::$cur->db->table_prefix . Warehouse\Block::table() . ' iewb |
||
293 | inner JOIN ' . \App::$cur->db->table_prefix . Cart::table() . ' icc ON icc.' . Cart::index() . ' = iewb.' . Warehouse\Block::colPrefix() . Cart::index() . ' AND ( |
||
294 | (`' . Cart::colPrefix() . 'warehouse_block` = 1 and `' . Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
295 | (`' . Cart::colPrefix() . Cart\Status::index() . '` in(0,1) and `' . Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
296 | ) |
||
297 | WHERE iewb.' . Warehouse\Block::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ') |
||
298 | )', |
||
299 | 0, |
||
300 | '>' |
||
301 | ]; |
||
302 | if (!Item\Offer::getList($selectOptions)) { |
||
303 | return false; |
||
304 | } |
||
305 | } |
||
306 | $isset = empty(\App::$cur->Ecommerce->config['available_price_types']); |
||
307 | $nozero = !empty(\App::$cur->Ecommerce->config['show_without_price']); |
||
308 | if (!$isset || !$nozero) { |
||
309 | $this->loadRelation('offers'); |
||
310 | foreach ($this->offers as $offer) { |
||
311 | foreach ($offer->prices as $price) { |
||
312 | if (empty(\App::$cur->Ecommerce->config['available_price_types']) || in_array($price->item_offer_price_type_id, \App::$cur->Ecommerce->config['available_price_types'])) { |
||
313 | $isset = true; |
||
314 | } |
||
315 | if ($price->price > 0) { |
||
316 | $nozero = true; |
||
317 | } |
||
318 | if ($isset && $nozero) { |
||
319 | break 2; |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | if (!$isset || !$nozero) { |
||
324 | return false; |
||
325 | } |
||
326 | } |
||
327 | return true; |
||
328 | } |
||
329 | |||
330 | public function beforeSave() { |
||
333 | |||
334 | public function afterSave() { |
||
394 | |||
395 | public static function relations() { |
||
437 | |||
438 | /** |
||
439 | * @param int $offerId |
||
440 | * @param bool|\Ecommerce\Cart $cart |
||
441 | * @return bool|Item\Offer\Price|null |
||
442 | */ |
||
443 | public function getPrice($offerId = 0, $cart = false) { |
||
449 | |||
450 | public function name() { |
||
459 | |||
460 | public function afterDelete() { |
||
481 | |||
482 | public function getHref() { |
||
485 | |||
486 | public function inFav() { |
||
495 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.