Conditions | 11 |
Paths | 45 |
Total Lines | 44 |
Code Lines | 30 |
Lines | 9 |
Ratio | 20.45 % |
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 |
||
19 | static function request($cart) { |
||
20 | $city = ''; |
||
21 | View Code Duplication | foreach ($cart->delivery->fields as $field) { |
|
22 | if ($field->code === 'index') { |
||
23 | if (!empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
||
24 | $city = $_POST['deliveryFields'][$field->id]; |
||
25 | } elseif (isset($cart->deliveryInfos[$field->id])) { |
||
26 | $city = $cart->deliveryInfos[$field->id]->value; |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | if (!$city) { |
||
31 | $fieldInfo = Field::get('deliveryfield_city', 'code'); |
||
32 | $field = \Ecommerce\Delivery\Field::get('city', 'code'); |
||
33 | if (isset($cart->infos[$fieldInfo->id])) { |
||
34 | $item = \Ecommerce\Delivery\Field\Item::get([['id', $cart->infos[$fieldInfo->id]->value], ['delivery_field_id', $field->id]]); |
||
35 | if ($item) { |
||
36 | $data = json_decode($item->data, true); |
||
37 | if (!empty($data['PostCodeList'])) { |
||
38 | $city = explode(',', $data['PostCodeList'])[0]; |
||
39 | } |
||
40 | |||
41 | } |
||
42 | } |
||
43 | if (!$city) { |
||
44 | return []; |
||
45 | } |
||
46 | } |
||
47 | $senderCity = '101000'; |
||
48 | |||
49 | $data = [ |
||
50 | 'object' => 4030, |
||
51 | 'weight' => '1000', |
||
52 | 'date' => date('Ymd'), |
||
53 | //'sumoc' => $cart->itemsSum()->sums[0], |
||
54 | 'from' => $senderCity, |
||
55 | 'to' => $city, |
||
56 | 'delivery' => 1, |
||
57 | ]; |
||
58 | $result = \Cache::get('russianPostCalc', $data, function ($data) { |
||
59 | return file_get_contents('http://tariff.russianpost.ru/tariff/v1/calculate?json&' . http_build_query($data)); |
||
60 | }); |
||
61 | return json_decode($result, true); |
||
62 | } |
||
63 | |||
83 | } |
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.