| Conditions | 22 |
| Paths | 90 |
| Total Lines | 76 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 76 | public function updateCartItems($cart, $items) { |
||
| 77 | $itemIds = []; |
||
| 78 | $cItems = []; |
||
| 79 | foreach ($items as $item) { |
||
| 80 | $cItem = []; |
||
| 81 | $id = \Migrations\Id::get([['parse_id', $item->Ид], ['type', 'Ecommerce\Item']]); |
||
| 82 | if (!$id) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | $itemIds[] = $id->object_id; |
||
| 86 | $siteItem = \Ecommerce\Item::get($id->object_id); |
||
| 87 | |||
| 88 | if (!$siteItem) { |
||
| 89 | $cItem['item_id'] = 0; |
||
| 90 | $cItem['item_offer_price_id'] = 0; |
||
| 91 | $cItem['count'] = (string) $item->Количество; |
||
| 92 | $cItem['name'] = (string) $item->Наименование; |
||
| 93 | |||
| 94 | $cItems[] = $cItem; |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $pricesByPrice = $siteItem->prices(['key' => 'price']); |
||
| 99 | $prices = $siteItem->prices; |
||
| 100 | $default = key($prices); |
||
| 101 | $itemPrice = number_format((string) $item->ЦенаЗаЕдиницу, 2, '.', ''); |
||
| 102 | if (!empty($pricesByPrice[$itemPrice])) { |
||
| 103 | $price = $pricesByPrice[$itemPrice]; |
||
| 104 | } else { |
||
| 105 | $rolePrice = 0; |
||
| 106 | foreach ($siteItem->prices as $priceId => $itemPrice) { |
||
| 107 | if (!$itemPrice->type->cipt_roles) { |
||
| 108 | $default = $priceId; |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | if ($itemPrice->type->cipt_roles && $cart->user->user_role_id && false !== strpos($itemPrice->type->cipt_roles, "|{$cart->user->user_role_id}|")) { |
||
| 112 | $rolePrice = $priceId; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | $price = $siteItem->prices[($rolePrice) ? $rolePrice : $default]; |
||
| 116 | } |
||
| 117 | |||
| 118 | $cItem['item_id'] = $id->object_id; |
||
| 119 | $cItem['item_offer_price_id'] = $price->id; |
||
| 120 | $cItem['count'] = (string) $item->Количество; |
||
| 121 | $cItem['final_price'] = (string) $item->ЦенаЗаЕдиницу; |
||
| 122 | $cItem['name'] = (string) $item->Наименование; |
||
| 123 | |||
| 124 | $cItems[] = $cItem; |
||
| 125 | } |
||
| 126 | foreach ($cart->cartItems as $cartItem) { |
||
| 127 | $isset = false; |
||
| 128 | foreach ($cItems as $key => $cItem) { |
||
| 129 | if (!($cItem['item_id'] == $cartItem->item_id)) { |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | $isset = true; |
||
| 133 | if ($cItem['final_price'] != $cartItem->final_price || $cItem['item_offer_price_id'] != $cartItem->item_offer_price_id || $cItem['count'] != $cartItem->count) { |
||
| 134 | $cartItem->item_offer_price_id = $cItem['item_offer_price_id']; |
||
| 135 | $cartItem->count = $cItem['count']; |
||
| 136 | $cartItem->final_price = $cItem['final_price']; |
||
| 137 | $cartItem->save(); |
||
| 138 | } |
||
| 139 | unset($cItems[$key]); |
||
| 140 | } |
||
| 141 | if (!$isset && !empty($cItem['name']) && !in_array($cItem['name'], ['Доставка', 'Клубная карта', 'Пакет майка'])) { |
||
| 142 | $cartItem->delete(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | if ($cItems) { |
||
| 146 | foreach ($cItems as $cItem) { |
||
| 147 | $cart->addItem($cItem['item_offer_price_id'], $cItem['count'], $cItem['final_price']); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | $cart->calc(); |
||
| 151 | } |
||
| 152 | |||
| 154 |