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:
1 | <?php |
||
14 | class Item extends \Model { |
||
15 | |||
16 | public static $categoryModel = 'Ecommerce\Category'; |
||
17 | public static $objectName = 'Товар'; |
||
18 | public static $labels = [ |
||
19 | 'name' => 'Название', |
||
20 | 'alias' => 'Алиас', |
||
21 | 'category_id' => 'Раздел', |
||
22 | 'description' => 'Описание', |
||
23 | 'item_type_id' => 'Тип товара', |
||
24 | 'image_file_id' => 'Изображение', |
||
25 | 'best' => 'Лучшее предложение', |
||
26 | 'options' => 'Параметры', |
||
27 | 'offers' => 'Торговые предложения', |
||
28 | 'widget' => 'Виджет для отображения в каталоге', |
||
29 | 'view' => 'Шаблон для отображения полной информации', |
||
30 | 'deleted' => 'Удален', |
||
31 | 'imgs' => 'Фото' |
||
32 | ]; |
||
33 | public static $cols = [ |
||
34 | //Основные параметры |
||
35 | 'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'], |
||
36 | 'image_file_id' => ['type' => 'image'], |
||
37 | 'name' => ['type' => 'text'], |
||
38 | 'alias' => ['type' => 'text'], |
||
39 | 'description' => ['type' => 'html'], |
||
40 | 'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'], |
||
41 | 'best' => ['type' => 'bool'], |
||
42 | 'deleted' => ['type' => 'bool'], |
||
43 | //Системные |
||
44 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
45 | 'weight' => ['type' => 'number'], |
||
46 | 'sales' => ['type' => 'number'], |
||
47 | 'imported' => ['type' => 'text'], |
||
48 | 'tree_path' => ['type' => 'text'], |
||
49 | 'search_index' => ['type' => 'text'], |
||
50 | 'date_create' => ['type' => 'dateTime'], |
||
51 | 'widget' => ['type' => 'text'], |
||
52 | 'view' => ['type' => 'text'], |
||
53 | //Менеджеры |
||
54 | 'options' => ['type' => 'dataManager', 'relation' => 'options'], |
||
55 | 'offers' => ['type' => 'dataManager', 'relation' => 'offers'], |
||
56 | 'imgs' => ['type' => 'dataManager', 'relation' => 'images'], |
||
57 | ]; |
||
58 | public static $dataManagers = [ |
||
59 | 'manager' => [ |
||
60 | 'name' => 'Товары', |
||
61 | 'cols' => [ |
||
62 | 'name', |
||
63 | 'imgs', |
||
64 | 'category_id', |
||
65 | 'item_type_id', |
||
66 | 'best', |
||
67 | 'deleted', |
||
68 | 'options', |
||
69 | 'offers', |
||
70 | ], |
||
71 | 'categorys' => [ |
||
72 | 'model' => 'Ecommerce\Category', |
||
73 | ], |
||
74 | 'sortMode' => true |
||
75 | ] |
||
76 | ]; |
||
77 | public static $forms = [ |
||
78 | 'manager' => [ |
||
79 | 'map' => [ |
||
80 | ['name', 'alias'], |
||
81 | ['category_id', 'item_type_id', 'deleted'], |
||
82 | ['widget', 'view'], |
||
83 | ['best', 'image_file_id'], |
||
84 | ['description'], |
||
85 | ['imgs'], |
||
86 | ['options'], |
||
87 | ['offers'], |
||
88 | ] |
||
89 | ]]; |
||
90 | |||
91 | public static function indexes() { |
||
113 | |||
114 | public function beforeSave() { |
||
149 | |||
150 | public static function relations() { |
||
189 | |||
190 | public function getPrice() { |
||
206 | |||
207 | public function name() { |
||
216 | |||
217 | public function beforeDelete() { |
||
234 | |||
235 | public function getHref() { |
||
238 | |||
239 | } |
||
240 |
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.