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 |
||
20 | class SMSFactor |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | const ENDPOINT = 'https://api.smsfactor.com/'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $endpoint; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $content_type; |
||
37 | |||
38 | /** |
||
39 | * @var AdapterInterface |
||
40 | */ |
||
41 | protected $adapter; |
||
42 | |||
43 | /** |
||
44 | * @param AdapterInterface $adapter |
||
45 | */ |
||
46 | public function __construct(AdapterInterface $adapter, $content_type, $endpoint = null) |
||
52 | |||
53 | /** |
||
54 | * Create an account or sub account. |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | View Code Duplication | public function createAccount($params) |
|
70 | |||
71 | /** |
||
72 | * Get current credits. |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | View Code Duplication | public function credits() |
|
88 | |||
89 | /** |
||
90 | * Send or simulate sending of single or multiple SMSs. |
||
91 | * |
||
92 | * @return mixed |
||
93 | */ |
||
94 | public function send($params, $method, $simulate = false) |
||
144 | |||
145 | /** |
||
146 | * Send or simulate sending of SMSs to selected lists. |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | View Code Duplication | public function sendLists($params, $simulate = false) |
|
167 | |||
168 | /** |
||
169 | * Cancel the sending with selected id. |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | View Code Duplication | public function delete($id) |
|
185 | |||
186 | /** |
||
187 | * Create or update a contact list. |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | View Code Duplication | public function contactList($params) |
|
203 | |||
204 | /** |
||
205 | * Retrieve contact lists. |
||
206 | * |
||
207 | * @return mixed |
||
208 | */ |
||
209 | View Code Duplication | public function getContactList($id = null) |
|
226 | |||
227 | /** |
||
228 | * Remove duplicated contacts from list. |
||
229 | * |
||
230 | * @return mixed |
||
231 | */ |
||
232 | View Code Duplication | public function deduplicate($id) |
|
243 | |||
244 | /** |
||
245 | * @return mixed |
||
246 | */ |
||
247 | View Code Duplication | public function deleteContact($id) |
|
258 | |||
259 | /** |
||
260 | * Retrieve blacklist contacts. |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | View Code Duplication | public function getBlacklist() |
|
276 | |||
277 | /** |
||
278 | * @return mixed |
||
279 | */ |
||
280 | View Code Duplication | public function deliveryReport($params) |
|
291 | |||
292 | private function isValidDate($date) |
||
307 | |||
308 | } |
||
309 |
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.