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 |
||
19 | View Code Duplication | class TrackingWrapper |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @var \Ups\Tracking |
||
23 | */ |
||
24 | private $upsTracking; |
||
25 | |||
26 | /** |
||
27 | * Shipping constructor. |
||
28 | * |
||
29 | * @param Tracking $upsTracking |
||
30 | */ |
||
31 | public function __construct(Tracking $upsTracking) |
||
35 | |||
36 | /** |
||
37 | * Get package tracking information. |
||
38 | * |
||
39 | * @param string $trackingNumber The package's tracking number. |
||
40 | * @param string $requestOption Optional processing. For Mail Innovations the only valid options are Last Activity and All activity. |
||
41 | * |
||
42 | * @throws Exception |
||
43 | * |
||
44 | * @return \StdClass |
||
45 | */ |
||
46 | public function track($trackingNumber, $requestOption = 'activity') |
||
50 | |||
51 | /** |
||
52 | * Get package tracking information. |
||
53 | * |
||
54 | * @param string $referenceNumber Reference numbers can be a purchase order number, job number, etc. Reference number can be added when creating a shipment. |
||
55 | * |
||
56 | * @throws Exception |
||
57 | * |
||
58 | * @return \StdClass |
||
59 | */ |
||
60 | public function trackByReference($referenceNumber, $requestOption = 'activity') |
||
64 | |||
65 | /** |
||
66 | * @return RequestInterface |
||
67 | */ |
||
68 | public function getRequest() |
||
72 | |||
73 | /** |
||
74 | * @param RequestInterface $request |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function setRequest(RequestInterface $request) |
||
84 | |||
85 | /** |
||
86 | * @return ResponseInterface |
||
87 | */ |
||
88 | public function getResponse() |
||
92 | |||
93 | /** |
||
94 | * @param ResponseInterface $response |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setResponse(ResponseInterface $response) |
||
104 | } |
||
105 |
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.