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 |
||
11 | class MenuApi extends BaseApi |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * 设置菜单 |
||
16 | * |
||
17 | * @param $menus |
||
18 | * |
||
19 | * @return array|bool |
||
20 | */ |
||
21 | View Code Duplication | public function set($menus) |
|
|
|||
22 | { |
||
23 | if ($menus instanceof Closure) { |
||
24 | $menus = $menus($this); |
||
25 | } |
||
26 | |||
27 | if (!is_array($menus)) { |
||
28 | $this->setError('子菜单必须是数组或者匿名函数返回数组'); |
||
29 | |||
30 | return false; |
||
31 | } |
||
32 | |||
33 | $menus = $this->extractMenus($menus); |
||
34 | |||
35 | $data = ['button' => $menus]; |
||
36 | |||
37 | $res = $this->_post('create', $data); |
||
38 | |||
39 | return $res; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * 获取菜单 |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function get() |
||
55 | |||
56 | /** |
||
57 | * 获取菜单【查询接口,能获取到任意方式设置的菜单】 |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function current() |
||
71 | |||
72 | /** |
||
73 | * 删除菜单 |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function delete() |
||
85 | |||
86 | /** |
||
87 | * 转menu为数组 |
||
88 | * |
||
89 | * @param array $menus |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function extractMenus(array $menus) |
||
105 | |||
106 | /** |
||
107 | * 设置个性化菜单 |
||
108 | * |
||
109 | * @author Jia <[email protected]> |
||
110 | * |
||
111 | * @date 2017-04-11 |
||
112 | * |
||
113 | * @param array $menus 菜单数组 |
||
114 | * @param array $matchrule 个性化规则 |
||
115 | * |
||
116 | * @return array|bool |
||
117 | */ |
||
118 | View Code Duplication | public function setIndividuationMenu($menus, $matchrule) |
|
140 | |||
141 | /** |
||
142 | * 测试个性化菜单匹配结果 |
||
143 | * |
||
144 | * @author Jia <[email protected]> |
||
145 | * |
||
146 | * @date 2017-04-11 |
||
147 | * |
||
148 | * @param string $openid 用户openid |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function tryMatchUser($openid) |
||
162 | |||
163 | /** |
||
164 | * 删除个性化菜单 |
||
165 | * |
||
166 | * @author Jia <[email protected]> |
||
167 | * |
||
168 | * @date 2017-04-12 |
||
169 | * |
||
170 | * @param int $menuId 个性化菜单id |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | public function deleteIndividuationMenu($menuId) |
||
184 | } |
||
185 |
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.