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 |
||
10 | class Chat extends AbstractAPI |
||
11 | { |
||
12 | const API_GET = 'https://qyapi.weixin.qq.com/cgi-bin/chat/get'; |
||
13 | const API_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/create'; |
||
14 | const API_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/update'; |
||
15 | const API_QUIT = 'https://qyapi.weixin.qq.com/cgi-bin/chat/quit'; |
||
16 | const API_CLEAR_NOTIFY = 'https://qyapi.weixin.qq.com/cgi-bin/chat/clearnotify'; |
||
17 | const API_SEND = 'https://qyapi.weixin.qq.com/cgi-bin/chat/send'; |
||
18 | const API_SET_MUTE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/setmute'; |
||
19 | |||
20 | const CHAT_TYPE_SINGLE = 'single'; // 单聊 |
||
21 | const CHAT_TYPE_GROUP = 'group'; // 群聊 |
||
22 | |||
23 | const MSG_TYPE_TEXT = 'text'; // 文本 |
||
24 | const MSG_TYPE_VOICE = 'voice'; // 语音 |
||
25 | const MSG_TYPE_IMAGE = 'image'; // 图片 |
||
26 | const MSG_TYPE_FILE = 'file'; // 文件 |
||
27 | const MSG_TYPE_LINK = 'link'; // 文件 |
||
28 | |||
29 | /** |
||
30 | * Fetch a chat by chat id. |
||
31 | * |
||
32 | * @param string $chatId |
||
33 | * |
||
34 | * @return \EntWeChat\Support\Collection |
||
35 | */ |
||
36 | public function get($chatId) |
||
44 | |||
45 | /** |
||
46 | * Create chat. |
||
47 | * |
||
48 | * @param string $chatId |
||
49 | * @param string $name |
||
50 | * @param string $owner |
||
51 | * @param array $userList |
||
52 | * |
||
53 | * @return \EntWeChat\Support\Collection |
||
54 | */ |
||
55 | public function create($chatId, $name, $owner, array $userList) |
||
66 | |||
67 | /** |
||
68 | * Update chat. |
||
69 | * |
||
70 | * @param string $chatId |
||
71 | * @param string $opUser |
||
72 | * @param array $chatInfo |
||
73 | * |
||
74 | * @return \EntWeChat\Support\Collection |
||
75 | */ |
||
76 | View Code Duplication | public function update($chatId, $opUser, array $chatInfo = []) |
|
85 | |||
86 | /** |
||
87 | * Quit chat. |
||
88 | * |
||
89 | * @param string $chatId |
||
90 | * @param string $opUser |
||
91 | * |
||
92 | * @return \EntWeChat\Support\Collection |
||
93 | */ |
||
94 | View Code Duplication | public function quit($chatId, $opUser) |
|
103 | |||
104 | /** |
||
105 | * Clear chat. |
||
106 | * |
||
107 | * @param string $chatId |
||
108 | * @param array $chat |
||
109 | * |
||
110 | * @return \EntWeChat\Support\Collection |
||
111 | */ |
||
112 | public function clear($chatId, array $chat) |
||
121 | |||
122 | /** |
||
123 | * Send chat. |
||
124 | * |
||
125 | * @param string $type |
||
126 | * @param string $id |
||
127 | * @param string $sender |
||
128 | * @param string $msgType |
||
129 | * @param mixed $message |
||
130 | * |
||
131 | * @return \EntWeChat\Support\Collection |
||
132 | */ |
||
133 | public function send($type, $id, $sender, $msgType, $message) |
||
147 | |||
148 | /** |
||
149 | * Set chat. |
||
150 | * |
||
151 | * @return \EntWeChat\Support\Collection |
||
152 | */ |
||
153 | public function mute(array $userMuteList) |
||
161 | } |
||
162 |
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.