Complex classes like Cart 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 Cart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Cart extends \Model |
||
| 15 | { |
||
| 16 | public static $objectName = 'Корзины'; |
||
| 17 | |||
| 18 | public static function indexes() |
||
| 42 | |||
| 43 | public static function relations() |
||
| 103 | |||
| 104 | public function beforeDelete() |
||
| 119 | |||
| 120 | public static $labels = [ |
||
| 121 | 'user_id' => 'Пользователь', |
||
| 122 | 'cart_status_id' => 'Статус', |
||
| 123 | 'delivery_id' => 'Доставка', |
||
| 124 | 'comment' => 'Комментарий', |
||
| 125 | 'bonus_used' => 'Выгодные рубли', |
||
| 126 | 'complete_data' => 'Время заказа', |
||
| 127 | 'info' => 'Информация', |
||
| 128 | 'items' => 'Товары', |
||
| 129 | 'paytype_id' => 'Способ оплаты', |
||
| 130 | 'payed' => 'Оплачен', |
||
| 131 | 'exported' => 'Выгружено', |
||
| 132 | 'warehouse_block' => 'Блокировка товаров', |
||
| 133 | 'extra' => 'Доп.', |
||
| 134 | 'card_item_id' => 'Дисконтная карта', |
||
| 135 | 'info' => 'Информация', |
||
| 136 | 'contacts' => 'Информация', |
||
| 137 | 'pay' => 'Счета', |
||
| 138 | 'sums' => 'Суммы', |
||
| 139 | 'deliveryInfo' => 'Для доставки', |
||
| 140 | 'discount' => 'Скидки', |
||
| 141 | ]; |
||
| 142 | public static $cols = [ |
||
| 143 | //Основные параметры |
||
| 144 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
| 145 | 'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
||
| 146 | 'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
||
| 147 | 'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
||
| 148 | 'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
||
| 149 | 'warehouse_block' => ['type' => 'bool'], |
||
| 150 | 'payed' => ['type' => 'bool'], |
||
| 151 | 'comment' => ['type' => 'textarea'], |
||
| 152 | //Системные |
||
| 153 | 'exported' => ['type' => 'bool'], |
||
| 154 | 'complete_data' => ['type' => 'dateTime'], |
||
| 155 | 'date_status' => ['type' => 'dateTime'], |
||
| 156 | 'date_last_activ' => ['type' => 'dateTime'], |
||
| 157 | 'date_create' => ['type' => 'dateTime'], |
||
| 158 | //Виджеты |
||
| 159 | 'sums' => [ |
||
| 160 | 'type' => 'void', |
||
| 161 | 'view' => [ |
||
| 162 | 'type' => 'widget', |
||
| 163 | 'widget' => 'Ecommerce\adminSums', |
||
| 164 | ], |
||
| 165 | ], |
||
| 166 | 'contacts' => [ |
||
| 167 | 'type' => 'void', |
||
| 168 | 'view' => [ |
||
| 169 | 'type' => 'widget', |
||
| 170 | 'widget' => 'Ecommerce\admin/contacts', |
||
| 171 | ], |
||
| 172 | ], |
||
| 173 | //Менеджеры |
||
| 174 | 'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
||
| 175 | 'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
||
| 176 | 'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
||
| 177 | 'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
||
| 178 | 'deliveryInfo' => ['type' => 'dataManager', 'relation' => 'deliveryInfos'], |
||
| 179 | 'discount' => ['type' => 'dataManager', 'relation' => 'discounts'], |
||
| 180 | ]; |
||
| 181 | public static $dataManagers = [ |
||
| 182 | 'manager' => [ |
||
| 183 | 'cols' => [ |
||
| 184 | 'contacts', |
||
| 185 | 'items', |
||
| 186 | 'extra', |
||
| 187 | 'discount', |
||
| 188 | 'sums', |
||
| 189 | 'cart_status_id', |
||
| 190 | 'delivery_id', |
||
| 191 | 'deliveryInfo', |
||
| 192 | 'payed', |
||
| 193 | 'pay', |
||
| 194 | 'complete_data', |
||
| 195 | ], |
||
| 196 | 'sortable' => [ |
||
| 197 | 'cart_status_id', |
||
| 198 | 'delivery_id', |
||
| 199 | 'payed', |
||
| 200 | 'complete_data', |
||
| 201 | ], |
||
| 202 | 'filters' => [ |
||
| 203 | 'cart_status_id', |
||
| 204 | 'delivery_id', |
||
| 205 | 'payed', |
||
| 206 | 'complete_data', |
||
| 207 | ], |
||
| 208 | 'preSort' => [ |
||
| 209 | 'complete_data' => 'desc' |
||
| 210 | ], |
||
| 211 | 'actions' => [ |
||
| 212 | 'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
||
| 213 | ] |
||
| 214 | ] |
||
| 215 | ]; |
||
| 216 | |||
| 217 | public static function itemName($item) |
||
| 221 | |||
| 222 | public static $forms = [ |
||
| 223 | 'manager' => [ |
||
| 224 | 'inputs' => [ |
||
| 225 | 'userSearch' => [ |
||
| 226 | 'type' => 'search', |
||
| 227 | 'source' => 'relation', |
||
| 228 | 'relation' => 'user', |
||
| 229 | 'label' => 'Покупатель', |
||
| 230 | 'cols' => [ |
||
| 231 | 'info:first_name', |
||
| 232 | 'info:last_name', |
||
| 233 | 'info:middle_name', |
||
| 234 | 'mail' |
||
| 235 | ], |
||
| 236 | 'col' => 'user_id', |
||
| 237 | 'required' => true, |
||
| 238 | 'showCol' => [ |
||
| 239 | 'type' => 'staticMethod', |
||
| 240 | 'class' => 'Ecommerce\Cart', |
||
| 241 | 'method' => 'itemName', |
||
| 242 | ], |
||
| 243 | ], |
||
| 244 | 'cardSearch' => [ |
||
| 245 | 'type' => 'search', |
||
| 246 | 'source' => 'relation', |
||
| 247 | 'relation' => 'card', |
||
| 248 | 'label' => 'Дисконтная карта', |
||
| 249 | 'cols' => [ |
||
| 250 | 'code', |
||
| 251 | 'user:info:first_name', |
||
| 252 | 'user:info:last_name', |
||
| 253 | 'user:info:middle_name', |
||
| 254 | 'user:mail' |
||
| 255 | ], |
||
| 256 | 'col' => 'card_item_id', |
||
| 257 | ], |
||
| 258 | ], |
||
| 259 | 'map' => [ |
||
| 260 | ['userSearch', 'cart_status_id'], |
||
| 261 | ['paytype_id', 'delivery_id'], |
||
| 262 | ['cardSearch', 'comment'], |
||
| 263 | ['warehouse_block', 'complete_data'], |
||
| 264 | ['payed'], |
||
| 265 | ['items'], |
||
| 266 | ['extra'], |
||
| 267 | ['pay'], |
||
| 268 | ['info'], |
||
| 269 | ['deliveryInfo'] |
||
| 270 | ] |
||
| 271 | ], |
||
| 272 | ]; |
||
| 273 | |||
| 274 | public function checkStage() |
||
| 305 | |||
| 306 | public function needDelivery() |
||
| 315 | |||
| 316 | public function deliverySum() |
||
| 330 | |||
| 331 | public function hasDiscount() |
||
| 335 | |||
| 336 | public function discountSum() |
||
| 344 | |||
| 345 | public function finalSum() |
||
| 352 | |||
| 353 | public function itemsSum() |
||
| 365 | |||
| 366 | public function addItem($offer_price_id, $count = 1, $final_price = 0) |
||
| 387 | |||
| 388 | public function calc($save = true) |
||
| 394 | |||
| 395 | } |
||
| 396 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.