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 |
||
17 | class SMS |
||
18 | { |
||
19 | /** |
||
20 | * Version number of the SMS API. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | const VERSION = '2.0.0'; |
||
25 | |||
26 | /** |
||
27 | * Base URL. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const BASE_URL = 'http://197.248.4.47/smsapi/'; |
||
32 | |||
33 | /** |
||
34 | * Send SMS endpoint. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const SMS_ENDPOINT = 'submit.php'; |
||
39 | |||
40 | /** |
||
41 | * Get Balance endpoint. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const GET_BALANCE_ENDPOINT = 'balance.php'; |
||
46 | |||
47 | /** |
||
48 | * token is generated by md5(password). |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $token; |
||
53 | |||
54 | /** |
||
55 | * timestamp is the current datetime. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $timestamp; |
||
60 | |||
61 | /** |
||
62 | * type of SMS to sent. |
||
63 | * |
||
64 | * @var int message |
||
65 | */ |
||
66 | protected $message_type = 0; |
||
67 | |||
68 | /** |
||
69 | * batch type of the SMS being sent. |
||
70 | * |
||
71 | * @var int batch |
||
72 | */ |
||
73 | protected $batch_type; |
||
74 | |||
75 | /** |
||
76 | * sms configurations. |
||
77 | * |
||
78 | * @array config |
||
79 | */ |
||
80 | protected $config; |
||
81 | |||
82 | /** |
||
83 | * end point url. |
||
84 | * |
||
85 | * @string endpoint |
||
86 | */ |
||
87 | protected $endpoint; |
||
88 | |||
89 | /** |
||
90 | * the message(s) being sent (array of messages in case message is different for each user. |
||
91 | * |
||
92 | * @var array. |
||
93 | */ |
||
94 | protected $message; |
||
95 | |||
96 | /** |
||
97 | * the recipients . |
||
98 | * |
||
99 | * @var array. |
||
100 | */ |
||
101 | protected $recipients; |
||
102 | |||
103 | /** |
||
104 | * SMS constructor. |
||
105 | */ |
||
106 | public function __construct() |
||
116 | |||
117 | /** |
||
118 | * set the timestamp. |
||
119 | */ |
||
120 | private function setTimestamp() |
||
124 | |||
125 | /** |
||
126 | * set the token. |
||
127 | */ |
||
128 | private function setToken() |
||
132 | |||
133 | /** |
||
134 | * invoke if SMS being sent is of type subscribable. |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function subscribable() |
||
144 | |||
145 | /** |
||
146 | * invoke if SMS being sent is of type on demand. |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function onDemand() |
||
156 | |||
157 | /** |
||
158 | * invoke if SMS being sent is of type bulk SMS. This will be the common one. |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function bulk() |
||
168 | |||
169 | /** |
||
170 | * invoke if SMS is being sent to a single recipient. |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function toOne() |
||
180 | |||
181 | /** |
||
182 | * invoke if SMS is being sent to a different recipients, or if each recipient receives custom sms. |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function toMany() |
||
192 | |||
193 | /** |
||
194 | * @param $recipients |
||
195 | * @param $message |
||
196 | * |
||
197 | * @throws BongaTechException |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function send($recipients, $message) |
||
233 | |||
234 | /** |
||
235 | * build the send object. |
||
236 | * |
||
237 | * @param recipients |
||
238 | * @param $messages |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | private function buildSendObject($recipients, $messages) |
||
274 | |||
275 | /** |
||
276 | * send a message to a single recipient. |
||
277 | * |
||
278 | * @param $body |
||
279 | * |
||
280 | * @return Response |
||
281 | */ |
||
282 | private function sendForNonBatch($body) |
||
289 | |||
290 | /** |
||
291 | * send batch. 1) same message to many recipients 2) different messages to many recipients. |
||
292 | * |
||
293 | * @param $body |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | private function sendForBatch($body) |
||
312 | |||
313 | public static function getBalance() |
||
324 | } |
||
325 |
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.