Conditions | 15 |
Paths | 24 |
Total Lines | 52 |
Code Lines | 36 |
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 |
||
22 | static function calcPrice($cart) { |
||
23 | $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek(); |
||
24 | $fields = []; |
||
25 | $cityId = 0; |
||
26 | $senderCity = 44; |
||
27 | foreach ($cart->delivery->fields as $field) { |
||
28 | if ($field->code === 'city' && !empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
||
29 | $item = Item::get([['id', $_POST['deliveryFields'][$field->id]], ['delivery_field_id', $field->id]]); |
||
30 | if ($item) { |
||
31 | $cityId = json_decode($item->data, true)['ID']; |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | if ($cityId == 278) { |
||
36 | $senderCity = 278; |
||
37 | foreach ($cart->cartItems as $cartItem) { |
||
38 | if ($cartItem->item->offers(['key' => false]) && $cartItem->item->offers(['key' => false])[0]->warehouses) { |
||
39 | $msocow = 0; |
||
40 | $kras = 0; |
||
41 | foreach ($cartItem->item->offers(['key' => false])[0]->warehouses as $warehouse) { |
||
42 | if ($warehouse->warehouse_id != 6) { |
||
43 | $kras += $warehouse->count; |
||
44 | } else { |
||
45 | $msocow += $warehouse->count; |
||
46 | } |
||
47 | |||
48 | } |
||
49 | if ($kras < $cartItem->count) { |
||
50 | $senderCity = 44; |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | } else { |
||
55 | $senderCity = 44; |
||
56 | } |
||
57 | if ($cityId) { |
||
58 | $calc->setAuth('5f0f6e81f9d39424f6286d22d81a0094', '77b002fb1c124fbe71eee000416e4963'); |
||
59 | $calc->setDateExecute(date('Y-m-d H:i:s')); |
||
60 | $calc->setSenderCityId($senderCity); |
||
61 | //устанавливаем город-получатель |
||
62 | $calc->setReceiverCityId($cityId); |
||
63 | $calc->setTariffId('136'); |
||
64 | $calc->addGoodsItemBySize(3, 25, 25, 24); |
||
65 | if ($calc->calculate()) { |
||
66 | return new \Money\Sums([$cart->delivery->currency_id => $calc->getResult()['result']['price']]); |
||
67 | } else { |
||
68 | //var_dump($calc->getError()); |
||
69 | } |
||
70 | |||
71 | } |
||
72 | return new \Money\Sums([$cart->delivery->currency_id => 0]); |
||
73 | } |
||
74 | } |
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.