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 Controller |
||
14 | { |
||
15 | use HasResourceActions; |
||
16 | |||
17 | /** |
||
18 | * Index interface. |
||
19 | * |
||
20 | * @param Content $content |
||
21 | * |
||
22 | * @return Content |
||
23 | */ |
||
24 | public function index(Content $content) |
||
25 | { |
||
26 | return $content |
||
27 | ->header(trans('admin.menu')) |
||
28 | ->description(trans('admin.list')) |
||
29 | ->row(function (Row $row) { |
||
30 | $row->column(6, $this->treeView()->render()); |
||
31 | |||
32 | $row->column(6, function (Column $column) { |
||
33 | $form = new \Encore\Admin\Widgets\Form(); |
||
34 | $form->action(admin_base_path('auth/menu')); |
||
35 | |||
36 | $menuModel = config('admin.database.menu_model'); |
||
37 | $permissionModel = config('admin.database.permissions_model'); |
||
38 | $roleModel = config('admin.database.roles_model'); |
||
39 | |||
40 | $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions()); |
||
41 | $form->text('title', trans('admin.title'))->rules('required'); |
||
42 | $form->icon('icon', trans('admin.icon'))->default('fa-bars')->rules('required')->help($this->iconHelp()); |
||
43 | $form->text('uri', trans('admin.uri')); |
||
44 | $form->multipleSelect('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id')); |
||
45 | if ((new $menuModel())->withPermission()) { |
||
46 | $form->select('permission', trans('admin.permission'))->options($permissionModel::pluck('name', 'slug')); |
||
47 | } |
||
48 | $form->hidden('_token')->default(csrf_token()); |
||
49 | |||
50 | $column->append((new Box(trans('admin.new'), $form))->style('success')); |
||
51 | }); |
||
52 | }); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Redirect to edit page. |
||
57 | * |
||
58 | * @param int $id |
||
59 | * |
||
60 | * @return \Illuminate\Http\RedirectResponse |
||
61 | */ |
||
62 | public function show($id) |
||
66 | |||
67 | /** |
||
68 | * @return \Encore\Admin\Tree |
||
69 | */ |
||
70 | protected function treeView() |
||
94 | |||
95 | /** |
||
96 | * Edit interface. |
||
97 | * |
||
98 | * @param string $id |
||
99 | * @param Content $content |
||
100 | * |
||
101 | * @return Content |
||
102 | */ |
||
103 | View Code Duplication | public function edit($id, Content $content) |
|
110 | |||
111 | /** |
||
112 | * Make a form builder. |
||
113 | * |
||
114 | * @return Form |
||
115 | */ |
||
116 | public function form() |
||
140 | |||
141 | /** |
||
142 | * Help message for icon field. |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function iconHelp() |
||
150 | } |
||
151 |
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.