DistanceShippingMethod   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 30 3
A calculateRate() 0 7 1
A getDistanceFare() 0 17 3
A greatestCostDistance() 0 6 1
A requiresAddress() 0 4 1
1
<?php
2
3
/**
4
 * @package silvershop-shipping
5
 */
6
class DistanceShippingMethod extends ShippingMethod
7
{
8
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
9
        'Name' => 'Distance Shipping',
10
        'Description' => 'Per product shipping'
11
    );
12
13
    private static $has_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
        "DistanceFares" => "DistanceShippingFare"
15
    );
16
17
    public function getCMSFields()
18
    {
19
        $fields = parent::getCMSFields();
20
        $fields->fieldByName('Root')->removeByName("DistanceFares");
21
        if ($this->isInDB()) {
22
            $fields->addFieldToTab("Root.Main", $gridfield = GridField::create(
23
                "DistanceFares", "Fares",
24
                $this->DistanceFares(), $config = new GridFieldConfig_RecordEditor()
0 ignored issues
show
Documentation Bug introduced by
The method DistanceFares does not exist on object<DistanceShippingMethod>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
25
            ));
26
            $config->removeComponentsByType("GridFieldDataColumns");
27
            $config->removeComponentsByType("GridFieldEditButton");
28
            $config->removeComponentsByType("GridFieldDeleteAction");
29
            $config->removeComponentsByType("GridFieldAddNewButton");
30
            $config->addComponent($cols = new GridFieldEditableColumns());
31
            $config->addComponent(new GridFieldDeleteAction());
32
            $config->addComponent($addnew = new GridFieldAddNewInlineButton());
33
            $addnew->setTitle($addnew->getTitle()." Fare");
34
            if ($greatest = $this->greatestCostDistance()) {
0 ignored issues
show
Unused Code introduced by
$greatest 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...
35
                $fields->insertAfter(
36
                    LiteralField::create("costnote",
37
                        "<p class=\"message\">Distances beyond the greatest specified distance will be cost ".
38
                            $this->greatestCostDistance()->dbObject("Cost")->Nice().
39
                        " (the most expensive fare)</p>"
40
                    ), "DistanceFares"
0 ignored issues
show
Documentation introduced by
'DistanceFares' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
                );
42
            }
43
        }
44
45
        return $fields;
46
    }
47
48
    public function calculateRate(ShippingPackage $package, Address $address)
49
    {
50
        $warehouse = Warehouse::closest_to($address);
51
        $distance = $warehouse->Address()->distanceTo($address);
0 ignored issues
show
Documentation Bug introduced by
The method Address does not exist on object<Warehouse>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
52
53
        return $this->getDistanceFare($distance);
54
    }
55
56
    public function getDistanceFare($distance)
57
    {
58
        $cost = 0;
59
        $fare = $this->DistanceFares()
0 ignored issues
show
Documentation Bug introduced by
The method DistanceFares does not exist on object<DistanceShippingMethod>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60
            ->filter("Distance:GreaterThan", 0)
61
            ->filter("Distance:GreaterThan", $distance)
62
            ->sort("Distance", "ASC")
63
            ->first();
64
        if (!$fare) {
65
            $fare = $this->greatestCostDistance();
66
        }
67
        if ($fare->exists()) {
68
            $cost = $fare->Cost;
69
        }
70
71
        return $cost;
72
    }
73
74
    public function greatestCostDistance()
75
    {
76
        return $this->DistanceFares()
0 ignored issues
show
Documentation Bug introduced by
The method DistanceFares does not exist on object<DistanceShippingMethod>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
                ->sort("Cost", "DESC")
78
                ->first();
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function requiresAddress()
85
    {
86
        return true;
87
    }
88
}
89
90
class DistanceShippingFare extends DataObject
91
{
92
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
93
        'Distance' => 'Float',
94
        'Cost' => 'Currency'
95
    );
96
97
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
98
        'ShippingMethod' => 'DistanceShippingMethod'
99
    );
100
101
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
102
        'MinDistance',
103
        'Distance',
104
        'Cost'
105
    );
106
107
    private static $field_labels = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
108
        'MinDistance' => 'Min Distance (km)',
109
        'Distance' => 'Max Distance (km)',
110
        'Cost' => 'Cost'
111
    );
112
113
    private static $singular_name = "Fare";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
114
115
    private static $default_sort = "\"Distance\" ASC";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
116
117
    public function getMinDistance()
118
    {
119
        $dist = 0;
120
        if (
121
            $dfare = self::get()
122
            ->filter("Distance:LessThan", $this->Distance)
0 ignored issues
show
Documentation introduced by
The property Distance does not exist on object<DistanceShippingFare>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
            ->filter("ShippingMethodID", $this->ShippingMethodID)
0 ignored issues
show
Documentation introduced by
The property ShippingMethodID does not exist on object<DistanceShippingFare>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
            ->sort("Distance", "DESC")
125
            ->first()
126
        ) {
127
            $dist = $dfare->Distance;
128
        }
129
130
        return $dist;
131
    }
132
}
133