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 |
||
| 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 | |||
| 59 | public static function simpleItemHandler($request) { |
||
| 110 | |||
| 111 | public static $dataManagers = [ |
||
| 112 | 'manager' => [ |
||
| 113 | 'name' => 'Товары', |
||
| 114 | 'cols' => [ |
||
| 115 | 'name', |
||
| 116 | 'imgs', |
||
| 117 | 'category_id', |
||
| 118 | 'item_type_id', |
||
| 119 | 'best', |
||
| 120 | 'deleted', |
||
| 121 | 'options', |
||
| 122 | 'offers', |
||
| 123 | ], |
||
| 124 | 'categorys' => [ |
||
| 125 | 'model' => 'Ecommerce\Category', |
||
| 126 | ], |
||
| 127 | 'sortMode' => true |
||
| 128 | ] |
||
| 129 | ]; |
||
| 130 | public static $forms = [ |
||
| 131 | 'manager' => [ |
||
| 132 | 'map' => [ |
||
| 133 | ['name', 'alias'], |
||
| 134 | ['category_id', 'item_type_id', 'deleted'], |
||
| 135 | ['widget', 'view'], |
||
| 136 | ['best', 'image_file_id'], |
||
| 137 | ['description'], |
||
| 138 | ['imgs'], |
||
| 139 | ['options'], |
||
| 140 | ['offers'], |
||
| 141 | ] |
||
| 142 | ], |
||
| 143 | 'simpleItem' => [ |
||
| 144 | 'options' => [ |
||
| 145 | 'access' => [ |
||
| 146 | 'groups' => [ |
||
| 147 | 3 |
||
| 148 | ] |
||
| 149 | ], |
||
| 150 | ], |
||
| 151 | 'name' => 'Простой товар с ценой', |
||
| 152 | 'inputs' => [ |
||
| 153 | 'name' => ['type' => 'text'], |
||
| 154 | 'description' => ['type' => 'html'], |
||
| 155 | 'category' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Category', 'label' => 'Категория'], |
||
| 156 | 'image' => ['type' => 'image', 'label' => 'Изображение'], |
||
| 157 | 'price' => ['type' => 'text', 'label' => 'Цена'], |
||
| 158 | 'currency' => ['type' => 'select', 'source' => 'model', 'model' => 'Money\Currency', 'label' => 'Валюта'], |
||
| 159 | 'options' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [ |
||
| 160 | 'inputs' => [ |
||
| 161 | 'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Option', 'label' => 'Свойство'], |
||
| 162 | 'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'], |
||
| 163 | ] |
||
| 164 | ] |
||
| 165 | ], |
||
| 166 | 'offerOptions' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [ |
||
| 167 | 'inputs' => [ |
||
| 168 | 'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Offer\Option', 'label' => 'Свойство предложения'], |
||
| 169 | 'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'], |
||
| 170 | ] |
||
| 171 | ],'label'=>'Параметры предлоежния' |
||
| 172 | ] |
||
| 173 | ], |
||
| 174 | 'map' => [ |
||
| 175 | ['name', 'category'], |
||
| 176 | ['description'], |
||
| 177 | ['image'], |
||
| 178 | ['price', 'currency'], |
||
| 179 | ['options'], |
||
| 180 | ['offerOptions'], |
||
| 181 | ], |
||
| 182 | 'handler' => 'simpleItemHandler' |
||
| 183 | ] |
||
| 184 | ]; |
||
| 185 | |||
| 186 | public function realType() { |
||
| 201 | |||
| 202 | public static function indexes() { |
||
| 224 | |||
| 225 | public function beforeSave() { |
||
| 260 | |||
| 261 | public static function relations() { |
||
| 300 | |||
| 301 | public function getPrice() { |
||
| 317 | |||
| 318 | public function name() { |
||
| 327 | |||
| 328 | public function beforeDelete() { |
||
| 345 | |||
| 346 | public function getHref() { |
||
| 349 | |||
| 350 | } |
||
| 351 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: