Customer   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 208
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
lcom 1
cbo 2
dl 16
loc 208
ccs 96
cts 96
cp 1
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setAddress() 0 4 1
A getAddress() 0 4 1
A setCompany() 0 4 1
A getCompany() 0 4 1
A setEmailAddress() 8 8 2
A getEmailAddress() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A setPhoneNumber() 8 8 2
A getPhoneNumber() 0 4 1
C toXML() 0 49 7
C createFromXMLHelper() 0 24 10

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
namespace Bpost\BpostApiClient\Bpost\Order;
3
4
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
5
6
/**
7
 * bPost Customer class
8
 *
9
 * @author Tijs Verkoyen <[email protected]>
10
 */
11
class Customer
12
{
13
    const TAG_NAME = 'customer';
14
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var string
22
     */
23
    private $company;
24
25
    /**
26
     * @var Address
27
     */
28
    private $address;
29
30
    /**
31
     * @var string
32
     */
33
    private $emailAddress;
34
35
    /**
36
     * @var string
37
     */
38
    private $phoneNumber;
39
40
    /**
41
     * @param \Bpost\BpostApiClient\Bpost\Order\Address $address
42
     */
43 12
    public function setAddress($address)
44
    {
45 12
        $this->address = $address;
46 12
    }
47
48
    /**
49
     * @return \Bpost\BpostApiClient\Bpost\Order\Address
50
     */
51 10
    public function getAddress()
52
    {
53 10
        return $this->address;
54
    }
55
56
    /**
57
     * @param string $company
58
     */
59 12
    public function setCompany($company)
60
    {
61 12
        $this->company = $company;
62 12
    }
63
64
    /**
65
     * @return string
66
     */
67 12
    public function getCompany()
68
    {
69 12
        return $this->company;
70
    }
71
72
    /**
73
     * @param string $emailAddress
74
     * @throws BpostInvalidLengthException
75
     */
76 15 View Code Duplication
    public function setEmailAddress($emailAddress)
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...
77
    {
78 15
        $length = 50;
79 15
        if (mb_strlen($emailAddress) > $length) {
80 3
            throw new BpostInvalidLengthException('emailAddress', mb_strlen($emailAddress), $length);
81
        }
82 12
        $this->emailAddress = $emailAddress;
83 12
    }
84
85
    /**
86
     * @return string
87
     */
88 10
    public function getEmailAddress()
89
    {
90 10
        return $this->emailAddress;
91
    }
92
93
    /**
94
     * @param string $name
95
     */
96 12
    public function setName($name)
97
    {
98 12
        $this->name = $name;
99 12
    }
100
101
    /**
102
     * @return string
103
     */
104 10
    public function getName()
105
    {
106 10
        return $this->name;
107
    }
108
109
    /**
110
     * @param string $phoneNumber
111
     * @throws BpostInvalidLengthException
112
     */
113 15 View Code Duplication
    public function setPhoneNumber($phoneNumber)
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...
114
    {
115 15
        $length = 20;
116 15
        if (mb_strlen($phoneNumber) > $length) {
117 3
            throw new BpostInvalidLengthException('phoneNumber', mb_strlen($phoneNumber), $length);
118
        }
119 12
        $this->phoneNumber = $phoneNumber;
120 12
    }
121
122
    /**
123
     * @return string
124
     */
125 10
    public function getPhoneNumber()
126
    {
127 10
        return $this->phoneNumber;
128
    }
129
130
    /**
131
     * Return the object as an array for usage in the XML
132
     *
133
     * @param \DomDocument $document
134
     * @param  string      $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
135
     * @return \DomElement
136
     */
137 9
    public function toXML(\DomDocument $document, $prefix = null)
138
    {
139 9
        $tagName = static::TAG_NAME;
140 9
        if ($prefix !== null) {
141 3
            $tagName = $prefix . ':' . $tagName;
142 3
        }
143
144 9
        $customer = $document->createElement($tagName);
145
146 9
        if ($this->getName() !== null) {
147 9
            $customer->appendChild(
148 9
                $document->createElement(
149 9
                    'common:name',
150 9
                    $this->getName()
151 9
                )
152 9
            );
153 9
        }
154 9
        if ($this->getCompany() !== null) {
155 9
            $customer->appendChild(
156 9
                $document->createElement(
157 9
                    'common:company',
158 9
                    $this->getCompany()
159 9
                )
160 9
            );
161 9
        }
162 9
        if ($this->getAddress() !== null) {
163 9
            $customer->appendChild(
164 9
                $this->getAddress()->toXML($document)
165 9
            );
166 9
        }
167 9
        if ($this->getEmailAddress() !== null) {
168 9
            $customer->appendChild(
169 9
                $document->createElement(
170 9
                    'common:emailAddress',
171 9
                    $this->getEmailAddress()
172 9
                )
173 9
            );
174 9
        }
175 9
        if ($this->getPhoneNumber() !== null) {
176 9
            $customer->appendChild(
177 9
                $document->createElement(
178 9
                    'common:phoneNumber',
179 9
                    $this->getPhoneNumber()
180 9
                )
181 9
            );
182 9
        }
183
184 9
        return $customer;
185
    }
186
187
    /**
188
     * @param  \SimpleXMLElement $xml
189
     * @param  Customer          $instance
190
     *
191
     * @return Customer
192
     * @throws BpostInvalidLengthException
193
     */
194 3
    public static function createFromXMLHelper(\SimpleXMLElement $xml, Customer $instance)
195
    {
196 3
        if (isset($xml->name) && $xml->name != '') {
197 3
            $instance->setName((string) $xml->name);
198 3
        }
199 3
        if (isset($xml->company) && $xml->company != '') {
200 3
            $instance->setCompany((string) $xml->company);
201 3
        }
202 3
        if (isset($xml->address)) {
203 3
            $instance->setAddress(
204 3
                Address::createFromXML($xml->address)
205 3
            );
206 3
        }
207 3
        if (isset($xml->emailAddress) && $xml->emailAddress != '') {
208 3
            $instance->setEmailAddress(
209 3
                (string) $xml->emailAddress
210 3
            );
211 3
        }
212 3
        if (isset($xml->phoneNumber) && $xml->phoneNumber != '') {
213 3
            $instance->setPhoneNumber((string) $xml->phoneNumber);
214 3
        }
215
216 3
        return $instance;
217
    }
218
}
219