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 |
||
20 | class Broadcast |
||
21 | { |
||
22 | const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall'; |
||
23 | const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send'; |
||
24 | const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete'; |
||
25 | const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview'; |
||
26 | const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get'; |
||
27 | |||
28 | const PREVIEW_BY_OPENID = 'touser'; |
||
29 | const PREVIEW_BY_WXH = 'towxname'; |
||
30 | |||
31 | /** |
||
32 | * Http对象 |
||
33 | * |
||
34 | * @var Http |
||
35 | */ |
||
36 | protected $http; |
||
37 | |||
38 | /** |
||
39 | * 消息 |
||
40 | * |
||
41 | * @var \Overtrue\Wechat\Messages\BaseMessage; |
||
42 | */ |
||
43 | protected $message; |
||
44 | |||
45 | /** |
||
46 | * constructor |
||
47 | * |
||
48 | * @param string $appId |
||
49 | * @param string $appSecret |
||
50 | */ |
||
51 | public function __construct($appId, $appSecret) |
||
55 | |||
56 | /** |
||
57 | * 准备消息 |
||
58 | * |
||
59 | * @param \Overtrue\Wechat\Messages\BaseMessage $message |
||
60 | * |
||
61 | * @return Broadcast |
||
62 | */ |
||
63 | View Code Duplication | public function send($message) |
|
75 | |||
76 | /** |
||
77 | * 发送消息 |
||
78 | * |
||
79 | * @param string $group 组或oppenid |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function to($group = null) |
||
95 | |||
96 | /** |
||
97 | * 删除群发 |
||
98 | * |
||
99 | * @param string $msgId 发出去的消息ID |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function delete($msgId) |
||
107 | |||
108 | /** |
||
109 | * 预览 |
||
110 | * |
||
111 | * @param string $openId 接收消息用户对应该公众号的openid |
||
112 | * @param string $type 接收消息用户的类型 |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function preview($openId, $type = self::PREVIEW_BY_OPENID) |
||
122 | |||
123 | /** |
||
124 | * 查询群发消息发送状态 |
||
125 | * |
||
126 | * @param string $msgId 全消息ID |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function status($msgId) |
||
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.