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 |
||
14 | class Tracking extends Ups |
||
15 | { |
||
16 | const ENDPOINT = '/Track'; |
||
17 | |||
18 | /** |
||
19 | * @var RequestInterface |
||
20 | */ |
||
21 | private $request; |
||
22 | |||
23 | /** |
||
24 | * @var ResponseInterface |
||
25 | * // todo make private |
||
26 | */ |
||
27 | public $response; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $trackingNumber; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $referenceNumber; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $requestOption; |
||
43 | |||
44 | /** |
||
45 | * @param string|null $accessKey UPS License Access Key |
||
46 | * @param string|null $userId UPS User ID |
||
47 | * @param string|null $password UPS User Password |
||
48 | * @param bool $useIntegration Determine if we should use production or CIE URLs. |
||
49 | * @param RequestInterface $request |
||
50 | * @param LoggerInterface PSR3 compatible logger (optional) |
||
51 | */ |
||
52 | 6 | View Code Duplication | public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, RequestInterface $request = null, LoggerInterface $logger = null) |
59 | |||
60 | /** |
||
61 | * Get package tracking information. |
||
62 | * |
||
63 | * @param string $trackingNumber The package's tracking number. |
||
64 | * @param string $requestOption Optional processing. For Mail Innovations the only valid options are Last Activity and All activity. |
||
65 | * |
||
66 | * @throws Exception |
||
67 | * |
||
68 | * @return stdClass |
||
69 | */ |
||
70 | 5 | View Code Duplication | public function track($trackingNumber, $requestOption = 'activity') |
94 | |||
95 | /** |
||
96 | * Get package tracking information. |
||
97 | * |
||
98 | * @param string $referenceNumber Reference numbers can be a purchase order number, job number, etc. Reference number can be added when creating a shipment. |
||
99 | * @throws Exception |
||
100 | * |
||
101 | * @return stdClass |
||
102 | */ |
||
103 | 1 | View Code Duplication | public function trackByReference($referenceNumber, $requestOption = 'activity') |
127 | |||
128 | /** |
||
129 | * Check if tracking number is for mail innovations. |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 6 | private function isMailInnovations() |
|
186 | |||
187 | /** |
||
188 | * Create the Tracking request. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 6 | private function createRequest() |
|
224 | |||
225 | /** |
||
226 | * Format the response. |
||
227 | * |
||
228 | * @param SimpleXMLElement $response |
||
229 | * |
||
230 | * @return stdClass |
||
231 | */ |
||
232 | 3 | private function formatResponse(SimpleXMLElement $response) |
|
236 | |||
237 | /** |
||
238 | * @return RequestInterface |
||
239 | */ |
||
240 | 6 | View Code Duplication | public function getRequest() |
248 | |||
249 | /** |
||
250 | * @param RequestInterface $request |
||
251 | * |
||
252 | * @return $this |
||
253 | */ |
||
254 | 6 | public function setRequest(RequestInterface $request) |
|
260 | |||
261 | /** |
||
262 | * @return ResponseInterface |
||
263 | */ |
||
264 | 2 | public function getResponse() |
|
268 | |||
269 | /** |
||
270 | * @param ResponseInterface $response |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function setResponse(ResponseInterface $response) |
||
280 | } |
||
281 |
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.