Conditions | 15 |
Paths | 24 |
Total Lines | 53 |
Code Lines | 37 |
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 |
||
23 | static function calcPrice($cart) { |
||
24 | $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek(); |
||
25 | $fields = []; |
||
26 | $cityId = 0; |
||
27 | $senderCity = 44; |
||
28 | foreach ($cart->delivery->fields as $field) { |
||
29 | if ($field->code === 'city' && !empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
||
30 | $item = Item::get([['id', $_POST['deliveryFields'][$field->id]], ['delivery_field_id', $field->id]]); |
||
31 | if ($item) { |
||
32 | $cityId = json_decode($item->data, true)['ID']; |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | if ($cityId == 278) { |
||
37 | $senderCity = 278; |
||
38 | foreach ($cart->cartItems as $cartItem) { |
||
39 | if ($cartItem->item->offers(['key' => false]) && $cartItem->item->offers(['key' => false])[0]->warehouses) { |
||
40 | $msocow = 0; |
||
41 | $kras = 0; |
||
42 | foreach ($cartItem->item->offers(['key' => false])[0]->warehouses as $warehouse) { |
||
43 | if ($warehouse->warehouse_id != 6) { |
||
44 | $kras += $warehouse->count; |
||
45 | } else { |
||
46 | $msocow += $warehouse->count; |
||
47 | } |
||
48 | |||
49 | } |
||
50 | if ($kras < $cartItem->count) { |
||
51 | $senderCity = 44; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | } else { |
||
56 | $senderCity = 44; |
||
57 | } |
||
58 | if ($cityId) { |
||
59 | $config = ConfigItem::getList(['where' => ['delivery_provider_id', $cart->delivery->delivery_provider_id], 'key' => 'name']); |
||
60 | $calc->setAuth($config['authLogin']->value, $config['authPassword']->value); |
||
61 | $calc->setDateExecute(date('Y-m-d H:i:s')); |
||
62 | $calc->setSenderCityId($senderCity); |
||
63 | //устанавливаем город-получатель |
||
64 | $calc->setReceiverCityId($cityId); |
||
65 | $calc->setTariffId('136'); |
||
66 | $calc->addGoodsItemBySize(3, 25, 25, 24); |
||
67 | if ($calc->calculate()) { |
||
68 | return new \Money\Sums([$cart->delivery->currency_id => $calc->getResult()['result']['price']]); |
||
69 | } else { |
||
70 | //var_dump($calc->getError()); |
||
71 | } |
||
72 | |||
73 | } |
||
74 | return new \Money\Sums([$cart->delivery->currency_id => 0]); |
||
75 | } |
||
76 | } |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.