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 |
||
16 | class Shipper |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Shipper Name. |
||
21 | * Mandatory |
||
22 | * Correspond to (sName) |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $name; |
||
27 | /** |
||
28 | * Shipper Contact name. |
||
29 | * Mandatory |
||
30 | * Correspond to (sContact) |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $contactName; |
||
35 | /** |
||
36 | * Shipper Address Line 1. |
||
37 | * Mandatory |
||
38 | * Correspond to (sAddr1) |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $addressLine1; |
||
43 | /** |
||
44 | * Shipper Address Line 2. |
||
45 | * Optional |
||
46 | * Correspond to (sAddr2) |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $addressLine2; |
||
51 | /** |
||
52 | * Shipper City. |
||
53 | * Mandatory |
||
54 | * Correspond to (sCity) |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $city; |
||
59 | /** |
||
60 | * Shipper country. |
||
61 | * Mandatory |
||
62 | * Correspond to (sCntry) |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $country; |
||
67 | /** |
||
68 | * Shipper phone number. |
||
69 | * Mandatory |
||
70 | * Correspond to (sPhone) |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $phone; |
||
75 | |||
76 | /** |
||
77 | * Shipper constructor. |
||
78 | * |
||
79 | * @param string $name |
||
80 | * @param string $contactName |
||
81 | * @param string $addressLine1 |
||
82 | * @param string $city |
||
83 | * @param string $country |
||
84 | * @param string $phone |
||
85 | */ |
||
86 | View Code Duplication | public function __construct( |
|
101 | |||
102 | /** |
||
103 | * Add customer details to the shipment object. |
||
104 | * |
||
105 | * @param AddShipment|AddShip $shipment |
||
106 | * |
||
107 | * @return AddShipment|AddShip |
||
108 | */ |
||
109 | public function prepareForShipment($shipment): AbstractStructBase |
||
122 | |||
123 | /* ************************************************************************************************************** |
||
124 | * Setters and Getters |
||
125 | * **************************************************************************************************************/ |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getName(): string |
||
134 | |||
135 | /** |
||
136 | * @param string $name |
||
137 | * |
||
138 | * @return Shipper |
||
139 | */ |
||
140 | public function setName(string $name): self |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getContactName(): string |
||
154 | |||
155 | /** |
||
156 | * @param string $contactName |
||
157 | * |
||
158 | * @return Shipper |
||
159 | */ |
||
160 | public function setContactName(string $contactName): self |
||
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getAddressLine1(): string |
||
174 | |||
175 | /** |
||
176 | * @param string $addressLine1 |
||
177 | * |
||
178 | * @return Shipper |
||
179 | */ |
||
180 | public function setAddressLine1(string $addressLine1): self |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getAddressLine2() |
||
194 | |||
195 | /** |
||
196 | * @param string $addressLine2 |
||
197 | * |
||
198 | * @return Shipper |
||
199 | */ |
||
200 | public function setAddressLine2(string $addressLine2): self |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getCity(): string |
||
214 | |||
215 | /** |
||
216 | * @param string $city |
||
217 | * |
||
218 | * @return Shipper |
||
219 | */ |
||
220 | public function setCity(string $city): self |
||
226 | |||
227 | /** |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getCountry(): string |
||
234 | |||
235 | /** |
||
236 | * @param string $country |
||
237 | * |
||
238 | * @return Shipper |
||
239 | */ |
||
240 | public function setCountry(string $country): self |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getPhone(): string |
||
254 | |||
255 | /** |
||
256 | * @param string $phone |
||
257 | * |
||
258 | * @return Shipper |
||
259 | */ |
||
260 | public function setPhone(string $phone): self |
||
266 | } |
||
267 |
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.