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 |
||
9 | class UserApi extends BaseApi |
||
10 | { |
||
11 | /** |
||
12 | * 获取用户信息. |
||
13 | * |
||
14 | * @author Tian |
||
15 | * |
||
16 | * @param string $openid 用户openid |
||
17 | * @param string $lang |
||
18 | * |
||
19 | * @return array 用户信息. |
||
20 | */ |
||
21 | public function getUserMsg($openid, $lang = 'zh_CN') |
||
32 | |||
33 | /** |
||
34 | * 批量获取用户基本信息. |
||
35 | * |
||
36 | * @author Tian |
||
37 | * |
||
38 | * @param array $user_list 用户openid列表 |
||
39 | * |
||
40 | * @return array|bool 用户信息. |
||
41 | */ |
||
42 | View Code Duplication | public function getUserList(array $user_list) |
|
56 | |||
57 | /** |
||
58 | * 获取用户Openid列表. |
||
59 | * |
||
60 | * @author Tian |
||
61 | * |
||
62 | * @param string $next_openid 下一个openid |
||
63 | * |
||
64 | * @return array Openid列表. |
||
65 | */ |
||
66 | public function getUserOpenidList($next_openid = '') |
||
76 | |||
77 | /** |
||
78 | * 设置用户备注名. |
||
79 | * |
||
80 | * @author Tian |
||
81 | * |
||
82 | * @param string $openid 用户openid sting |
||
83 | * @param string $remark 用户备注名,长度必须小于30字符 |
||
84 | * |
||
85 | * @return string bool. |
||
86 | */ |
||
87 | public function setUserRemark($openid, $remark = "") |
||
98 | } |
||
99 |
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.