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 |
||
15 | class Tracking extends Ups |
||
16 | { |
||
17 | const ENDPOINT = '/Track'; |
||
18 | |||
19 | /** |
||
20 | * @var RequestInterface |
||
21 | */ |
||
22 | private $request; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * Workaround flag to handle Multiple shipment nodes in tracking response |
||
27 | * See GitHub Issue #117 |
||
28 | * |
||
29 | * fixme in next major release |
||
30 | * |
||
31 | * @var boolean |
||
32 | */ |
||
33 | protected $allowMultipleShipments = false; |
||
34 | |||
35 | /** |
||
36 | * @var ResponseInterface |
||
37 | * // todo make private |
||
38 | */ |
||
39 | public $response; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $trackingNumber; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $referenceNumber; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $requestOption; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $shipperNumber; |
||
60 | |||
61 | /** |
||
62 | * @var \DateTime |
||
63 | */ |
||
64 | private $beginDate; |
||
65 | |||
66 | /** |
||
67 | * @var \DateTime |
||
68 | */ |
||
69 | private $endDate; |
||
70 | |||
71 | /** |
||
72 | * @param string|null $accessKey UPS License Access Key |
||
73 | * @param string|null $userId UPS User ID |
||
74 | * @param string|null $password UPS User Password |
||
75 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
76 | * @param RequestInterface|null $request |
||
77 | * @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
||
78 | */ |
||
79 | 6 | View Code Duplication | public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, RequestInterface $request = null, LoggerInterface $logger = null) |
86 | |||
87 | /** |
||
88 | * Get package tracking information. |
||
89 | * |
||
90 | * @param string $trackingNumber The package's tracking number. |
||
91 | * @param string $requestOption Optional processing. For Mail Innovations the only valid options are Last Activity and All activity. |
||
92 | * |
||
93 | * @throws Exception |
||
94 | * |
||
95 | * @return stdClass |
||
96 | */ |
||
97 | 5 | View Code Duplication | public function track($trackingNumber, $requestOption = 'activity') |
121 | |||
122 | /** |
||
123 | * Get package tracking information. |
||
124 | * |
||
125 | * @param string $referenceNumber Reference numbers can be a purchase order number, job number, etc. Reference number can be added when creating a shipment. |
||
126 | * @param string $requestOption |
||
127 | * |
||
128 | * @throws Exception |
||
129 | * |
||
130 | * @return stdClass |
||
131 | */ |
||
132 | 1 | View Code Duplication | public function trackByReference($referenceNumber, $requestOption = 'activity') |
156 | |||
157 | /** |
||
158 | * Set shipper number |
||
159 | * |
||
160 | * @param string $shipperNumber |
||
161 | * |
||
162 | */ |
||
163 | public function setShipperNumber($shipperNumber) |
||
167 | |||
168 | /** |
||
169 | * Set begin date |
||
170 | * |
||
171 | * @param string $beginDate |
||
172 | * |
||
173 | */ |
||
174 | public function setBeginDate(DateTime $beginDate) |
||
178 | |||
179 | /** |
||
180 | * Set end date |
||
181 | * |
||
182 | * @param string $endDate |
||
183 | * |
||
184 | */ |
||
185 | public function setEndDate(DateTime $endDate) |
||
189 | |||
190 | /** |
||
191 | * Check if tracking number is for mail innovations. |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 6 | private function isMailInnovations() |
|
251 | |||
252 | /** |
||
253 | * Create the Tracking request. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 6 | private function createRequest() |
|
304 | |||
305 | /** |
||
306 | * Format the response. |
||
307 | * |
||
308 | * @param SimpleXMLElement $response |
||
309 | * |
||
310 | * @return stdClass |
||
311 | */ |
||
312 | 3 | private function formatResponse(SimpleXMLElement $response) |
|
324 | |||
325 | /** |
||
326 | * @return RequestInterface |
||
327 | */ |
||
328 | 6 | View Code Duplication | public function getRequest() |
336 | |||
337 | /** |
||
338 | * @param RequestInterface $request |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | 6 | public function setRequest(RequestInterface $request) |
|
348 | |||
349 | /** |
||
350 | * @return ResponseInterface |
||
351 | */ |
||
352 | 2 | public function getResponse() |
|
356 | |||
357 | /** |
||
358 | * @param ResponseInterface $response |
||
359 | * |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function setResponse(ResponseInterface $response) |
||
368 | |||
369 | /** |
||
370 | * @param bool $value |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function allowMultipleShipments($value = true) |
||
379 | } |
||
380 |
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.