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 |
||
| 57 | class Cart extends \Model { |
||
| 58 | |||
| 59 | public static $objectName = 'Корзины'; |
||
| 60 | |||
| 61 | public static function indexes() { |
||
| 84 | |||
| 85 | public static function relations() { |
||
| 146 | |||
| 147 | public function beforeDelete() { |
||
| 161 | |||
| 162 | public static $labels = [ |
||
| 163 | 'user_id' => 'Пользователь', |
||
| 164 | 'cart_status_id' => 'Статус', |
||
| 165 | 'delivery_id' => 'Доставка', |
||
| 166 | 'comment' => 'Комментарий', |
||
| 167 | 'bonus_used' => 'Выгодные рубли', |
||
| 168 | 'complete_data' => 'Время заказа', |
||
| 169 | 'info' => 'Информация', |
||
| 170 | 'items' => 'Товары', |
||
| 171 | 'paytype_id' => 'Способ оплаты', |
||
| 172 | 'payed' => 'Оплачен', |
||
| 173 | 'exported' => 'Выгружено', |
||
| 174 | 'warehouse_block' => 'Блокировка товаров', |
||
| 175 | 'extra' => 'Доп.', |
||
| 176 | 'card_item_id' => 'Дисконтная карта', |
||
| 177 | 'info' => 'Информация', |
||
| 178 | 'contacts' => 'Информация', |
||
| 179 | 'pay' => 'Счета', |
||
| 180 | 'sums' => 'Суммы', |
||
| 181 | 'deliveryInfo' => 'Для доставки', |
||
| 182 | 'discount' => 'Скидки', |
||
| 183 | ]; |
||
| 184 | public static $cols = [ |
||
| 185 | //Основные параметры |
||
| 186 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
| 187 | 'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
||
| 188 | 'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
||
| 189 | 'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
||
| 190 | 'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
||
| 191 | 'warehouse_block' => ['type' => 'bool'], |
||
| 192 | 'payed' => ['type' => 'bool'], |
||
| 193 | 'comment' => ['type' => 'textarea'], |
||
| 194 | //Системные |
||
| 195 | 'exported' => ['type' => 'bool'], |
||
| 196 | 'complete_data' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 197 | 'date_status' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 198 | 'date_last_activ' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
| 199 | 'date_create' => ['type' => 'dateTime'], |
||
| 200 | //Виджеты |
||
| 201 | 'sums' => [ |
||
| 202 | 'type' => 'void', |
||
| 203 | 'view' => [ |
||
| 204 | 'type' => 'widget', |
||
| 205 | 'widget' => 'Ecommerce\adminSums', |
||
| 206 | ], |
||
| 207 | ], |
||
| 208 | 'contacts' => [ |
||
| 209 | 'type' => 'void', |
||
| 210 | 'view' => [ |
||
| 211 | 'type' => 'widget', |
||
| 212 | 'widget' => 'Ecommerce\admin/contacts', |
||
| 213 | ], |
||
| 214 | ], |
||
| 215 | //Менеджеры |
||
| 216 | 'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
||
| 217 | 'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
||
| 218 | 'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
||
| 219 | 'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
||
| 220 | 'deliveryInfo' => ['type' => 'dataManager', 'relation' => 'deliveryInfos'], |
||
| 221 | 'discount' => ['type' => 'dataManager', 'relation' => 'discounts'], |
||
| 222 | ]; |
||
| 223 | public static $dataManagers = [ |
||
| 224 | 'manager' => [ |
||
| 225 | 'cols' => [ |
||
| 226 | 'contacts', |
||
| 227 | 'items', |
||
| 228 | 'extra', |
||
| 229 | 'discount', |
||
| 230 | 'sums', |
||
| 231 | 'cart_status_id', |
||
| 232 | 'delivery_id', |
||
| 233 | 'deliveryInfo', |
||
| 234 | 'payed', |
||
| 235 | 'pay', |
||
| 236 | 'complete_data', |
||
| 237 | ], |
||
| 238 | 'sortable' => [ |
||
| 239 | 'cart_status_id', |
||
| 240 | 'delivery_id', |
||
| 241 | 'payed', |
||
| 242 | 'complete_data', |
||
| 243 | ], |
||
| 244 | 'filters' => [ |
||
| 245 | 'cart_status_id', |
||
| 246 | 'delivery_id', |
||
| 247 | 'payed', |
||
| 248 | 'complete_data', |
||
| 249 | ], |
||
| 250 | 'preSort' => [ |
||
| 251 | 'complete_data' => 'desc' |
||
| 252 | ], |
||
| 253 | 'actions' => [ |
||
| 254 | 'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
||
| 255 | ] |
||
| 256 | ] |
||
| 257 | ]; |
||
| 258 | |||
| 259 | public static function itemName($item) { |
||
| 262 | |||
| 263 | public static $forms = [ |
||
| 264 | 'manager' => [ |
||
| 265 | 'inputs' => [ |
||
| 266 | 'userSearch' => [ |
||
| 267 | 'type' => 'search', |
||
| 268 | 'source' => 'relation', |
||
| 269 | 'relation' => 'user', |
||
| 270 | 'label' => 'Покупатель', |
||
| 271 | 'cols' => [ |
||
| 272 | 'info:first_name', |
||
| 273 | 'info:last_name', |
||
| 274 | 'info:middle_name', |
||
| 275 | 'mail' |
||
| 276 | ], |
||
| 277 | 'col' => 'user_id', |
||
| 278 | 'required' => true, |
||
| 279 | 'showCol' => [ |
||
| 280 | 'type' => 'staticMethod', |
||
| 281 | 'class' => 'Ecommerce\Cart', |
||
| 282 | 'method' => 'itemName', |
||
| 283 | ], |
||
| 284 | ], |
||
| 285 | 'cardSearch' => [ |
||
| 286 | 'type' => 'search', |
||
| 287 | 'source' => 'relation', |
||
| 288 | 'relation' => 'card', |
||
| 289 | 'label' => 'Дисконтная карта', |
||
| 290 | 'cols' => [ |
||
| 291 | 'code', |
||
| 292 | 'user:info:first_name', |
||
| 293 | 'user:info:last_name', |
||
| 294 | 'user:info:middle_name', |
||
| 295 | 'user:mail' |
||
| 296 | ], |
||
| 297 | 'col' => 'card_item_id', |
||
| 298 | ], |
||
| 299 | ], |
||
| 300 | 'map' => [ |
||
| 301 | ['userSearch', 'cart_status_id'], |
||
| 302 | ['paytype_id', 'delivery_id'], |
||
| 303 | ['cardSearch', 'comment'], |
||
| 304 | ['warehouse_block', 'complete_data'], |
||
| 305 | ['payed'], |
||
| 306 | ['items'], |
||
| 307 | ['extra'], |
||
| 308 | ['pay'], |
||
| 309 | ['info'], |
||
| 310 | ['deliveryInfo'] |
||
| 311 | ] |
||
| 312 | ], |
||
| 313 | ]; |
||
| 314 | |||
| 315 | public function checkStage() { |
||
| 344 | |||
| 345 | public function needDelivery() { |
||
| 353 | |||
| 354 | public function deliverySum() { |
||
| 382 | |||
| 383 | public function hasDiscount() { |
||
| 386 | |||
| 387 | public function discountSum() { |
||
| 394 | |||
| 395 | public function finalSum() { |
||
| 401 | |||
| 402 | public function itemsSum() { |
||
| 413 | |||
| 414 | public function checkPrices() { |
||
| 427 | |||
| 428 | public function addItem($offer_price_id, $count = 1, $final_price = 0) { |
||
| 448 | |||
| 449 | public function calc($save = true) { |
||
| 454 | |||
| 455 | public function availablePayTypes() { |
||
| 469 | } |
||
| 470 |
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.