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 = '1.0.0'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * token is generated by md5(password). |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $token; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * timestamp is the current datetime. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $timestamp; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * type of SMS to sent. |
||
| 42 | * |
||
| 43 | * @var int message |
||
| 44 | */ |
||
| 45 | protected $message_type; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * batch type of the SMS being sent. |
||
| 49 | * |
||
| 50 | * @var int batch |
||
| 51 | */ |
||
| 52 | protected $batch_type; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * sms configurations. |
||
| 56 | * |
||
| 57 | * @array config |
||
| 58 | */ |
||
| 59 | protected $config; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * end point url. |
||
| 63 | * |
||
| 64 | * @string endpoint |
||
| 65 | */ |
||
| 66 | protected $endpoint; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * the message(s) being sent (array of messages in case message is different for each user. |
||
| 70 | * |
||
| 71 | * @var array. |
||
| 72 | */ |
||
| 73 | protected $message; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * the recipients . |
||
| 77 | * |
||
| 78 | * @var array. |
||
| 79 | */ |
||
| 80 | protected $recipients; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * SMS constructor. |
||
| 84 | */ |
||
| 85 | public function __construct() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * set the timestamp. |
||
| 94 | */ |
||
| 95 | private function setTimestamp() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * set the token. |
||
| 102 | */ |
||
| 103 | private function setToken() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * invoke if SMS being sent is of type subscribable. |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function messageTypeSubscribable() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * invoke if SMS being sent is of type on demand. |
||
| 122 | * |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | public function messageTypeOnDemand() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * invoke if SMS being sent is of type bulk SMS. This will be the common one. |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function messageTypeBulk() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * invoke if SMS is being sent to a single recipient. |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function batchTypeNoBatch() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * invoke if SMS is being sent to a different recipients. |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function batchTypeSameMessage() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * invoke if each recipient will receive a different message. |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function batchTypeDifferentMessages() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $recipients |
||
| 182 | * @param $message |
||
| 183 | * |
||
| 184 | * @throws BongaTechException |
||
| 185 | * |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function send($recipients, $message) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * build the send object. |
||
| 267 | * |
||
| 268 | * @param recipients |
||
| 269 | * @param $messages |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | private function buildSendObject($recipients, $messages) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * send a message to a single recipient. |
||
| 311 | * |
||
| 312 | * @param $body |
||
| 313 | * |
||
| 314 | * @return Response |
||
| 315 | */ |
||
| 316 | private function sendForNonBatch($body) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * send batch. 1) same message to many recipients 2) different messages to many recipients. |
||
| 328 | * |
||
| 329 | * @param $body |
||
| 330 | * |
||
| 331 | * @return Response |
||
| 332 | */ |
||
| 333 | private function sendForBatch($body) |
||
| 353 | |||
| 354 | public static function getBalance() |
||
| 365 | } |
||
| 366 |
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.