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 PermissionController extends Controller |
||
13 | { |
||
14 | /** |
||
15 | * Show all the permissions. |
||
16 | * |
||
17 | * @return \Illuminate\View\View |
||
18 | */ |
||
19 | View Code Duplication | public function index(): View |
|
27 | |||
28 | /** |
||
29 | * Show the permission create form. |
||
30 | * |
||
31 | * @return \Illuminate\View\View |
||
32 | */ |
||
33 | public function showCreateForm(): View |
||
34 | { |
||
35 | $breadcrumbs = $this->breadcrumbs |
||
36 | ->addCrumb('Manage Permissions', route('admin.role.permission.index')) |
||
37 | ->addCrumb("Create", route('admin.role.permission.create')); |
||
38 | |||
39 | return view('Admin::Role.permission.create', compact('breadcrumbs')); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Handle a permission create request for the application. |
||
44 | * |
||
45 | * @param \Illuminate\Http\Request $request |
||
46 | * |
||
47 | * @return \Illuminate\Http\RedirectResponse |
||
48 | */ |
||
49 | public function create(Request $request): RedirectResponse |
||
58 | |||
59 | /** |
||
60 | * Show the update form. |
||
61 | * |
||
62 | * @param \Illuminate\Http\Request $request |
||
63 | * @param int $id The id of the permission. |
||
64 | * |
||
65 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
||
66 | */ |
||
67 | public function showUpdateForm(Request $request, int $id) |
||
84 | |||
85 | /** |
||
86 | * Handle an permission update request for the application. |
||
87 | * |
||
88 | * @param \Illuminate\Http\Request $request |
||
89 | * @param int $id The id of the permission to update. |
||
90 | * |
||
91 | * @return \Illuminate\Http\RedirectResponse |
||
92 | */ |
||
93 | View Code Duplication | public function update(Request $request, int $id): RedirectResponse |
|
104 | |||
105 | /** |
||
106 | * Handle the delete request for the permission. |
||
107 | * |
||
108 | * @param int $id The id of the permission to delete. |
||
109 | * |
||
110 | * @return \Illuminate\Http\RedirectResponse |
||
111 | */ |
||
112 | public function delete(int $id): RedirectResponse |
||
132 | } |
||
133 |
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.