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 RoleController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * Show the search page. |
||
17 | * |
||
18 | * @return \Illuminate\View\View |
||
19 | */ |
||
20 | View Code Duplication | public function index(): View |
|
28 | |||
29 | /** |
||
30 | * Show the role create form. |
||
31 | * |
||
32 | * @return \Illuminate\View\View |
||
33 | */ |
||
34 | View Code Duplication | public function showCreateForm(): View |
|
46 | |||
47 | /** |
||
48 | * Handle a role create request for the application. |
||
49 | * |
||
50 | * @param \Illuminate\Http\Request $request |
||
51 | * |
||
52 | * @return \Illuminate\Http\RedirectResponse |
||
53 | */ |
||
54 | public function create(Request $request): RedirectResponse |
||
65 | |||
66 | /** |
||
67 | * Show the update form. |
||
68 | * |
||
69 | * @param \Illuminate\Http\Request $request |
||
70 | * @param string $slug The slug of the role. |
||
71 | * @param int $id The id of the role. |
||
72 | * |
||
73 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
||
74 | */ |
||
75 | public function showUpdateForm(Request $request, string $slug, int $id) |
||
97 | |||
98 | /** |
||
99 | * Handle an user update request for the application. |
||
100 | * |
||
101 | * @param \Illuminate\Http\Request $request |
||
102 | * @param int $id The id of the role to update. |
||
103 | * |
||
104 | * @return \Illuminate\Http\RedirectResponse |
||
105 | */ |
||
106 | public function update(Request $request, int $id): RedirectResponse |
||
119 | |||
120 | /** |
||
121 | * Handle the delete request for the role. |
||
122 | * |
||
123 | * @param int $id The id of the role to delete. |
||
124 | * |
||
125 | * @return \Illuminate\Http\RedirectResponse |
||
126 | */ |
||
127 | public function delete(int $id): RedirectResponse |
||
155 | |||
156 | /** |
||
157 | * Return a list of attributes for the permissions option field. |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function getOptionAttributes(): array |
||
178 | } |
||
179 |
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.