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 ArticleController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * Show all articles. |
||
17 | * |
||
18 | * @return \Illuminate\View\View |
||
19 | */ |
||
20 | View Code Duplication | public function index(): View |
|
29 | |||
30 | /** |
||
31 | * Show the article create form. |
||
32 | * |
||
33 | * @return \Illuminate\View\View |
||
34 | */ |
||
35 | View Code Duplication | public function showCreateForm(): View |
|
45 | |||
46 | /** |
||
47 | * Handle an article create request for the application. |
||
48 | * |
||
49 | * @param \Illuminate\Http\Request $request |
||
50 | * |
||
51 | * @return \Illuminate\Http\RedirectResponse |
||
52 | */ |
||
53 | public function create(Request $request): RedirectResponse |
||
63 | |||
64 | /** |
||
65 | * Show the article update form. |
||
66 | * |
||
67 | * @param string $slug The slug of the article. |
||
68 | * @param int $id The id of the article. |
||
69 | * |
||
70 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
||
71 | */ |
||
72 | public function showUpdateForm(string $slug, int $id) |
||
97 | |||
98 | /** |
||
99 | * Handle an article update request for the application. |
||
100 | * |
||
101 | * @param \Illuminate\Http\Request $request |
||
102 | * @param int $id The id of the article. |
||
103 | * |
||
104 | * @return \Illuminate\Http\RedirectResponse |
||
105 | */ |
||
106 | public function update(Request $request, int $id): RedirectResponse |
||
118 | |||
119 | /** |
||
120 | * Handle the delete request for the article. |
||
121 | * |
||
122 | * @param int $id The id of the article to delete. |
||
123 | * |
||
124 | * @return \Illuminate\Http\RedirectResponse |
||
125 | */ |
||
126 | public function delete(int $id): RedirectResponse |
||
146 | } |
||
147 |
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.