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 |
||
18 | class CountryMappingController extends Controller |
||
19 | { |
||
20 | /** |
||
21 | * @Method("GET") |
||
22 | * @Route("", name="loevgaard_pakkelabels_country_mapping_index") |
||
23 | * |
||
24 | * @param Request $request |
||
25 | * |
||
26 | * @return Response |
||
27 | */ |
||
28 | View Code Duplication | public function indexAction(Request $request) |
|
39 | |||
40 | /** |
||
41 | * @Method("GET") |
||
42 | * @Route("/{id}/show", name="loevgaard_pakkelabels_country_mapping_show") |
||
43 | * |
||
44 | * @param CountryMapping $countryMapping |
||
45 | * |
||
46 | * @return Response |
||
47 | */ |
||
48 | public function showAction(CountryMapping $countryMapping) |
||
54 | |||
55 | /** |
||
56 | * @Method({"GET", "POST"}) |
||
57 | * @Route("/new", name="loevgaard_pakkelabels_country_mapping_new") |
||
58 | * |
||
59 | * @param Request $request |
||
60 | * |
||
61 | * @return Response |
||
62 | */ |
||
63 | View Code Duplication | public function newAction(Request $request) |
|
64 | { |
||
65 | $countryMapping = new CountryMapping(); |
||
66 | $form = $this->getForm($countryMapping); |
||
67 | $res = $this->handleUpdate($form, $countryMapping, $request); |
||
68 | if ($res) { |
||
69 | return $res; |
||
70 | } |
||
71 | |||
72 | return $this->updateResponse($countryMapping, $form); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @Method({"GET", "POST"}) |
||
77 | * @Route("/{id}/edit", name="loevgaard_pakkelabels_country_mapping_edit") |
||
78 | * |
||
79 | * @param CountryMapping $countryMapping |
||
80 | * @param Request $request |
||
81 | * |
||
82 | * @return Response |
||
83 | */ |
||
84 | View Code Duplication | public function editAction(CountryMapping $countryMapping, Request $request) |
|
85 | { |
||
86 | $form = $this->getForm($countryMapping); |
||
87 | $res = $this->handleUpdate($form, $countryMapping, $request); |
||
88 | if ($res) { |
||
89 | return $res; |
||
90 | } |
||
91 | |||
92 | return $this->updateResponse($countryMapping, $form); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param Form $form |
||
97 | * @param CountryMapping $countryMapping |
||
98 | * @param Request $request |
||
99 | * |
||
100 | * @return null|RedirectResponse |
||
101 | */ |
||
102 | private function handleUpdate(Form $form, CountryMapping $countryMapping, Request $request) |
||
129 | |||
130 | /** |
||
131 | * @param CountryMapping $countryMapping |
||
132 | * @param Form $form |
||
133 | * |
||
134 | * @return Response |
||
135 | */ |
||
136 | private function updateResponse(CountryMapping $countryMapping, Form $form): Response |
||
143 | |||
144 | /** |
||
145 | * @param CountryMapping $countryMapping |
||
146 | * |
||
147 | * @return Form |
||
148 | */ |
||
149 | private function getForm(CountryMapping $countryMapping): Form |
||
153 | } |
||
154 |
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.