Shipment   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 269
Duplicated Lines 1.86 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 269
rs 9.8
wmc 31
lcom 1
cbo 9

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setDeliveryTime() 0 5 1
A getDeliveryTime() 0 4 1
A setPickupDate() 0 5 1
A getPickupDate() 0 4 1
A setShipmentType() 0 5 1
A getShipmentType() 0 4 1
A isSmallPackage() 0 4 1
A isFreight() 0 4 1
A isMailInnovation() 0 4 1
A setStatus() 0 5 1
A getStatus() 0 4 1
A setWeight() 0 5 1
A getWeight() 0 4 1
A setReferenceNumber() 0 5 1
A getReferenceNumber() 0 4 1
A setShipmentIdentificationNumber() 0 5 1
A getShipmentIdentificationNumber() 0 4 1
F fromXml() 5 56 14

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 namespace SimpleUPS\Track\SmallPackage;
2
3
use \SimpleUPS\Service;
4
use \SimpleUPS\Shipper;
5
use \SimpleUPS\InstructionalAddress;
6
use \SimpleUPS\Track\ShipmentType;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SimpleUPS\Track\SmallPackage\ShipmentType.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use \SimpleUPS\Track\Status;
8
use \SimpleUPS\Track\ReferenceNumber;
9
10
/**
11
 * A shipment is associated with a tracking number and is made up of 1 or more packages
12
 * @since 1.0
13
 */
14
class Shipment extends \SimpleUPS\Shipment
15
{
16
17
    private
18
        /* @var \SimpleUPS\Track\Status $status */
19
        $status,
20
        /* @var \SimpleUPS\Track\ShipmentType $shipmentType */
21
        $shipmentType,
22
23
        /* @var ReferenceNumber $referenceNumber */
24
        $referenceNumber,
25
        /* @var string $shipmentIdentificationNumber */
26
        $shipmentIdentificationNumber,
27
28
        /* @var \DateTime $pickupDate */
29
        $pickupDate,
30
        /* @var Weight $weight */
31
        $weight,
32
33
        /* @var \DateTime $deliveryTime */
34
        $deliveryTime;
35
36
    /**
37
     * @internal
38
     *
39
     * @param \DateTime $deliveryTime
40
     *
41
     * @return Shipment
42
     */
43
    public function setDeliveryTime(\DateTime $deliveryTime)
44
    {
45
        $this->deliveryTime = $deliveryTime;
46
        return $this;
47
    }
48
49
    /**
50
     * When the item was delivered
51
     * @see getStatus()
52
     * @return \DateTime
53
     */
54
    public function getDeliveryTime()
55
    {
56
        return $this->deliveryTime;
57
    }
58
59
    /**
60
     * @internal
61
     *
62
     * @param \DateTime $pickupDate
63
     *
64
     * @return Shipment
65
     */
66
    public function setPickupDate(\DateTime $pickupDate)
67
    {
68
        $this->pickupDate = $pickupDate;
69
        return $this;
70
    }
71
72
    /**
73
     * Date the shipment was picked up from the shipper
74
     * @return \DateTime
75
     */
76
    public function getPickupDate()
77
    {
78
        return $this->pickupDate;
79
    }
80
81
    /**
82
     * @internal
83
     *
84
     * @param \SimpleUPS\Track\ShipmentType $shipmentType
85
     *
86
     * @return Shipment
87
     */
88
    public function setShipmentType(\SimpleUPS\Track\ShipmentType $shipmentType)
89
    {
90
        $this->shipmentType = $shipmentType;
91
        return $this;
92
    }
93
94
    /**
95
     * The type of shipment being tracked
96
     * @return string
97
     */
98
    public function getShipmentType()
99
    {
100
        return $this->shipmentType->getDescription();
101
    }
102
103
    /**
104
     * Determine if this shipment is a small package
105
     * @return bool
106
     */
107
    public function isSmallPackage()
108
    {
109
        return ShipmentType::TYPE_SMALL_PACKAGE == $this->shipmentType->getCode();
110
    }
111
112
    /**
113
     * Determine if this shipment is freight
114
     * @return bool
115
     */
116
    public function isFreight()
117
    {
118
        return ShipmentType::TYPE_FREIGHT == $this->shipmentType->getCode();
119
    }
120
121
    /**
122
     * Determine if this shipment is mail innovations
123
     * @return bool
124
     */
125
    public function isMailInnovation()
126
    {
127
        return ShipmentType::TYPE_MAIL_INNOVATION == $this->shipmentType->getCode();
128
    }
129
130
    /**
131
     * @internal
132
     *
133
     * @param Status $status
134
     *
135
     * @return Shipment
136
     */
137
    public function setStatus(Status $status)
138
    {
139
        $this->status = $status;
140
        return $this;
141
    }
142
143
    /**
144
     * Status of shipment
145
     * @return Status
146
     */
147
    public function getStatus()
148
    {
149
        return $this->status;
150
    }
151
152
    /**
153
     * @internal
154
     *
155
     * @param \SimpleUPS\Weight $weight
156
     *
157
     * @return Shipment
158
     */
159
    public function setWeight(\SimpleUPS\Weight $weight)
160
    {
161
        $this->weight = $weight;
162
        return $this;
163
    }
164
165
    /**
166
     * Shipment weight
167
     * @return \SimpleUPS\Weight
168
     */
169
    public function getWeight()
170
    {
171
        return $this->weight;
172
    }
173
174
    /**
175
     * @internal
176
     *
177
     * @param ReferenceNumber $referenceNumber
178
     *
179
     * @return Shipment
180
     */
181
    public function setReferenceNumber(ReferenceNumber $referenceNumber)
182
    {
183
        $this->referenceNumber = $referenceNumber;
184
        return $this;
185
    }
186
187
    /**
188
     * Shipment reference number
189
     * @return ReferenceNumber
190
     */
191
    public function getReferenceNumber()
192
    {
193
        return $this->referenceNumber;
194
    }
195
196
    /**
197
     * @internal
198
     *
199
     * @param string $shipmentIdentificationNumber
200
     *
201
     * @return Shipment
202
     */
203
    public function setShipmentIdentificationNumber($shipmentIdentificationNumber)
204
    {
205
        $this->shipmentIdentificationNumber = (string)$shipmentIdentificationNumber;
206
        return $this;
207
    }
208
209
    /**
210
     * Shipment identification number
211
     * @return string
212
     */
213
    public function getShipmentIdentificationNumber()
214
    {
215
        return $this->shipmentIdentificationNumber;
216
    }
217
218
    /**
219
     * @internal
220
     *
221
     * @param \SimpleXMLElement $xml
222
     *
223
     * @return Shipment
224
     */
225
    public static function fromXml(\SimpleXMLElement $xml)
226
    {
227
        $shipment = new Shipment();
228
        $shipment->setIsResponse();
229
230
        if (isset($xml->PickupDate)) {
231
            $shipment->setPickupDate(new \DateTime((string)$xml->PickupDate));
232
        }
233
234 View Code Duplication
        if (isset($xml->ScheduledDeliveryDate) && isset($xml->ScheduledDeliveryTime)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
235
            $shipment->setDeliveryTime(
236
                new \DateTime((string)trim($xml->ScheduledDeliveryDate . ' ' . $xml->ScheduledDeliveryTime))
237
            );
238
        }
239
240
        if (isset($xml->ShipmentType)) {
241
            $shipment->setShipmentType(ShipmentType::fromXml($xml->ShipmentType));
242
        }
243
244
        if (isset($xml->Shipper)) {
245
            $shipment->setShipper(Shipper::fromXml($xml->Shipper));
246
        }
247
248
        if (isset($xml->ShipTo->Address)) {
249
            $shipment->setDestination(InstructionalAddress::fromXml($xml->ShipTo->Address));
250
        }
251
252
        if (isset($xml->Package)) {
253
            foreach ($xml->Package as $package) {
254
                $shipment->addPackage(Package::fromXml($package));
255
            }
256
        }
257
258
        if (isset($xml->Service)) {
259
            $shipment->setService(Service::fromXml($xml->Service));
260
        }
261
262
        if (isset($xml->CurrentStatus)) {
263
            $shipment->setStatus(Status::fromXml($xml->CurrentStatus));
264
        }
265
266
        if (isset($xml->ReferenceNumber)) {
267
            $shipment->setReferenceNumber(ReferenceNumber::fromXml($xml->ReferenceNumber));
268
        }
269
270
        if (isset($xml->ShipmentWeight)) {
271
            $shipment->setWeight(\SimpleUPS\Weight::fromXml($xml->ShipmentWeight));
272
        }
273
274
        if (isset($xml->ShipmentIdentificationNumber)) {
275
            $shipment->setShipmentIdentificationNumber($xml->ShipmentIdentificationNumber);
276
        }
277
278
        return $shipment;
279
280
    }
281
282
}