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 |
||
| 28 | class User extends AbstractAPI |
||
| 29 | { |
||
| 30 | const API_GET = 'https://api.weixin.qq.com/cgi-bin/user/info'; |
||
| 31 | const API_BATCH_GET = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget'; |
||
| 32 | const API_LIST = 'https://api.weixin.qq.com/cgi-bin/user/get'; |
||
| 33 | const API_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/getid'; |
||
| 34 | const API_REMARK = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark'; |
||
| 35 | const API_OAUTH_GET = 'https://api.weixin.qq.com/sns/userinfo'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Fetch a user by open id. |
||
| 39 | * |
||
| 40 | * @param string $openId |
||
| 41 | * @param string $lang |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | 1 | View Code Duplication | public function get($openId, $lang = 'zh_CN') |
| 54 | |||
| 55 | /** |
||
| 56 | * Batch get users. |
||
| 57 | * |
||
| 58 | * @param array $openIds |
||
| 59 | * @param string $lang |
||
| 60 | * |
||
| 61 | * @return \EasyWeChat\Support\Collection |
||
| 62 | */ |
||
| 63 | 1 | public function batchGet(array $openIds, $lang = 'zh_CN') |
|
| 76 | |||
| 77 | /** |
||
| 78 | * List users. |
||
| 79 | * |
||
| 80 | * @param string $nextOpenId |
||
| 81 | * |
||
| 82 | * @return \EasyWeChat\Support\Collection |
||
| 83 | */ |
||
| 84 | 1 | public function lists($nextOpenId = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Set user remark. |
||
| 93 | * |
||
| 94 | * @param string $openId |
||
| 95 | * @param string $remark |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 1 | View Code Duplication | public function remark($openId, $remark) |
| 108 | |||
| 109 | /** |
||
| 110 | * Get user's group id. |
||
| 111 | * |
||
| 112 | * @param string $openId |
||
| 113 | * |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 1 | public function group($openId) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get user's group. |
||
| 123 | * |
||
| 124 | * @param string $openId |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | 1 | public function getGroup($openId) |
|
| 134 | } |
||
| 135 |