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 |
||
16 | final class SettingsController extends AbstractController |
||
17 | { |
||
18 | /** |
||
19 | * @var SettingsRepository |
||
20 | */ |
||
21 | private $repository; |
||
22 | |||
23 | /** |
||
24 | * @var SettingsService |
||
25 | */ |
||
26 | private $service; |
||
27 | |||
28 | public function __construct(SettingsRepository $repository, SettingsService $service) |
||
33 | |||
34 | /** |
||
35 | * @Route("/admin/settings", name="admin_settings") |
||
36 | */ |
||
37 | View Code Duplication | public function settings(Request $request): Response |
|
53 | |||
54 | /** |
||
55 | * @Route("/admin/setting/header", name="admin_header_settings") |
||
56 | */ |
||
57 | public function changeHeaderImage(Request $request): Response |
||
75 | |||
76 | /** |
||
77 | * @Route("/admin/setting/upload_header_image", methods={"POST"}, name="admin_setting_upload_header_image") |
||
78 | * |
||
79 | * @throws \Exception |
||
80 | */ |
||
81 | public function uploadHeaderImage(Request $request): Response |
||
86 | |||
87 | /** |
||
88 | * @Route("/admin/setting/upload_logo_image", methods={"POST"}, name="admin_setting_upload_logo_image") |
||
89 | * |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | public function uploadLogoImage(Request $request): Response |
||
97 | |||
98 | /** |
||
99 | * @Route("/admin/setting/delete_header_image", methods={"POST"}, name="admin_setting_delete_header_image") |
||
100 | */ |
||
101 | public function deleteHeaderImage(Request $request): Response |
||
108 | |||
109 | /** |
||
110 | * @Route("/admin/setting/delete_logo_image", methods={"POST"}, name="admin_setting_delete_logo_image") |
||
111 | */ |
||
112 | public function deleteLogoImage(Request $request): Response |
||
119 | } |
||
120 |
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.