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 |
||
19 | View Code Duplication | class ShippingMethodMappingController extends Controller |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @Method("GET") |
||
23 | * @Route("", name="loevgaard_pakkelabels_shipping_method_mapping_index") |
||
24 | * |
||
25 | * @param Request $request |
||
26 | * |
||
27 | * @return Response |
||
28 | */ |
||
29 | public function indexAction(Request $request) |
||
53 | |||
54 | /** |
||
55 | * @Method("GET") |
||
56 | * @Route("/{id}/show", name="loevgaard_pakkelabels_shipping_method_mapping_show") |
||
57 | * |
||
58 | * @param ShippingMethodMapping $shippingMethodMapping |
||
59 | * |
||
60 | * @return Response |
||
61 | */ |
||
62 | public function showAction(ShippingMethodMapping $shippingMethodMapping) |
||
68 | |||
69 | /** |
||
70 | * @Method({"GET", "POST"}) |
||
71 | * @Route("/new", name="loevgaard_pakkelabels_shipping_method_mapping_new") |
||
72 | * |
||
73 | * @param Request $request |
||
74 | * |
||
75 | * @return Response |
||
76 | */ |
||
77 | public function newAction(Request $request) |
||
88 | |||
89 | /** |
||
90 | * @Method({"GET", "POST"}) |
||
91 | * @Route("/{id}/edit", name="loevgaard_pakkelabels_shipping_method_mapping_edit") |
||
92 | * |
||
93 | * @param ShippingMethodMapping $shippingMethodMapping |
||
94 | * @param Request $request |
||
95 | * |
||
96 | * @return Response |
||
97 | */ |
||
98 | public function editAction(ShippingMethodMapping $shippingMethodMapping, Request $request) |
||
108 | |||
109 | /** |
||
110 | * @param Form $form |
||
111 | * @param ShippingMethodMapping $shippingMethodMapping |
||
112 | * @param Request $request |
||
113 | * |
||
114 | * @return null|RedirectResponse |
||
115 | */ |
||
116 | private function handleUpdate(Form $form, ShippingMethodMapping $shippingMethodMapping, Request $request) |
||
143 | |||
144 | /** |
||
145 | * @param ShippingMethodMapping $shippingMethodMapping |
||
146 | * @param Form $form |
||
147 | * |
||
148 | * @return Response |
||
149 | */ |
||
150 | private function updateResponse(ShippingMethodMapping $shippingMethodMapping, Form $form): Response |
||
157 | |||
158 | /** |
||
159 | * @param ShippingMethodMapping $shippingMethodMapping |
||
160 | * |
||
161 | * @return Form |
||
162 | */ |
||
163 | private function getForm(ShippingMethodMapping $shippingMethodMapping): Form |
||
167 | } |
||
168 |
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.