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 User extends AbstractAPI |
||
11 | { |
||
12 | const API_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/user/create'; |
||
13 | const API_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/user/update'; |
||
14 | const API_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/user/delete'; |
||
15 | const API_BATCH_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete'; |
||
16 | const API_GET = 'https://qyapi.weixin.qq.com/cgi-bin/user/get'; |
||
17 | const API_BATCH_GET = 'https://qyapi.weixin.qq.com/cgi-bin/user/simplelist'; |
||
18 | const API_LIST = 'https://qyapi.weixin.qq.com/cgi-bin/user/list'; |
||
19 | const API_TO_OPENID = 'https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid'; |
||
20 | const API_TO_USERID = 'https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid'; |
||
21 | |||
22 | /** |
||
23 | * Fetch a user by user id. |
||
24 | * |
||
25 | * @param string $userId |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | View Code Duplication | public function get($userId) |
|
37 | |||
38 | /** |
||
39 | * Batch get users. |
||
40 | * |
||
41 | * @param int $departmentId |
||
42 | * @param int|null $fetchChild |
||
43 | * @param int|null $status |
||
44 | * |
||
45 | * @return \EntWeChat\Support\Collection |
||
46 | */ |
||
47 | View Code Duplication | public function batchGet($departmentId, $fetchChild = null, $status = null) |
|
57 | |||
58 | /** |
||
59 | * List users. |
||
60 | * |
||
61 | * @param int $departmentId |
||
62 | * @param int|null $fetchChild |
||
63 | * @param int|null $status |
||
64 | * |
||
65 | * @return \EntWeChat\Support\Collection |
||
66 | */ |
||
67 | View Code Duplication | public function lists($departmentId, $fetchChild = null, $status = null) |
|
77 | |||
78 | /** |
||
79 | * Create a user. |
||
80 | * |
||
81 | * @param array $userInfo |
||
82 | * |
||
83 | * @return \EntWeChat\Support\Collection |
||
84 | */ |
||
85 | public function create(array $userInfo = []) |
||
91 | |||
92 | /** |
||
93 | * Update a user. |
||
94 | * |
||
95 | * @param string $userId |
||
96 | * @param array $userInfo |
||
97 | * |
||
98 | * @return \EntWeChat\Support\Collection |
||
99 | */ |
||
100 | public function update($userId, array $userInfo = []) |
||
106 | |||
107 | /** |
||
108 | * Delete a user. |
||
109 | * |
||
110 | * @param string $userId |
||
111 | * |
||
112 | * @return \EntWeChat\Support\Collection |
||
113 | */ |
||
114 | public function delete($userId) |
||
120 | |||
121 | /** |
||
122 | * Batch delete users. |
||
123 | * |
||
124 | * @param array $userIds |
||
125 | * |
||
126 | * @return \EntWeChat\Support\Collection |
||
127 | */ |
||
128 | public function batchDelete(array $userIds) |
||
134 | |||
135 | /** |
||
136 | * Convert userid to openid. |
||
137 | * |
||
138 | * @param string $userId |
||
139 | * @param int $agentId |
||
140 | * |
||
141 | * @return \EntWeChat\Support\Collection |
||
142 | */ |
||
143 | View Code Duplication | public function toOpenId($userId, $agentId) |
|
152 | |||
153 | /** |
||
154 | * Convert openid to userid. |
||
155 | * |
||
156 | * @param string $openId |
||
157 | * |
||
158 | * @return \EntWeChat\Support\Collection |
||
159 | */ |
||
160 | public function toUserId($openId) |
||
166 | } |
||
167 |
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.