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