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 |
||
13 | class MenuController extends BaseController |
||
14 | { |
||
15 | private $permissionModel; |
||
16 | |||
17 | /** |
||
18 | * 菜单列表 |
||
19 | * |
||
20 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
21 | */ |
||
22 | public function getList() |
||
23 | { |
||
24 | $menus = $this->getPermissionModel()->getMenuTreeList(); |
||
25 | |||
26 | return response()->json($menus); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * 新增菜单 |
||
31 | * |
||
32 | * @param int $pid |
||
33 | * |
||
34 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
35 | */ |
||
36 | public function getAdd($pid = 0) |
||
45 | |||
46 | /** |
||
47 | * 保存菜单信息 |
||
48 | * |
||
49 | * @param \Illuminate\Http\Request $request |
||
50 | * |
||
51 | * @return \Illuminate\Http\JsonResponse |
||
52 | */ |
||
53 | View Code Duplication | public function postAdd(Request $request) |
|
65 | |||
66 | /** |
||
67 | * 编辑菜单 |
||
68 | * |
||
69 | * @param integer $id 菜单ID |
||
70 | * |
||
71 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
72 | */ |
||
73 | public function getEdit($id) |
||
80 | |||
81 | /** |
||
82 | * 保存菜单信息 |
||
83 | * |
||
84 | * @param \Illuminate\Http\Request $request 提交数据 |
||
85 | * @param integer $id 菜单ID |
||
86 | * |
||
87 | * @return \Illuminate\Http\JsonResponse |
||
88 | */ |
||
89 | View Code Duplication | public function postEdit(Request $request, $id) |
|
101 | |||
102 | /** |
||
103 | * 删除菜单 |
||
104 | * |
||
105 | * @param integer $id |
||
106 | */ |
||
107 | public function getDelete($id) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @return \Sco\Admin\Models\Permission |
||
114 | */ |
||
115 | private function getPermissionModel() |
||
123 | } |
||
124 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.