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 |
||
12 | class Permission extends EntrustPermission |
||
13 | { |
||
14 | use TreeTrait; |
||
15 | |||
16 | protected $guarded = ['created_at', 'updated_at']; |
||
17 | |||
18 | protected $treeNodeParentIdName = 'pid'; |
||
19 | |||
20 | private $allRoutes = null; |
||
21 | |||
22 | private $validList = null; |
||
23 | |||
24 | private $permList = null; |
||
25 | |||
26 | private $menuList = null; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * @return \Illuminate\Support\Collection |
||
31 | */ |
||
32 | public function getMenuTreeList() |
||
38 | |||
39 | private function getAll() |
||
51 | |||
52 | /** |
||
53 | * Tree Trait 获取所有节点 |
||
54 | * |
||
55 | * @return mixed|null |
||
56 | */ |
||
57 | protected function getTreeAllNodes() |
||
61 | |||
62 | /** |
||
63 | * 获取有效的路由列表 |
||
64 | * |
||
65 | * @return \Illuminate\Support\Collection |
||
66 | */ |
||
67 | public function getValidRouteList() |
||
83 | |||
84 | /** |
||
85 | * 获取权限列表 |
||
86 | * |
||
87 | * @return \Illuminate\Support\Collection|null |
||
88 | */ |
||
89 | public function getPermRouteList() |
||
97 | |||
98 | public function getMenuList() |
||
115 | |||
116 | public function getInfoById($id) |
||
120 | |||
121 | public function getInfoByName($name) |
||
129 | |||
130 | public function getParentTree($id) |
||
134 | |||
135 | View Code Duplication | public function getParentTreeAndSelfById($id) |
|
145 | |||
146 | View Code Duplication | public function getParentTreeAndSelfByName($name) |
|
156 | |||
157 | public function saveMenu(Request $request) |
||
178 | |||
179 | public function deleteMenu($id) |
||
196 | |||
197 | private function clearCache() |
||
201 | } |
||
202 |
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.