DeliveryMethod   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 20
loc 105
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromXML() 20 20 3
A getName() 0 4 1
A setName() 0 4 1
A getVisibility() 0 4 1
A isVisibleAndActive() 0 4 1
A setVisibility() 0 4 1
A getProducts() 0 4 1
A addProduct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
4
5
use SimpleXMLElement;
6
7
/**
8
 * Class DeliveryMethod
9
 * @package Bpost\BpostApiClient\Bpost\ProductConfiguration
10
 */
11
class DeliveryMethod
12
{
13
    const DELIVERY_METHOD_NAME_HOME_OR_OFFICE = 'home or office';
14
    const DELIVERY_METHOD_NAME_PICKUP_POINT = 'pick-up point';
15
    const DELIVERY_METHOD_NAME_PARCEL_LOCKER = 'parcel locker';
16
    const DELIVERY_METHOD_NAME_CLICK_AND_COLLECT = 'Click & Collect';
17
18
    const DELIVERY_METHOD_VISIBILITY_VISIBLE = 'VISIBLE';
19
    const DELIVERY_METHOD_VISIBILITY_GREYED_OUT = 'GREYED_OUT';
20
    const DELIVERY_METHOD_VISIBILITY_INVISIBLE = 'INVISIBLE';
21
22
    /** @var  string */
23
    private $name;
24
    /** @var  string */
25
    private $visibility;
26
    /** @var Product[] */
27
    private $products = array();
28
29
    /**
30
     * @param SimpleXMLElement $xml
31
     *
32
     * @return DeliveryMethod
33
     */
34 View Code Duplication
    public static function createFromXML(SimpleXMLElement $xml)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        /*
37
        <characteristic displayValue="Basic (0-500 EUR)" value="1" name="Insurance range code"/>
38
        */
39
        $attributes = $xml->attributes();
40
        $children = $xml->children();
41
42
        $deliveryMethod = new self();
43
        $deliveryMethod->setName($attributes['name']);
44
        $deliveryMethod->setVisibility($attributes['visiblity']);
45
46
        if (isset($children->product)) {
47
            foreach ($children->product as $productXml) {
48
                $deliveryMethod->addProduct(Product::createFromXML($productXml));
49
            }
50
        }
51
52
        return $deliveryMethod;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getName()
59
    {
60
        return $this->name;
61
    }
62
63
    /**
64
     * @param string $name
65
     */
66
    public function setName($name)
67
    {
68
        $this->name = (string)$name;
69
    }
70
71
    /**
72
     * @return string
73
     * @see Constants self::VISIBLITY_*
74
     */
75
    public function getVisibility()
76
    {
77
        return $this->visibility;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function isVisibleAndActive()
84
    {
85
        return $this->getVisibility() === self::DELIVERY_METHOD_VISIBILITY_VISIBLE;
86
    }
87
88
    /**
89
     * @param string $visibility
90
     *
91
     * @see Constants self::VISIBLITY_*
92
     */
93
    public function setVisibility($visibility)
94
    {
95
        $this->visibility = (string)$visibility;
96
    }
97
98
    /**
99
     * @return Product[]
100
     */
101
    public function getProducts()
102
    {
103
        return $this->products;
104
    }
105
106
    /**
107
     * @param Product $product
108
     */
109
    public function addProduct(Product $product)
110
    {
111
        $this->products[] = $product;
112
    }
113
114
115
}
116