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 |
||
29 | class Staff extends AbstractAPI |
||
30 | { |
||
31 | const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist'; |
||
32 | const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist'; |
||
33 | const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del'; |
||
34 | const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update'; |
||
35 | const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add'; |
||
36 | const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send'; |
||
37 | const API_AVATAR_UPLOAD = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg'; |
||
38 | |||
39 | /** |
||
40 | * List all staffs. |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | 1 | public function lists() |
|
48 | |||
49 | /** |
||
50 | * List all online staffs. |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | 1 | public function onlines() |
|
58 | |||
59 | /** |
||
60 | * Create a staff. |
||
61 | * |
||
62 | * @param string $email |
||
63 | * @param string $nickname |
||
64 | * @param string $password |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | 1 | View Code Duplication | public function create($email, $nickname, $password) |
78 | |||
79 | /** |
||
80 | * Update a staff. |
||
81 | * |
||
82 | * @param string $email |
||
83 | * @param string $nickname |
||
84 | * @param string $password |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | 1 | View Code Duplication | public function update($email, $nickname, $password) |
98 | |||
99 | /** |
||
100 | * Delete a staff. |
||
101 | * |
||
102 | * @param string $email |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function delete($email) |
||
107 | { |
||
108 | // XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode, |
||
109 | // 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。 |
||
110 | // 简直是日了... |
||
111 | // #222 |
||
112 | // PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。 |
||
113 | $accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken()); |
||
114 | $url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $email); |
||
115 | |||
116 | $contents = $this->getHttp()->parseJSON(file_get_contents($url)); |
||
117 | |||
118 | $this->checkAndThrow($contents); |
||
119 | |||
120 | return new Collection($contents); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Set staff avatar. |
||
125 | * |
||
126 | * @param string $email |
||
127 | * @param string $path |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 1 | public function avatar($email, $path) |
|
135 | |||
136 | /** |
||
137 | * Get message builder. |
||
138 | * |
||
139 | * @param \EasyWeChat\Message\AbstractMessage|string $message |
||
140 | * |
||
141 | * @return \EasyWeChat\Staff\MessageBuilder |
||
142 | * |
||
143 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
144 | */ |
||
145 | public function message($message) |
||
151 | |||
152 | /** |
||
153 | * Send a message. |
||
154 | * |
||
155 | * @param string|array $message |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function send($message) |
||
163 | } |
||
164 |