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 | trait SmartLoad |
||
16 | { |
||
17 | /** |
||
18 | * Authenticate and retrieves the dealer balance in Rands. |
||
19 | * |
||
20 | * @param string $dealerMsisdn |
||
21 | * |
||
22 | * @throws Exception |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | View Code Duplication | public function balance($dealerMsisdn) |
|
52 | |||
53 | /** |
||
54 | * Authenticate and request period based cashup reports. |
||
55 | * |
||
56 | * @param string $dealerMsisdn |
||
57 | * @param string $start |
||
58 | * @param string $end |
||
59 | * |
||
60 | * @throws Exception |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | View Code Duplication | public function cashup($dealerMsisdn, $start, $end) |
|
92 | |||
93 | /** |
||
94 | * Authenticate and request current day cashup report. |
||
95 | * |
||
96 | * @param string $dealerMsisdn |
||
97 | * |
||
98 | * @throws Exception |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | View Code Duplication | public function cashupToday($dealerMsisdn) |
|
128 | |||
129 | /** |
||
130 | * Authenticate and request to transfer funds from one Smartload account to another. |
||
131 | * |
||
132 | * @param string $fromDealerMsisdn |
||
133 | * @param string $toDealerMsisdn |
||
134 | * @param string $amount |
||
135 | * @param string $sendSms |
||
136 | * |
||
137 | * @throws Exception |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | View Code Duplication | public function fundstransfer($fromDealerMsisdn, $toDealerMsisdn, $amount, $sendSms) |
|
170 | |||
171 | /** |
||
172 | * Authenticate and retrieves a list of all available networks. |
||
173 | * |
||
174 | * @throws Exception |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 2 | View Code Duplication | public function network($id) |
204 | |||
205 | /** |
||
206 | * Authenticate and retrieves a list of all available networks. |
||
207 | * |
||
208 | * @throws Exception |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | 2 | View Code Duplication | public function networks() |
235 | |||
236 | /** |
||
237 | * Authenticate and recharge prevend request. |
||
238 | * |
||
239 | * @param string $dealerMsisdn |
||
240 | * @param string $clientReference |
||
241 | * @param string $smsRecipientMsisdn |
||
242 | * @param string $deviceId |
||
243 | * @param integer $productId |
||
244 | * @param integer $amount |
||
245 | * @param boolean $pinless |
||
246 | * @param boolean $sendSms |
||
247 | * |
||
248 | * |
||
249 | { |
||
250 | "smartloadId": "27821234567", |
||
251 | "clientReference": "abc123", |
||
252 | "smsRecipientMsisdn": "27821234567", |
||
253 | "deviceId": "27821234567", |
||
254 | "productId": 62, |
||
255 | "amount": 12, |
||
256 | "pinless": true, |
||
257 | "sendSms": true |
||
258 | } |
||
259 | |||
260 | */ |
||
261 | public function prevend() |
||
297 | |||
298 | /** |
||
299 | * Authenticate and retrieves a list of all available networks. |
||
300 | * |
||
301 | * @param integer $id |
||
302 | * |
||
303 | * @throws Exception |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | 2 | View Code Duplication | public function products($id) |
333 | |||
334 | /** |
||
335 | * Authenticate and checks if the provided ID (MSISDN) is registered with Smartload. |
||
336 | * |
||
337 | * @param string $dealerMsisdn |
||
338 | * |
||
339 | * @throws Exception |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | View Code Duplication | public function registered($dealerMsisdn) |
|
369 | } |
||
370 |
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.