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 |
||
| 44 | class Cart extends \Model { |
||
| 45 | |||
| 46 | public static $objectName = 'Корзины'; |
||
| 47 | |||
| 48 | public static function indexes() { |
||
| 71 | |||
| 72 | public static function relations() { |
||
| 131 | |||
| 132 | public function beforeDelete() { |
||
| 146 | |||
| 147 | public static $labels = [ |
||
| 148 | 'user_id' => 'Пользователь', |
||
| 149 | 'cart_status_id' => 'Статус', |
||
| 150 | 'delivery_id' => 'Доставка', |
||
| 151 | 'comment' => 'Комментарий', |
||
| 152 | 'bonus_used' => 'Выгодные рубли', |
||
| 153 | 'complete_data' => 'Время заказа', |
||
| 154 | 'info' => 'Информация', |
||
| 155 | 'items' => 'Товары', |
||
| 156 | 'paytype_id' => 'Способ оплаты', |
||
| 157 | 'payed' => 'Оплачен', |
||
| 158 | 'exported' => 'Выгружено', |
||
| 159 | 'warehouse_block' => 'Блокировка товаров', |
||
| 160 | 'extra' => 'Доп.', |
||
| 161 | 'card_item_id' => 'Дисконтная карта', |
||
| 162 | 'info' => 'Информация', |
||
| 163 | 'contacts' => 'Информация', |
||
| 164 | 'pay' => 'Счета', |
||
| 165 | 'sums' => 'Суммы', |
||
| 166 | 'deliveryInfo' => 'Для доставки', |
||
| 167 | 'discount' => 'Скидки', |
||
| 168 | ]; |
||
| 169 | public static $cols = [ |
||
| 170 | //Основные параметры |
||
| 171 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
| 172 | 'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
||
| 173 | 'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
||
| 174 | 'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
||
| 175 | 'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
||
| 176 | 'warehouse_block' => ['type' => 'bool'], |
||
| 177 | 'payed' => ['type' => 'bool'], |
||
| 178 | 'comment' => ['type' => 'textarea'], |
||
| 179 | //Системные |
||
| 180 | 'exported' => ['type' => 'bool'], |
||
| 181 | 'complete_data' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 182 | 'date_status' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 183 | 'date_last_activ' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 184 | 'date_create' => ['type' => 'dateTime'], |
||
| 185 | //Виджеты |
||
| 186 | 'sums' => [ |
||
| 187 | 'type' => 'void', |
||
| 188 | 'view' => [ |
||
| 189 | 'type' => 'widget', |
||
| 190 | 'widget' => 'Ecommerce\adminSums', |
||
| 191 | ], |
||
| 192 | ], |
||
| 193 | 'contacts' => [ |
||
| 194 | 'type' => 'void', |
||
| 195 | 'view' => [ |
||
| 196 | 'type' => 'widget', |
||
| 197 | 'widget' => 'Ecommerce\admin/contacts', |
||
| 198 | ], |
||
| 199 | ], |
||
| 200 | //Менеджеры |
||
| 201 | 'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
||
| 202 | 'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
||
| 203 | 'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
||
| 204 | 'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
||
| 205 | 'deliveryInfo' => ['type' => 'dataManager', 'relation' => 'deliveryInfos'], |
||
| 206 | 'discount' => ['type' => 'dataManager', 'relation' => 'discounts'], |
||
| 207 | ]; |
||
| 208 | public static $dataManagers = [ |
||
| 209 | 'manager' => [ |
||
| 210 | 'cols' => [ |
||
| 211 | 'contacts', |
||
| 212 | 'items', |
||
| 213 | 'extra', |
||
| 214 | 'discount', |
||
| 215 | 'sums', |
||
| 216 | 'cart_status_id', |
||
| 217 | 'delivery_id', |
||
| 218 | 'deliveryInfo', |
||
| 219 | 'payed', |
||
| 220 | 'pay', |
||
| 221 | 'complete_data', |
||
| 222 | ], |
||
| 223 | 'sortable' => [ |
||
| 224 | 'cart_status_id', |
||
| 225 | 'delivery_id', |
||
| 226 | 'payed', |
||
| 227 | 'complete_data', |
||
| 228 | ], |
||
| 229 | 'filters' => [ |
||
| 230 | 'cart_status_id', |
||
| 231 | 'delivery_id', |
||
| 232 | 'payed', |
||
| 233 | 'complete_data', |
||
| 234 | ], |
||
| 235 | 'preSort' => [ |
||
| 236 | 'complete_data' => 'desc' |
||
| 237 | ], |
||
| 238 | 'actions' => [ |
||
| 239 | 'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
||
| 240 | ] |
||
| 241 | ] |
||
| 242 | ]; |
||
| 243 | |||
| 244 | public static function itemName($item) { |
||
| 247 | |||
| 248 | public static $forms = [ |
||
| 249 | 'manager' => [ |
||
| 250 | 'inputs' => [ |
||
| 251 | 'userSearch' => [ |
||
| 252 | 'type' => 'search', |
||
| 253 | 'source' => 'relation', |
||
| 254 | 'relation' => 'user', |
||
| 255 | 'label' => 'Покупатель', |
||
| 256 | 'cols' => [ |
||
| 257 | 'info:first_name', |
||
| 258 | 'info:last_name', |
||
| 259 | 'info:middle_name', |
||
| 260 | 'mail' |
||
| 261 | ], |
||
| 262 | 'col' => 'user_id', |
||
| 263 | 'required' => true, |
||
| 264 | 'showCol' => [ |
||
| 265 | 'type' => 'staticMethod', |
||
| 266 | 'class' => 'Ecommerce\Cart', |
||
| 267 | 'method' => 'itemName', |
||
| 268 | ], |
||
| 269 | ], |
||
| 270 | 'cardSearch' => [ |
||
| 271 | 'type' => 'search', |
||
| 272 | 'source' => 'relation', |
||
| 273 | 'relation' => 'card', |
||
| 274 | 'label' => 'Дисконтная карта', |
||
| 275 | 'cols' => [ |
||
| 276 | 'code', |
||
| 277 | 'user:info:first_name', |
||
| 278 | 'user:info:last_name', |
||
| 279 | 'user:info:middle_name', |
||
| 280 | 'user:mail' |
||
| 281 | ], |
||
| 282 | 'col' => 'card_item_id', |
||
| 283 | ], |
||
| 284 | ], |
||
| 285 | 'map' => [ |
||
| 286 | ['userSearch', 'cart_status_id'], |
||
| 287 | ['paytype_id', 'delivery_id'], |
||
| 288 | ['cardSearch', 'comment'], |
||
| 289 | ['warehouse_block', 'complete_data'], |
||
| 290 | ['payed'], |
||
| 291 | ['items'], |
||
| 292 | ['extra'], |
||
| 293 | ['pay'], |
||
| 294 | ['info'], |
||
| 295 | ['deliveryInfo'] |
||
| 296 | ] |
||
| 297 | ], |
||
| 298 | ]; |
||
| 299 | |||
| 300 | public function checkStage() { |
||
| 329 | |||
| 330 | public function needDelivery() { |
||
| 338 | |||
| 339 | public function deliverySum() { |
||
| 367 | |||
| 368 | public function hasDiscount() { |
||
| 371 | |||
| 372 | public function discountSum() { |
||
| 379 | |||
| 380 | public function finalSum() { |
||
| 386 | |||
| 387 | public function itemsSum() { |
||
| 398 | |||
| 399 | public function addItem($offer_price_id, $count = 1, $final_price = 0) { |
||
| 419 | |||
| 420 | public function calc($save = true) { |
||
| 425 | |||
| 426 | } |
||
| 427 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.