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 |
||
10 | class Transaction |
||
11 | { |
||
12 | const STATUS_AUTHORIZED = "AUTHORIZED"; |
||
13 | const STATUS_CONFIRMED = "CONFIRMED"; |
||
14 | const STATUS_PENDING = "PENDING"; |
||
15 | const STATUS_APPROVED = "APPROVED"; |
||
16 | const STATUS_CANCELED = "CANCELED"; |
||
17 | const STATUS_DENIED = "DENIED"; |
||
18 | const STATUS_ERROR = "ERROR"; |
||
19 | |||
20 | /** @var */ |
||
21 | private $seller_id; |
||
22 | |||
23 | /** @var */ |
||
24 | private $amount; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $currency = "BRL"; |
||
28 | |||
29 | /** @var */ |
||
30 | private $order; |
||
31 | |||
32 | /** @var */ |
||
33 | private $customer; |
||
34 | |||
35 | /** @var */ |
||
36 | private $device; |
||
37 | |||
38 | /** @var */ |
||
39 | private $shippings; |
||
40 | |||
41 | /** @var */ |
||
42 | private $credit; |
||
43 | |||
44 | /** @var */ |
||
45 | private $debit; |
||
46 | |||
47 | /** @var */ |
||
48 | private $boleto; |
||
49 | |||
50 | /** |
||
51 | * @return false|string |
||
52 | */ |
||
53 | View Code Duplication | public function toJSON() |
|
62 | |||
63 | /** |
||
64 | * @return mixed |
||
65 | */ |
||
66 | public function getSellerId() |
||
70 | |||
71 | /** |
||
72 | * @param $seller_id |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function setSellerId($seller_id) |
||
81 | |||
82 | /** |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function getAmount() |
||
89 | |||
90 | /** |
||
91 | * @param $amount |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function setAmount($amount) |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getCurrency() |
||
108 | |||
109 | /** |
||
110 | * @param $currency |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setCurrency($currency) |
||
119 | |||
120 | /** |
||
121 | * @param $order_id |
||
122 | * @return Order |
||
123 | */ |
||
124 | public function order($order_id) |
||
131 | |||
132 | /** |
||
133 | * @return Order |
||
134 | */ |
||
135 | public function getOrder() |
||
139 | |||
140 | /** |
||
141 | * @param Order $order |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setOrder(Order $order) |
||
150 | |||
151 | /** |
||
152 | * @param null $id |
||
153 | * @return Customer |
||
154 | */ |
||
155 | public function customer($id = null) |
||
163 | |||
164 | /** |
||
165 | * @return mixed |
||
166 | */ |
||
167 | public function getCustomer() |
||
171 | |||
172 | /** |
||
173 | * @param Customer $customer |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setCustomer(Customer $customer) |
||
182 | |||
183 | /** |
||
184 | * @param $device_id |
||
185 | * @return Device |
||
186 | */ |
||
187 | public function device($device_id) |
||
195 | |||
196 | /** |
||
197 | * @return Device |
||
198 | */ |
||
199 | public function getDevice() |
||
203 | |||
204 | /** |
||
205 | * @param Device $device |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setDevice(Device $device) |
||
214 | |||
215 | /** |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function getShippings() |
||
222 | |||
223 | /** |
||
224 | * @param $shippings |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setShippings($shippings) |
||
233 | |||
234 | /** |
||
235 | * @return Shipping |
||
236 | */ |
||
237 | public function shipping() |
||
245 | |||
246 | /** |
||
247 | * @param Shipping $shipping |
||
248 | */ |
||
249 | public function addShipping(Shipping $shipping) |
||
257 | |||
258 | /** |
||
259 | * @param Customer $customer |
||
260 | * @return Shipping |
||
261 | */ |
||
262 | public function addShippingByCustomer(Customer $customer) |
||
270 | |||
271 | /** |
||
272 | * @param null $brand |
||
273 | * @return Credit |
||
274 | */ |
||
275 | public function credit($brand = null) |
||
282 | |||
283 | /** |
||
284 | * @return Credit |
||
285 | */ |
||
286 | public function getCredit() |
||
290 | |||
291 | /** |
||
292 | * @param Credit $credit |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setCredit(Credit $credit) |
||
301 | |||
302 | /** |
||
303 | * @param null $brand |
||
304 | * @return Credit |
||
305 | */ |
||
306 | public function debit($brand = null) |
||
314 | |||
315 | /** |
||
316 | * @return mixed |
||
317 | */ |
||
318 | public function getDebit() |
||
322 | |||
323 | /** |
||
324 | * @param Credit $debit |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function setDebit(Credit $debit) |
||
333 | |||
334 | /** |
||
335 | * @param $our_number |
||
336 | * @return Boleto |
||
337 | */ |
||
338 | public function boleto($our_number) |
||
345 | |||
346 | /** |
||
347 | * @return mixed |
||
348 | */ |
||
349 | public function getBoleto() |
||
353 | |||
354 | /** |
||
355 | * @param Boleto $boleto |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function setBoleto(Boleto $boleto) |
||
364 | } |
||
365 |
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.