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 |
||
| 16 | class SmsLogger |
||
| 17 | { |
||
| 18 | use AdodbAwareTrait; |
||
| 19 | use UtilContainerAwareTrait; |
||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Log table name |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $table = 'sms_log'; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Count dest number for each mobile company |
||
| 32 | * |
||
| 33 | * @param array $arDest |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | public function countDestCompany($arDest) |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Count SMS will split to how many parts to send |
||
| 71 | * |
||
| 72 | * If only ascii chars include, 140 chars for 1 sms part, if has chinese |
||
| 73 | * chars, 70 chars for 1 sms part only. |
||
| 74 | * |
||
| 75 | * 1 chinese char will count as 1 char. |
||
| 76 | * |
||
| 77 | * @param string $sms |
||
| 78 | * @return integer |
||
| 79 | */ |
||
| 80 | public function countPart($sms = '') |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Log sent sms with stat information |
||
| 98 | * |
||
| 99 | * @param array $arDest |
||
| 100 | * @param string $sms |
||
| 101 | * @param integer $cat |
||
| 102 | */ |
||
| 103 | public function log($arDest, $sms, $cat) |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Generate an UUID |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | protected function generateUuid() |
||
| 134 | } |
||
| 135 |
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.