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 |
||
24 | class BusinessTemplateController extends Controller |
||
25 | { |
||
26 | use VictoireAlertifyControllerTrait; |
||
27 | |||
28 | /** |
||
29 | * List all business entity page pattern. |
||
30 | * |
||
31 | * @Route("/", name="victoire_business_template_index") |
||
32 | * |
||
33 | * @return JsonResponse |
||
34 | */ |
||
35 | public function indexAction() |
||
69 | |||
70 | /** |
||
71 | * show BusinessTemplate. |
||
72 | * |
||
73 | * @Route("/show/{id}", name="victoire_business_template_show") |
||
74 | * @ParamConverter("template", class="VictoireBusinessPageBundle:BusinessTemplate") |
||
75 | * |
||
76 | * @return Response |
||
77 | */ |
||
78 | public function showAction(BusinessTemplate $view) |
||
94 | |||
95 | /** |
||
96 | * Creates a new BusinessTemplate entity. |
||
97 | * |
||
98 | * @param Request $request |
||
99 | * @param int $id |
||
100 | * |
||
101 | * @Route("{id}/create", name="victoire_business_template_create") |
||
102 | * @Method("POST") |
||
103 | * @Template("VictoireBusinessPageBundle:BusinessTemplate:new.html.twig") |
||
104 | * |
||
105 | * @return JsonResponse |
||
106 | */ |
||
107 | public function createAction(Request $request, $id) |
||
141 | |||
142 | /** |
||
143 | * Creates a form to create a BusinessTemplate entity. |
||
144 | * |
||
145 | * @param BusinessTemplate $view The entity |
||
146 | * |
||
147 | * @return \Symfony\Component\Form\Form The form |
||
148 | * @return Form |
||
149 | */ |
||
150 | View Code Duplication | private function createCreateForm(BusinessTemplate $view) |
|
|
|||
151 | { |
||
152 | $id = $view->getBusinessEntityId(); |
||
153 | |||
154 | $businessProperties = $this->getBusinessProperties($view); |
||
155 | $form = $this->createForm( |
||
156 | BusinessTemplateType::class, |
||
157 | $view, |
||
158 | [ |
||
159 | 'action' => $this->generateUrl('victoire_business_template_create', ['id' => $id]), |
||
160 | 'method' => 'POST', |
||
161 | 'vic_business_properties' => $businessProperties, |
||
162 | ] |
||
163 | ); |
||
164 | |||
165 | return $form; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Displays a form to create a new BusinessTemplate entity. |
||
170 | * |
||
171 | * @param string $id The id of the businessEntity |
||
172 | * |
||
173 | * @Route("/{id}/new", name="victoire_business_template_new") |
||
174 | * @Method("GET") |
||
175 | * @Template() |
||
176 | * |
||
177 | * @return JsonResponse The entity and the form |
||
178 | */ |
||
179 | View Code Duplication | public function newAction($id) |
|
203 | |||
204 | /** |
||
205 | * Displays a form to edit an existing BusinessTemplate entity. |
||
206 | * |
||
207 | * @Route("/{id}/edit", name="victoire_business_template_edit") |
||
208 | * @Method("GET") |
||
209 | * @Template() |
||
210 | * @ParamConverter("id", class="VictoireCoreBundle:View") |
||
211 | * |
||
212 | * @throws \Exception |
||
213 | * |
||
214 | * @return JsonResponse The entity and the form |
||
215 | */ |
||
216 | View Code Duplication | public function editAction(View $view) |
|
238 | |||
239 | /** |
||
240 | * Creates a form to edit a BusinessTemplate entity. |
||
241 | * |
||
242 | * @param BusinessTemplate $view The entity |
||
243 | * |
||
244 | * @return \Symfony\Component\Form\Form The form |
||
245 | */ |
||
246 | View Code Duplication | private function createEditForm(BusinessTemplate $view) |
|
247 | { |
||
248 | $businessProperties = $this->getBusinessProperties($view); |
||
249 | |||
250 | $form = $this->createForm(BusinessTemplateType::class, $view, [ |
||
251 | 'action' => $this->generateUrl('victoire_business_template_update', ['id' => $view->getId()]), |
||
252 | 'method' => 'PUT', |
||
253 | 'vic_business_properties' => $businessProperties, |
||
254 | ]); |
||
255 | |||
256 | return $form; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Edits an existing BusinessTemplate entity. |
||
261 | * |
||
262 | * @param Request $request |
||
263 | * @param string $id |
||
264 | * |
||
265 | * @Route("/{id}", name="victoire_business_template_update") |
||
266 | * @Method("PUT") |
||
267 | * @Template("VictoireBusinessPageBundle:BusinessTemplate:edit.html.twig") |
||
268 | * |
||
269 | * @throws \Exception |
||
270 | * |
||
271 | * @return JsonResponse The parameter for the response |
||
272 | */ |
||
273 | public function updateAction(Request $request, $id) |
||
307 | |||
308 | /** |
||
309 | * Deletes a BusinessTemplate entity. |
||
310 | * |
||
311 | * @param Request $request |
||
312 | * @param string $id |
||
313 | * |
||
314 | * @Route("/{id}", name="victoire_business_template_delete") |
||
315 | * @Method("DELETE") |
||
316 | * |
||
317 | * @throws \Exception |
||
318 | * |
||
319 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
320 | */ |
||
321 | View Code Duplication | public function deleteAction(Request $request, $id) |
|
340 | |||
341 | /** |
||
342 | * Creates a form to delete a BusinessTemplate entity by id. |
||
343 | * |
||
344 | * @param string $id The entity id |
||
345 | * |
||
346 | * @return \Symfony\Component\Form\Form The form |
||
347 | */ |
||
348 | View Code Duplication | private function createDeleteForm($id) |
|
356 | |||
357 | /** |
||
358 | * List the entities that matches the query of the BusinessTemplate. |
||
359 | * |
||
360 | * @param BusinessTemplate $view |
||
361 | * |
||
362 | * @Route("/listEntities/{id}", name="victoire_business_template_listentities") |
||
363 | * @ParamConverter("id", class="VictoireBusinessPageBundle:BusinessTemplate") |
||
364 | * @Template |
||
365 | * |
||
366 | * @throws Exception |
||
367 | * |
||
368 | * @return array|Response The list of items for this template |
||
369 | */ |
||
370 | public function listEntitiesAction(BusinessTemplate $view) |
||
381 | |||
382 | /** |
||
383 | * Get an array of business properties by the business entity page pattern. |
||
384 | * |
||
385 | * @param BusinessTemplate $view |
||
386 | * |
||
387 | * @return array of business properties |
||
388 | */ |
||
389 | private function getBusinessProperties(BusinessTemplate $view) |
||
400 | |||
401 | /** |
||
402 | * @param string $id The id of the business entity |
||
403 | * |
||
404 | * @throws Exception If the business entity was not found |
||
405 | * |
||
406 | * @return template |
||
407 | */ |
||
408 | View Code Duplication | private function getBusinessEntity($id) |
|
423 | } |
||
424 |
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.