Conditions | 10 |
Paths | 40 |
Total Lines | 48 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
43 | static function calcPrice($cart) { |
||
44 | |||
45 | |||
46 | $sessionId = \Cache::get('PickPointSession', []); |
||
47 | if (!$sessionId) { |
||
48 | $config = ConfigItem::getList(['where' => ['delivery_provider_id', $cart->delivery->delivery_provider_id], 'key' => 'name']); |
||
49 | $result = self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/login', [ |
||
50 | 'Login' => $config['login']->value, |
||
51 | 'Password' => $config['pass']->value, |
||
52 | ]); |
||
53 | $sessionId = json_decode($result, true)['SessionId']; |
||
54 | \Cache::set('PickPointSession', $sessionId, [], 12 * 60 * 60); |
||
55 | } |
||
56 | $city = ''; |
||
57 | foreach ($cart->delivery->fields as $field) { |
||
58 | if ($field->code === 'index' && !empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
||
59 | $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/postindexpostamatlist', ['PostIndex' => $_POST['deliveryFields'][$field->id]]), true); |
||
60 | //print_r($result['PostamatList'][0]); |
||
61 | if (!empty($result['PostamatList'][0]['CitiName'])) { |
||
62 | $city = $result['PostamatList'][0]['CitiName']; |
||
63 | $toId = $result['PostamatList'][0]['Number']; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | if (!$city) { |
||
68 | return new \Money\Sums([$cart->delivery->currency_id => 0]); |
||
69 | } |
||
70 | if ($city === 'Красноярск') { |
||
71 | $senderCity = 'Красноярск'; |
||
72 | } else { |
||
73 | $senderCity = 'Москва'; |
||
74 | } |
||
75 | $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/calctariff', [ |
||
76 | 'SessionId' => $sessionId, |
||
77 | 'FromCity' => $senderCity, |
||
78 | 'IKN' => $config['ikn']->value, |
||
79 | 'PTNumber' => $toId, |
||
80 | 'Length' => 25, |
||
81 | 'Depth' => 25, |
||
82 | 'Width' => 25, |
||
83 | ]), true); |
||
84 | $summ = 0; |
||
85 | if (!empty($result['Services'][0]['Tariff'])) { |
||
86 | $summ = $result['Services'][0]['Tariff'] + $result['Services'][0]['NDS']; |
||
87 | } |
||
88 | |||
89 | return new \Money\Sums([$cart->delivery->currency_id => $summ]); |
||
90 | } |
||
91 | } |
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.