Box   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 356
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 41
eloc 112
c 3
b 1
f 0
dl 0
loc 356
rs 9.1199

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setInternationalBox() 0 3 1
A getPossibleStatusValues() 0 13 1
A getNationalBox() 0 3 1
A setAdditionalCustomerReference() 0 3 1
A setStatus() 0 8 2
A toXML() 0 11 1
A setSender() 0 3 1
A getRemark() 0 3 1
A getInternationalBox() 0 3 1
A setBarcode() 0 3 1
A setNationalBox() 0 3 1
A getStatus() 0 3 1
A getAdditionalCustomerReference() 0 3 1
A getBarcode() 0 3 1
A getSender() 0 3 1
A setRemark() 0 3 1
A additionalCustomerReferenceToXML() 0 7 2
A barcodeToXML() 0 7 2
D createFromXML() 0 64 13
A remarkToXML() 0 7 2
A senderToXML() 0 5 2
A boxToXML() 0 10 3

How to fix   Complexity   

Complex Class

Complex classes like Box often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Box, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\Order;
4
5
use Bpost\BpostApiClient\Common\XmlHelper;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
7
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
8
use DOMDocument;
9
use DOMElement;
10
use SimpleXMLElement;
11
12
/**
13
 * bPost Box class
14
 *
15
 * @author Tijs Verkoyen <[email protected]>
16
 */
17
class Box
18
{
19
    const BOX_STATUS_OPEN = 'OPEN';
20
    const BOX_STATUS_PENDING = 'PENDING';
21
    const BOX_STATUS_PRINTED = 'PRINTED';
22
    const BOX_STATUS_CANCELLED = 'CANCELLED';
23
    const BOX_STATUS_ON_HOLD = 'ON-HOLD';
24
    const BOX_STATUS_ANNOUNCED = 'ANNOUNCED';
25
    const BOX_STATUS_IN_TRANSIT = 'IN_TRANSIT';
26
    const BOX_STATUS_AWAITING_PICKUP = 'AWAITING_PICKUP';
27
    const BOX_STATUS_DELIVERED = 'DELIVERED';
28
    const BOX_STATUS_BACK_TO_SENDER = 'BACK_TO_SENDER';
29
30
    /**
31
     * @var \Bpost\BpostApiClient\Bpost\Order\Sender
32
     */
33
    private $sender;
34
35
    /**
36
     * @var \Bpost\BpostApiClient\Bpost\Order\Box\AtHome
37
     */
38
    private $nationalBox;
39
40
    /**
41
     * @var \Bpost\BpostApiClient\Bpost\Order\Box\International
42
     */
43
    private $internationalBox;
44
45
    /**
46
     * @var string
47
     */
48
    private $remark;
49
50
    /**
51
     * @var string
52
     */
53
    private $status;
54
55
    /** @var string */
56
    private $barcode;
57
58
    /** @var string */
59
    private $additionalCustomerReference;
60
61
    /**
62
     * @param \Bpost\BpostApiClient\Bpost\Order\Box\International $internationalBox
63
     */
64
    public function setInternationalBox(Box\International $internationalBox)
65
    {
66
        $this->internationalBox = $internationalBox;
67
    }
68
69
    /**
70
     * @return \Bpost\BpostApiClient\Bpost\Order\Box\International
71
     */
72
    public function getInternationalBox()
73
    {
74
        return $this->internationalBox;
75
    }
76
77
    /**
78
     * @param \Bpost\BpostApiClient\Bpost\Order\Box\National $nationalBox
79
     */
80
    public function setNationalBox(Box\National $nationalBox)
81
    {
82
        $this->nationalBox = $nationalBox;
0 ignored issues
show
Documentation Bug introduced by
$nationalBox is of type Bpost\BpostApiClient\Bpost\Order\Box\National, but the property $nationalBox was declared to be of type Bpost\BpostApiClient\Bpost\Order\Box\AtHome. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
83
    }
84
85
    /**
86
     * @return \Bpost\BpostApiClient\Bpost\Order\Box\National
87
     */
88
    public function getNationalBox()
89
    {
90
        return $this->nationalBox;
91
    }
92
93
    /**
94
     * @param string $remark
95
     */
96
    public function setRemark($remark)
97
    {
98
        $this->remark = $remark;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getRemark()
105
    {
106
        return $this->remark;
107
    }
108
109
    /**
110
     * @param \Bpost\BpostApiClient\Bpost\Order\Sender $sender
111
     */
112
    public function setSender(Sender $sender)
113
    {
114
        $this->sender = $sender;
115
    }
116
117
    /**
118
     * @return \Bpost\BpostApiClient\Bpost\Order\Sender
119
     */
120
    public function getSender()
121
    {
122
        return $this->sender;
123
    }
124
125
    /**
126
     * @param string $status
127
     *
128
     * @throws BpostInvalidValueException
129
     */
130
    public function setStatus($status)
131
    {
132
        $status = strtoupper($status);
133
        if (!in_array($status, self::getPossibleStatusValues())) {
134
            throw new BpostInvalidValueException('status', $status, self::getPossibleStatusValues());
135
        }
136
137
        $this->status = $status;
138
    }
139
140
    /**
141
     * @param string $barcode
142
     */
143
    public function setBarcode($barcode)
144
    {
145
        $this->barcode = strtoupper((string) $barcode);
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getBarcode()
152
    {
153
        return $this->barcode;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getStatus()
160
    {
161
        return $this->status;
162
    }
163
164
    /**
165
     * @param string $additionalCustomerReference
166
     */
167
    public function setAdditionalCustomerReference($additionalCustomerReference)
168
    {
169
        $this->additionalCustomerReference = (string) $additionalCustomerReference;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getAdditionalCustomerReference()
176
    {
177
        return $this->additionalCustomerReference;
178
    }
179
180
    /**
181
     * @return array
182
     */
183
    public static function getPossibleStatusValues()
184
    {
185
        return array(
186
            self::BOX_STATUS_OPEN,
187
            self::BOX_STATUS_PENDING,
188
            self::BOX_STATUS_PRINTED,
189
            self::BOX_STATUS_CANCELLED,
190
            self::BOX_STATUS_ON_HOLD,
191
            self::BOX_STATUS_ANNOUNCED,
192
            self::BOX_STATUS_IN_TRANSIT,
193
            self::BOX_STATUS_AWAITING_PICKUP,
194
            self::BOX_STATUS_DELIVERED,
195
            self::BOX_STATUS_BACK_TO_SENDER,
196
        );
197
    }
198
199
    /**
200
     * Return the object as an array for usage in the XML
201
     *
202
     * @param DomDocument $document
203
     * @param string      $prefix
204
     *
205
     * @return DomElement
206
     */
207
    public function toXML(DOMDocument $document, $prefix = null)
208
    {
209
        $box = $document->createElement(XmlHelper::getPrefixedTagName('box', $prefix));
210
211
        $this->senderToXML($document, $prefix, $box);
212
        $this->boxToXML($document, $prefix, $box);
213
        $this->remarkToXML($document, $prefix, $box);
214
        $this->additionalCustomerReferenceToXML($document, $prefix, $box);
215
        $this->barcodeToXML($document, $prefix, $box);
216
217
        return $box;
218
    }
219
220
    /**
221
     * @param SimpleXMLElement $xml
222
     *
223
     * @return Box
224
     *
225
     * @throws BpostInvalidValueException
226
     * @throws BpostNotImplementedException
227
     */
228
    public static function createFromXML(SimpleXMLElement $xml)
229
    {
230
        $box = new Box();
231
        if (isset($xml->sender)) {
232
            $box->setSender(
233
                Sender::createFromXML(
234
                    $xml->sender->children(
235
                        'http://schema.post.be/shm/deepintegration/v3/common'
236
                    )
237
                )
238
            );
239
        }
240
        if (isset($xml->nationalBox)) {
241
            /** @var SimpleXMLElement $nationalBoxData */
242
            $nationalBoxData = $xml->nationalBox->children('http://schema.post.be/shm/deepintegration/v3/national');
243
244
            $classNameExtracted = $nationalBoxData->getName();
245
            if ($classNameExtracted == 'at24-7') {
246
                $classNameExtracted = 'at247';
247
            }
248
249
            // build classname based on the tag name
250
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($classNameExtracted);
251
252
            XmlHelper::assertMethodCreateFromXmlExists($className);
253
254
            $nationalBox = call_user_func(array($className, 'createFromXML'), $nationalBoxData);
255
256
            $box->setNationalBox($nationalBox);
257
        }
258
        if (isset($xml->internationalBox)) {
259
            /** @var SimpleXMLElement $internationalBoxData */
260
            $internationalBoxData = $xml->internationalBox->children('http://schema.post.be/shm/deepintegration/v3/international');
261
262
            $classNameExtracted = $internationalBoxData->getName();
263
            if ($classNameExtracted == 'atIntlHome') {
264
                $classNameExtracted = 'international';
265
            }
266
            // build classname based on the tag name
267
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($classNameExtracted);
268
269
            XmlHelper::assertMethodCreateFromXmlExists($className);
270
271
            $internationalBox = call_user_func(
272
                array($className, 'createFromXML'),
273
                $internationalBoxData
274
            );
275
276
            $box->setInternationalBox($internationalBox);
277
        }
278
        if (isset($xml->remark) && $xml->remark != '') {
279
            $box->setRemark((string) $xml->remark);
280
        }
281
        if (isset($xml->additionalCustomerReference) && $xml->additionalCustomerReference != '') {
282
            $box->setAdditionalCustomerReference((string) $xml->additionalCustomerReference);
283
        }
284
        if (!empty($xml->barcode)) {
285
            $box->setBarcode((string) $xml->barcode);
286
        }
287
        if (isset($xml->status) && $xml->status != '') {
288
            $box->setStatus((string) $xml->status);
289
        }
290
291
        return $box;
292
    }
293
294
    /**
295
     * @param DOMDocument $document
296
     * @param $prefix
297
     * @param DOMElement $box
298
     */
299
    private function barcodeToXML(DOMDocument $document, $prefix, DOMElement $box)
300
    {
301
        if ($this->getBarcode() !== null) {
0 ignored issues
show
introduced by
The condition $this->getBarcode() !== null is always true.
Loading history...
302
            $box->appendChild(
303
                $document->createElement(
304
                    XmlHelper::getPrefixedTagName('barcode', $prefix),
305
                    $this->getBarcode()
306
                )
307
            );
308
        }
309
    }
310
311
    /**
312
     * @param DOMDocument $document
313
     * @param $prefix
314
     * @param DOMElement $box
315
     */
316
    private function boxToXML(DOMDocument $document, $prefix, DOMElement $box)
317
    {
318
        if ($this->getNationalBox() !== null) {
319
            $box->appendChild(
320
                $this->getNationalBox()->toXML($document, $prefix)
321
            );
322
        }
323
        if ($this->getInternationalBox() !== null) {
324
            $box->appendChild(
325
                $this->getInternationalBox()->toXML($document, $prefix)
326
            );
327
        }
328
    }
329
330
    /**
331
     * @param DOMDocument $document
332
     * @param $prefix
333
     * @param DOMElement $box
334
     */
335
    private function senderToXML(DOMDocument $document, $prefix, DOMElement $box)
336
    {
337
        if ($this->getSender() !== null) {
338
            $box->appendChild(
339
                $this->getSender()->toXML($document, $prefix)
340
            );
341
        }
342
    }
343
344
    /**
345
     * @param DOMDocument $document
346
     * @param $prefix
347
     * @param DOMElement $box
348
     */
349
    private function remarkToXML(DOMDocument $document, $prefix, DOMElement $box)
350
    {
351
        if ($this->getRemark() !== null) {
0 ignored issues
show
introduced by
The condition $this->getRemark() !== null is always true.
Loading history...
352
            $box->appendChild(
353
                $document->createElement(
354
                    XmlHelper::getPrefixedTagName('remark', $prefix),
355
                    $this->getRemark()
356
                )
357
            );
358
        }
359
    }
360
361
    /**
362
     * @param DOMDocument $document
363
     * @param $prefix
364
     * @param DOMElement $box
365
     */
366
    private function additionalCustomerReferenceToXML(DOMDocument $document, $prefix, DOMElement $box)
367
    {
368
        if ($this->getAdditionalCustomerReference() !== null) {
0 ignored issues
show
introduced by
The condition $this->getAdditionalCustomerReference() !== null is always true.
Loading history...
369
            $box->appendChild(
370
                $document->createElement(
371
                    XmlHelper::getPrefixedTagName('additionalCustomerReference', $prefix),
372
                    $this->getAdditionalCustomerReference()
373
                )
374
            );
375
        }
376
    }
377
}
378