Test Failed
Push — master ( 1aeff4...14c3d9 )
by Alexey
05:16
created

CDEK::calcPrice()   D

Complexity

Conditions 10
Paths 36

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 29
nc 36
nop 1
dl 0
loc 40
rs 4.8196
c 2
b 1
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * INJI
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2017 Alexey Krupskiy
8
 * @license https://github.com/injitools/Inji/blob/master/LICENSE
9
 */
10
11
namespace Ecommerce\DeliveryProvider;
12
13
use Ecommerce\Delivery\Field\Item;
14
use Ecommerce\Delivery\Provider\ConfigItem;
15
use Ecommerce\UserAdds\Field;
16
17
class CDEK extends \Ecommerce\DeliveryProvider {
18
    static $name = 'СДЭК - курьерская служба';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
19
20
    /**
21
     * @param \Ecommerce\Cart $cart
22
     * @return \Money\Sums
23
     */
24
    static function calcPrice($cart) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
        $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek();
26
        $fields = [];
0 ignored issues
show
Unused Code introduced by
$fields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
58
                //var_dump($tariff,$calc->getError());
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
            }
60
61
        }
62
        return new \Money\Sums([$cart->delivery->currency_id => 0]);
63
    }
64
}