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 |
||
11 | class SmsBaoAgent extends Agent |
||
12 | { |
||
13 | protected $resultArr = [ |
||
14 | '0' => '发送成功', |
||
15 | '-1' => '参数不全', |
||
16 | '-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!', |
||
17 | '30' => '密码错误', |
||
18 | '40' => '账号不存在', |
||
19 | '41' => '余额不足', |
||
20 | '42' => '帐户已过期', |
||
21 | '43' => 'IP地址限制', |
||
22 | '50' => '内容含有敏感词', |
||
23 | '51' => '手机号码不正确', |
||
24 | ]; |
||
25 | |||
26 | public function sendSms($to, $content, $tempId, array $data) |
||
30 | |||
31 | /** |
||
32 | * Content SMS send process. |
||
33 | * |
||
34 | * @param $to |
||
35 | * @param $content |
||
36 | */ |
||
37 | public function sendContentSms($to, $content) |
||
47 | |||
48 | /** |
||
49 | * Template SMS send process. |
||
50 | * |
||
51 | * @param $to |
||
52 | * @param $tempId |
||
53 | * @param array $tempData |
||
54 | */ |
||
55 | public function sendTemplateSms($to, $tempId, array $tempData) |
||
58 | |||
59 | /** |
||
60 | * Voice verify send process. |
||
61 | * |
||
62 | * @param $to |
||
63 | * @param $code |
||
64 | * @param $tempId |
||
65 | * @param array $tempData |
||
66 | */ |
||
67 | View Code Duplication | public function voiceVerify($to, $code, $tempId, array $tempData) |
|
76 | |||
77 | protected function setResult($result) |
||
84 | } |
||
85 |
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.