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 CartController 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 CartController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class CartController extends Controller { |
||
| 12 | |||
| 13 | public function indexAction() { |
||
| 14 | $cart = ''; |
||
| 15 | $deliverys = \Ecommerce\Delivery::getList(['where' => ['disabled', 0], 'order' => ['weight', 'ASC']]); |
||
| 16 | $payTypes = \Ecommerce\PayType::getList(['order' => ['weight', 'ASC']]); |
||
| 17 | if (!empty($_SESSION['cart']['cart_id'])) { |
||
| 18 | $cart = Ecommerce\Cart::get($_SESSION['cart']['cart_id']); |
||
| 19 | if (!empty($_POST)) { |
||
| 20 | $error = false; |
||
| 21 | if (!Users\User::$cur->id) { |
||
| 22 | $user_id = $this->Users->registration($_POST, true); |
||
| 23 | if (!$user_id) { |
||
| 24 | $error = true; |
||
| 25 | } else { |
||
| 26 | $user = Users\User::get($user_id); |
||
| 27 | } |
||
| 28 | } else { |
||
| 29 | $user = Users\User::$cur; |
||
| 30 | } |
||
| 31 | $ids = []; |
||
| 32 | if (!empty($_POST['cartItems'])) { |
||
| 33 | foreach ($_POST['cartItems'] as $cartItemId => $cartItemCont) { |
||
| 34 | $cartItem = \Ecommerce\Cart\Item::get((int)$cartItemId); |
||
| 35 | if (!$cartItem) { |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | if ($cartItem->cart_id != $cart->id) { |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | $count = (float)$cartItemCont; |
||
| 42 | if ($count < 0.001) { |
||
| 43 | $count = 1; |
||
| 44 | } |
||
| 45 | $cartItem->count = $count; |
||
| 46 | $cartItem->save(); |
||
| 47 | $ids[] = $cartItemId; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | foreach ($cart->cartItems as $cartItem) { |
||
| 51 | if (!in_array($cartItem->id, $ids)) { |
||
| 52 | $cartItem->delete(); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | $cart = Ecommerce\Cart::get($cart->id); |
||
| 56 | if (!$cart->cartItems) { |
||
| 57 | $error = true; |
||
| 58 | } |
||
| 59 | if (empty($this->module->config['sell_over_warehouse'])) { |
||
| 60 | foreach ($cart->cartItems as $cartitem) { |
||
| 61 | $warecount = $cartitem->price->offer->warehouseCount($cart->id); |
||
| 62 | if ($cartitem->count > $warecount) { |
||
| 63 | $error = true; |
||
| 64 | Msg::add('Вы заказали <b>' . $cartitem->item->name . '</b> больше чем есть на складе. на складе: <b>' . $warecount . '</b>', 'danger'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | if ($deliverys && empty($deliverys[$_POST['delivery']])) { |
||
|
|
|||
| 69 | $error = 1; |
||
| 70 | Msg::add('Выберите способ доставки', 'danger'); |
||
| 71 | } elseif ($deliverys && !empty($deliverys[$_POST['delivery']])) { |
||
| 72 | $cart->delivery_id = $_POST['delivery']; |
||
| 73 | foreach ($deliverys[$cart->delivery_id]->fields as $field) { |
||
| 74 | View Code Duplication | if (empty($_POST['deliveryFields'][$field->id]) && $field->required) { |
|
| 75 | $error = 1; |
||
| 76 | Msg::add('Вы не указали: ' . $field->name, 'danger'); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | if ($payTypes && (empty($_POST['payType']) || empty($payTypes[$_POST['payType']]))) { |
||
| 81 | $error = 1; |
||
| 82 | Msg::add('Выберите способ оплаты', 'danger'); |
||
| 83 | } elseif ($payTypes && !empty($payTypes[$_POST['payType']])) { |
||
| 84 | $payType = $payTypes[$_POST['payType']]; |
||
| 85 | $cart->paytype_id = $payType->id; |
||
| 86 | } else { |
||
| 87 | $payType = null; |
||
| 88 | } |
||
| 89 | foreach (\Ecommerce\UserAdds\Field::getList() as $field) { |
||
| 90 | View Code Duplication | if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) { |
|
| 91 | $error = 1; |
||
| 92 | Msg::add('Вы не указали: ' . $field->name, 'danger'); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | if (!empty($_POST['discounts']['card_item_id'])) { |
||
| 96 | $userCard = \Ecommerce\Card\Item::get((int)$_POST['discounts']['card_item_id']); |
||
| 97 | if (!$userCard) { |
||
| 98 | $error = true; |
||
| 99 | Msg::add('Такой карты не существует', 'danger'); |
||
| 100 | } elseif ($userCard->user_id != $user->id) { |
||
| 101 | $error = true; |
||
| 102 | Msg::add('Это не ваша карта', 'danger'); |
||
| 103 | } else { |
||
| 104 | $cart->card_item_id = $userCard->id; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | $cart->save(); |
||
| 108 | if (!$error && !empty($_POST['action']) && $_POST['action'] = 'order') { |
||
| 109 | $cart->user_id = $user->user_id; |
||
| 110 | $this->module->parseFields($_POST['userAdds']['fields'], $cart); |
||
| 111 | if ($deliverys && !empty($deliverys[$cart->delivery_id]) && !empty($_POST['deliveryFields'])) { |
||
| 112 | $this->module->parseDeliveryFields($_POST['deliveryFields'], $cart, $deliverys[$cart->delivery_id]->fields); |
||
| 113 | } |
||
| 114 | $cart->cart_status_id = 2; |
||
| 115 | $cart->comment = !empty($_POST['comment']) ? htmlspecialchars($_POST['comment']) : ''; |
||
| 116 | $cart->date_status = date('Y-m-d H:i:s'); |
||
| 117 | $cart->complete_data = date('Y-m-d H:i:s'); |
||
| 118 | $cart->warehouse_block = 1; |
||
| 119 | $cart->save(); |
||
| 120 | |||
| 121 | $cart = \Ecommerce\Cart::get($cart->id); |
||
| 122 | foreach ($cart->cartItems as $cartItem) { |
||
| 123 | $cartItem->discount = $cartItem->discount(); |
||
| 124 | $cartItem->final_price = $cartItem->price->price - $cartItem->discount; |
||
| 125 | $cartItem->save(); |
||
| 126 | } |
||
| 127 | $cart = \Ecommerce\Cart::get($cart->id); |
||
| 128 | if (!empty(\App::$cur->ecommerce->config['notify_mail'])) { |
||
| 129 | $text = 'Перейдите в админ панель чтобы просмотреть новый заказ <a href = "http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/admin/ecommerce/Cart">Админ панель</a>'; |
||
| 130 | $title = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
||
| 131 | \Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, \App::$cur->ecommerce->config['notify_mail'], $title, $text); |
||
| 132 | } |
||
| 133 | if ($this->notifications) { |
||
| 134 | $notification = new Notifications\Notification(); |
||
| 135 | $notification->name = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
||
| 136 | $notification->text = 'Перейдите в админ панель чтобы просмотреть новый заказ'; |
||
| 137 | $notification->chanel_id = $this->notifications->getChanel('Ecommerce-orders')->id; |
||
| 138 | $notification->save(); |
||
| 139 | } |
||
| 140 | $handlers = $this->ecommerce->getSnippets('payTypeHandler'); |
||
| 141 | $redirect = ['/ecommerce/cart/success']; |
||
| 142 | if ($payType && !empty($handlers[$payType->handler]['handler'])) { |
||
| 143 | $newRedirect = $handlers[$payType->handler]['handler']($cart); |
||
| 144 | if (!empty($newRedirect)) { |
||
| 145 | $redirect = $newRedirect; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | unset($_SESSION['cart']['cart_id']); |
||
| 149 | call_user_func_array(['Tools', 'redirect'], $redirect); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | $this->view->setTitle('Корзина'); |
||
| 154 | $bread = []; |
||
| 155 | $bread[] = [ |
||
| 156 | 'text' => 'Каталог', |
||
| 157 | 'href' => '/ecommerce' |
||
| 158 | ]; |
||
| 159 | $bread[] = [ |
||
| 160 | 'text' => 'Корзина', |
||
| 161 | 'href' => '/ecommerce/cart' |
||
| 162 | ]; |
||
| 163 | $this->view->page(['data' => compact('cart', 'items', 'deliverys', 'payTypes', 'packItem', 'bread')]); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function orderDetailAction($id = 0) { |
||
| 167 | $cart = Ecommerce\Cart::get((int)$id); |
||
| 168 | if ($cart->user_id != Users\User::$cur->id) { |
||
| 169 | $this->url->redirect('/', 'Это не ваша корзина'); |
||
| 170 | } |
||
| 171 | $bread = []; |
||
| 172 | $bread[] = [ |
||
| 173 | 'text' => 'Каталог', |
||
| 174 | 'href' => '/ecommerce' |
||
| 175 | ]; |
||
| 176 | $bread[] = [ |
||
| 177 | 'text' => 'Корзина', |
||
| 178 | 'href' => '/ecommerce/cart' |
||
| 179 | ]; |
||
| 180 | $bread[] = [ |
||
| 181 | 'text' => 'Заказ: №' . $cart->id, |
||
| 182 | 'href' => '/ecommerce/cart/orderDetail/' . $cart->id |
||
| 183 | ]; |
||
| 184 | $this->view->setTitle('Заказ №' . $cart->id); |
||
| 185 | $this->view->page(['data' => compact('cart', 'bread')]); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function continueAction($id = 0) { |
||
| 199 | |||
| 200 | public function deleteAction($id = 0) { |
||
| 214 | |||
| 215 | public function refillAction($id = 0) { |
||
| 232 | |||
| 233 | public function successAction() { |
||
| 250 | |||
| 251 | public function addAction() { |
||
| 252 | $result = new Server\Result(); |
||
| 306 | |||
| 307 | public function deleteItemAction() { |
||
| 329 | |||
| 330 | public function getcartAction() { |
||
| 338 | |||
| 339 | } |
||
| 340 |
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.