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 User extends AbstractAPI |
||
30 | { |
||
31 | const API_GET = 'https://api.weixin.qq.com/cgi-bin/user/info'; |
||
32 | const API_BATCH_GET = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget'; |
||
33 | const API_LIST = 'https://api.weixin.qq.com/cgi-bin/user/get'; |
||
34 | const API_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/getid'; |
||
35 | const API_REMARK = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark'; |
||
36 | const API_OAUTH_GET = 'https://api.weixin.qq.com/sns/userinfo'; |
||
37 | const API_GET_BLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist'; |
||
38 | const API_BATCH_BLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist'; |
||
39 | const API_BATCH_UNBLACK_LIST = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist'; |
||
40 | |||
41 | /** |
||
42 | * Fetch a user by open id. |
||
43 | * |
||
44 | * @param string $openId |
||
45 | * @param string $lang |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | 1 | View Code Duplication | public function get($openId, $lang = 'zh_CN') |
58 | |||
59 | /** |
||
60 | * Batch get users. |
||
61 | * |
||
62 | * @param array $openIds |
||
63 | * @param string $lang |
||
64 | * |
||
65 | * @return \EasyWeChat\Support\Collection |
||
66 | */ |
||
67 | 1 | public function batchGet(array $openIds, $lang = 'zh_CN') |
|
80 | |||
81 | /** |
||
82 | * List users. |
||
83 | * |
||
84 | * @param string $nextOpenId |
||
85 | * |
||
86 | * @return \EasyWeChat\Support\Collection |
||
87 | */ |
||
88 | 1 | public function lists($nextOpenId = null) |
|
94 | |||
95 | /** |
||
96 | * Set user remark. |
||
97 | * |
||
98 | * @param string $openId |
||
99 | * @param string $remark |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | 1 | View Code Duplication | public function remark($openId, $remark) |
112 | |||
113 | /** |
||
114 | * Get user's group id. |
||
115 | * |
||
116 | * @param string $openId |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | 1 | public function group($openId) |
|
124 | |||
125 | /** |
||
126 | * Get user's group. |
||
127 | * |
||
128 | * @param string $openId |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 1 | public function getGroup($openId) |
|
138 | |||
139 | /** |
||
140 | * Get black list. |
||
141 | * |
||
142 | * @param string|null $beginOpenid |
||
143 | * |
||
144 | * @return \EasyWeChat\Support\Collection |
||
145 | */ |
||
146 | 1 | public function blacklist($beginOpenid = null) |
|
152 | |||
153 | /** |
||
154 | * Batch block user. |
||
155 | * |
||
156 | * @param array $openidList |
||
157 | * |
||
158 | * @return \EasyWeChat\Support\Collection |
||
159 | */ |
||
160 | 1 | public function batchBlock(array $openidList) |
|
166 | |||
167 | /** |
||
168 | * Batch unblock user. |
||
169 | * |
||
170 | * @param array $openidList |
||
171 | * |
||
172 | * @return \EasyWeChat\Support\Collection |
||
173 | */ |
||
174 | 1 | public function batchUnblock(array $openidList) |
|
180 | } |
||
181 |