Test Failed
Push — master ( 19c6b2...92125e )
by Alexey
09:11 queued 04:40
created

CDEK::calcPrice()   C

Complexity

Conditions 15
Paths 24

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 37
nc 24
nop 1
dl 0
loc 53
rs 6.2939
c 0
b 0
f 0

How to fix   Long Method    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
16
class CDEK extends \Ecommerce\DeliveryProvider {
17
    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...
18
19
    /**
20
     * @param \Ecommerce\Cart $cart
21
     * @return \Money\Sums
22
     */
23
    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...
24
        $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek();
25
        $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...
26
        $cityId = 0;
27
        $senderCity = 44;
0 ignored issues
show
Unused Code introduced by
$senderCity 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...
28
        foreach ($cart->delivery->fields as $field) {
29
            if ($field->code === 'city' && !empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) {
30
                $item = Item::get([['id', $_POST['deliveryFields'][$field->id]], ['delivery_field_id', $field->id]]);
31
                if ($item) {
32
                    $cityId = json_decode($item->data, true)['ID'];
33
                }
34
            }
35
        }
36
        if ($cityId == 278) {
37
            $senderCity = 278;
38
            foreach ($cart->cartItems as $cartItem) {
39
                if ($cartItem->item->offers(['key' => false]) && $cartItem->item->offers(['key' => false])[0]->warehouses) {
0 ignored issues
show
Unused Code introduced by
The call to Item::offers() has too many arguments starting with array('key' => false).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug Best Practice introduced by
The expression $cartItem->item->offers(... false))[0]->warehouses of type Ecommerce\Item\Offer\Warehouse[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
                    $msocow = 0;
41
                    $kras = 0;
42
                    foreach ($cartItem->item->offers(['key' => false])[0]->warehouses as $warehouse) {
0 ignored issues
show
Unused Code introduced by
The call to Item::offers() has too many arguments starting with array('key' => false).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
                        if ($warehouse->warehouse_id != 6) {
44
                            $kras += $warehouse->count;
45
                        } else {
46
                            $msocow += $warehouse->count;
47
                        }
48
49
                    }
50
                    if ($kras < $cartItem->count) {
51
                        $senderCity = 44;
52
                    }
53
                }
54
            }
55
        } else {
56
            $senderCity = 44;
57
        }
58
        if ($cityId) {
59
            $config = ConfigItem::getList(['where' => ['delivery_provider_id', $cart->delivery->delivery_provider_id], 'key' => 'name']);
60
            $calc->setAuth($config['authLogin']->value, $config['authPassword']->value);
61
            $calc->setDateExecute(date('Y-m-d H:i:s'));
62
            $calc->setSenderCityId($senderCity);
63
            //устанавливаем город-получатель
64
            $calc->setReceiverCityId($cityId);
65
            $calc->setTariffId('136');
66
            $calc->addGoodsItemBySize(3, 25, 25, 24);
67
            if ($calc->calculate()) {
68
                return new \Money\Sums([$cart->delivery->currency_id => $calc->getResult()['result']['price']]);
69
            } 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...
70
                //var_dump($calc->getError());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
71
            }
72
73
        }
74
        return new \Money\Sums([$cart->delivery->currency_id => 0]);
75
    }
76
}