Conditions | 10 |
Paths | 36 |
Total Lines | 40 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
24 | static function calcPrice($cart) { |
||
25 | $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek(); |
||
26 | $fields = []; |
||
27 | $cityId = 0; |
||
28 | $senderCity = 44; |
||
29 | $tariff = 136; |
||
30 | $fieldInfo = Field::get('deliveryfield_city', 'code'); |
||
31 | $field = \Ecommerce\Delivery\Field::get('city', 'code'); |
||
32 | if (isset($cart->infos[$fieldInfo->id])) { |
||
33 | $item = Item::get([['id', $cart->infos[$fieldInfo->id]->value], ['delivery_field_id', $field->id]]); |
||
34 | if ($item) { |
||
35 | $cityId = json_decode($item->data, true)['ID']; |
||
36 | } |
||
37 | } |
||
38 | foreach ($cart->delivery->fields as $field) { |
||
39 | if ($field->code === 'cdektype' && !empty($_POST['deliveryFields'][$field->id]) && is_numeric($_POST['deliveryFields'][$field->id])) { |
||
40 | $item = Item::get([['id', $_POST['deliveryFields'][$field->id]], ['delivery_field_id', $field->id]]); |
||
41 | if ($item) { |
||
42 | $tariff = $item->data; |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | if ($cityId) { |
||
47 | $config = ConfigItem::getList(['where' => ['delivery_provider_id', $cart->delivery->delivery_provider_id], 'key' => 'name']); |
||
48 | $calc->setAuth($config['authLogin']->value, $config['authPassword']->value); |
||
49 | $calc->setDateExecute(date('Y-m-d H:i:s')); |
||
50 | $calc->setSenderCityId($senderCity); |
||
51 | //устанавливаем город-получатель |
||
52 | $calc->setReceiverCityId($cityId); |
||
53 | $calc->setTariffId($tariff); |
||
54 | $calc->addGoodsItemBySize(3, 25, 25, 24); |
||
55 | if ($calc->calculate()) { |
||
56 | return new \Money\Sums([$cart->delivery->currency_id => $calc->getResult()['result']['price']]); |
||
57 | } else { |
||
58 | //var_dump($tariff,$calc->getError()); |
||
59 | } |
||
60 | |||
61 | } |
||
62 | return new \Money\Sums([$cart->delivery->currency_id => 0]); |
||
63 | } |
||
64 | } |
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.