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 |
||
9 | class KfaccountApi extends BaseApi |
||
10 | { |
||
11 | /** |
||
12 | * 客服账号管理-添加客服账号. |
||
13 | * |
||
14 | * @author Huang |
||
15 | * |
||
16 | * @param string $kf_account 客服账号 |
||
17 | * |
||
18 | * @param string $nickname 客服昵称 |
||
19 | * |
||
20 | * @param string $password 客服密码 |
||
21 | * |
||
22 | * @return array 客服信息. |
||
23 | */ |
||
24 | View Code Duplication | public function add($kf_account, $nickname, $password) |
|
40 | |||
41 | /** |
||
42 | * 客服账号管理-修改客服账号. |
||
43 | * |
||
44 | * @author Huang |
||
45 | * |
||
46 | * @param string $kf_account 客服账号 |
||
47 | * |
||
48 | * @param string $nickname 客服昵称 |
||
49 | * |
||
50 | * @param string $password 客服密码 |
||
51 | * |
||
52 | * @return array 客服信息. |
||
53 | */ |
||
54 | View Code Duplication | public function update($kf_account, $nickname, $password) |
|
70 | |||
71 | /** |
||
72 | * 客服账号管理-删除客服账号. |
||
73 | * |
||
74 | * @author Huang |
||
75 | * |
||
76 | * @param string $kf_account 客服账号 |
||
77 | * |
||
78 | * @return array 客服信息. |
||
79 | */ |
||
80 | public function del($kf_account) |
||
93 | |||
94 | /** |
||
95 | * 客服账号管理-获取客服基本信息. |
||
96 | * |
||
97 | * @author Huang |
||
98 | * |
||
99 | * @return array 客服信息. |
||
100 | */ |
||
101 | public function getkflist() |
||
111 | |||
112 | /** |
||
113 | * 客服账号管理-获取在线客服接待信息. |
||
114 | * |
||
115 | * @author Huang |
||
116 | * |
||
117 | * @return array 客服信息. |
||
118 | */ |
||
119 | public function getonlinekflist() |
||
129 | |||
130 | /** |
||
131 | * 客服账号管理-设置客服账号的头像. |
||
132 | * |
||
133 | * @author Huang |
||
134 | * |
||
135 | * @param string $kf_account 客服账号 |
||
136 | * |
||
137 | * @return array 客服信息. |
||
138 | */ |
||
139 | public function uploadheadimg($file, $kf_account) |
||
165 | |||
166 | /** |
||
167 | * 获取客服聊天记录. |
||
168 | * |
||
169 | * @author Huang |
||
170 | * |
||
171 | * @param int $endtime 查询结束时间,UNIX时间戳,每次查询不能跨日查询 |
||
172 | * |
||
173 | * @param int $pageindex 查询第几页,从1开始 |
||
174 | * |
||
175 | * @param int $pagesize 每页大小,每页最多拉取50条 |
||
176 | * |
||
177 | * @param int $starttime 查询开始时间,UNIX时间戳 |
||
178 | * |
||
179 | * @return array 客服信息. |
||
180 | */ |
||
181 | public function getrecord($endtime, $pageindex, $pagesize, $starttime) |
||
198 | |||
199 | |||
200 | /** |
||
201 | * ------------------------------------------发送客服消息------------------------------------------ |
||
202 | */ |
||
203 | |||
204 | /** |
||
205 | * 图文消息- 自定义 |
||
206 | * |
||
207 | * @param array $articles 图文消息 |
||
208 | * @param string $openid 接收消息用户对应该公众号的openid |
||
209 | * @param string $kf_account 客服账号 |
||
210 | * |
||
211 | * @return int msg_id 发送出去的消息ID |
||
212 | */ |
||
213 | public function customNews($articles = [], $openid, $kf_account = '') |
||
228 | |||
229 | /** |
||
230 | * 图文消息 - 微信 |
||
231 | * |
||
232 | * @param boolean $media_id 用于设定是否向全部用户发送,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户 |
||
233 | * @param string $openid 接收消息用户对应该公众号的openid |
||
234 | * @param string $type 接受用户 是 openid 还是 wxname |
||
235 | * @param string $kf_account 客服账号 |
||
236 | * |
||
237 | * @return int msg_id 发送出去的消息ID |
||
238 | */ |
||
239 | View Code Duplication | public function customMpnews($media_id = '', $openid = '', $kf_account = '') |
|
254 | |||
255 | /** |
||
256 | * 文本消息 |
||
257 | * |
||
258 | * @param string $content 文字 |
||
259 | * @param string $openid 接收消息用户对应该公众号的openid |
||
260 | * @param string $kf_account 客服账号 |
||
261 | * |
||
262 | * @return int msg_id 发送出去的消息ID |
||
263 | */ |
||
264 | View Code Duplication | public function customText($content = '', $openid, $kf_account = '') |
|
279 | |||
280 | /** |
||
281 | * 语音(注意此处media_id需通过基础支持中的上传下载多媒体文件来得到) |
||
282 | * |
||
283 | * @param string $media_id 用于群发的消息的media_id |
||
284 | * @param string $openid 接收消息用户对应该公众号的openid |
||
285 | * @param string $kf_account 客服账号 |
||
286 | * |
||
287 | * @return int msg_id 发送出去的消息ID |
||
288 | */ |
||
289 | View Code Duplication | public function customVoice($media_id = '', $openid, $kf_account = '') |
|
304 | |||
305 | /** |
||
306 | * 图片(注意此处media_id需通过基础支持中的上传下载多媒体文件来得到) |
||
307 | * |
||
308 | * @param string $media_id 用于群发的消息的media_id |
||
309 | * @param string $openid 接收消息用户对应该公众号的openid |
||
310 | * @param string $kf_account 客服账号 |
||
311 | * |
||
312 | * @return int msg_id 发送出去的消息ID |
||
313 | */ |
||
314 | View Code Duplication | public function customImage($media_id = '', $openid, $kf_account = '') |
|
329 | |||
330 | /** |
||
331 | * 视频(请注意,此处视频的media_id需通过POST请求到下述接口特别地得到) |
||
332 | * |
||
333 | * @param string $media_id 用于群发的消息的media_id |
||
334 | * @param string $openid 接收消息用户对应该公众号的openid |
||
335 | * @param string $title 标题 |
||
336 | * @param string $description 描述 |
||
337 | * @param string $kf_account 客服账号 |
||
338 | * |
||
339 | * @return int msg_id 发送出去的消息ID |
||
340 | */ |
||
341 | public function customMpvideo($media_id = '', $title = '', $description = '', $openid, $kf_account = '') |
||
358 | |||
359 | /** |
||
360 | * 卡券(注意图文消息的card_id需要通过上述方法来得到) |
||
361 | * |
||
362 | * @param string $card_id card_id |
||
363 | * @param array $card_ext 签名 |
||
364 | * @param string $openid 接收消息用户对应该公众号的openid |
||
365 | * @param string $kf_account 客服账号 |
||
366 | * |
||
367 | * @return int msg_id 发送出去的消息ID |
||
368 | */ |
||
369 | public function customWxcard($card_id = '', $card_ext = [], $openid, $kf_account = '') |
||
385 | |||
386 | /** |
||
387 | * 发送音乐消息 |
||
388 | * |
||
389 | * @param string $musicurl 音乐链接 |
||
390 | * @param string $openid 接收消息用户对应该公众号的openid |
||
391 | * @param string $thumb_media_id 缩略图的媒体ID |
||
392 | * @param string $hqmusicurl 高品质音乐链接,wifi环境优先使用该链接播放音乐 |
||
393 | * @param string $title 标题 |
||
394 | * @param string $description 描述 |
||
395 | * @param string $kf_account 客服账号 |
||
396 | * |
||
397 | * @return string msg_id |
||
398 | */ |
||
399 | public function customMusic($musicurl = '', $openid = '', $thumb_media_id = '', $hqmusicurl = '', $title = '', $description = '', $kf_account = '') |
||
418 | } |
||
419 |
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.