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 | View Code Duplication | 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) |
||
27 | { |
||
28 | $this->sendContentSms($to, $content); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Content SMS send process. |
||
33 | * |
||
34 | * @param $to |
||
35 | * @param $content |
||
36 | */ |
||
37 | public function sendContentSms($to, $content) |
||
38 | { |
||
39 | $url = 'http://api.smsbao.com/sms'; |
||
40 | $username = $this->smsUser; |
||
41 | $password = md5($this->smsPassword); |
||
42 | $content = urlencode($content); |
||
43 | $postString = "u=$username&p=$password&m=$to&c=$content"; |
||
44 | $response = $this->sockPost($url, $postString); |
||
45 | $this->setResult($response); |
||
46 | } |
||
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) |
||
56 | { |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Voice verify send process. |
||
61 | * |
||
62 | * @param $to |
||
63 | * @param $code |
||
64 | * @param $tempId |
||
65 | * @param array $tempData |
||
66 | */ |
||
67 | public function voiceVerify($to, $code, $tempId, array $tempData) |
||
76 | |||
77 | protected function setResult($result) |
||
78 | { |
||
79 | $msg = array_key_exists($result, $this->resultArr) ? $this->resultArr[$result] : '未知错误'; |
||
84 | } |
||
85 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.