| Conditions | 47 |
| Paths | > 20000 |
| Total Lines | 152 |
| Code Lines | 118 |
| Lines | 8 |
| Ratio | 5.26 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 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.