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:
Complex classes like WidgetController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WidgetController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class WidgetController extends Controller |
||
25 | { |
||
26 | use VictoireAlertifyControllerTrait; |
||
27 | |||
28 | /** |
||
29 | * Show a widget. |
||
30 | * |
||
31 | * @param Request $request |
||
32 | * @param Widget $widget |
||
33 | * @param int $viewReferenceId |
||
34 | * |
||
35 | * @Route("/victoire-dcms-public/widget/show/{id}/{viewReferenceId}", name="victoire_core_widget_show", options={"expose"=true}) |
||
36 | * @Template() |
||
37 | * @ParamConverter("id", class="VictoireWidgetBundle:Widget") |
||
38 | * |
||
39 | * @throws Exception |
||
40 | * |
||
41 | * @return Response |
||
42 | */ |
||
43 | public function showAction(Request $request, Widget $widget, $viewReferenceId) |
||
|
|||
44 | { |
||
45 | try { |
||
46 | $view = $this->get('victoire_page.page_helper')->findPageByParameters(['id' => $viewReferenceId]); |
||
47 | $this->get('victoire_widget_map.builder')->build($view); |
||
48 | $this->get('victoire_core.current_view')->setCurrentView($view); |
||
49 | $response = new JsonResponse([ |
||
50 | 'html' => $this->get('victoire_widget.widget_renderer')->render($widget, $view), |
||
51 | 'update' => 'vic-widget-'.$widget->getId().'-container', |
||
52 | 'success' => true, |
||
53 | ] |
||
54 | ); |
||
55 | } catch (Exception $ex) { |
||
56 | $response = $this->getJsonReponseFromException($ex); |
||
57 | } |
||
58 | |||
59 | return $response; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * API widgets function. |
||
64 | * |
||
65 | * @param string $widgetIds the widget ids to fetch in json |
||
66 | * @param int $viewReferenceId |
||
67 | * |
||
68 | * @Route("/victoire-dcms-public/api/widgets/{widgetIds}/{viewReferenceId}", name="victoire_core_widget_apiWidgets", options={"expose"=true}) |
||
69 | * |
||
70 | * @return JsonResponse |
||
71 | */ |
||
72 | public function apiWidgetsAction($widgetIds, $viewReferenceId) |
||
85 | |||
86 | /** |
||
87 | * New Widget. |
||
88 | * |
||
89 | * @param Request $request |
||
90 | * @param string $type The type of the widget we edit |
||
91 | * @param int $viewReference The view reference where attach the widget |
||
92 | * @param string $slot The slot where attach the widget |
||
93 | * @param string $quantum The quantum letter used to avoid same form name |
||
94 | * |
||
95 | * @throws Exception |
||
96 | * |
||
97 | * @return JsonResponse |
||
98 | * @Route("/victoire-dcms/widget/new/{type}/{viewReference}/{slot}/{quantum}", name="victoire_core_widget_new", defaults={"slot":null, "quantum":"a"}, options={"expose"=true}) |
||
99 | */ |
||
100 | public function newAction(Request $request, $type, $viewReference, $slot = null, $quantum = 'a') |
||
133 | |||
134 | /** |
||
135 | * Create a widget. |
||
136 | * This action needs 2 routes to handle the presence or not of "businessEntityId" and 'parentWidgetMap' |
||
137 | * that are both integers but "businessEntityId" present only in !static mode. |
||
138 | * |
||
139 | * @param string $type The type of the widget we edit |
||
140 | * @param int $viewReference The view reference where attach the widget |
||
141 | * @param string $slot The slot where attach the widget |
||
142 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
143 | * |
||
144 | * @return JsonResponse |
||
145 | * @Route("/victoire-dcms/widget/create/static/{type}/{viewReference}/{slot}/{quantum}/{position}/{parentWidgetMap}", name="victoire_core_widget_create_static", defaults={"mode":"static", "slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json", "quantum":"a"}) |
||
146 | * @Route("/victoire-dcms/widget/create/{mode}/{type}/{viewReference}/{slot}/{quantum}/{businessEntityId}/{position}/{parentWidgetMap}", name="victoire_core_widget_create", defaults={"slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json", "quantum":"a"}) |
||
147 | * @Template() |
||
148 | */ |
||
149 | public function createAction($mode, $type, $viewReference, $slot = null, $position = null, $parentWidgetMap = null, $businessEntityId = null, $quantum = null) |
||
187 | |||
188 | /** |
||
189 | * Edit a widget. |
||
190 | * |
||
191 | * @param Widget $widget The widget to edit |
||
192 | * @param int $viewReference The current view |
||
193 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
194 | * |
||
195 | * @return JsonResponse |
||
196 | * |
||
197 | * @Route("/victoire-dcms/widget/edit/{id}/{viewReference}/{mode}/{businessEntityId}", name="victoire_core_widget_edit", options={"expose"=true}, defaults={"quantum":"a", "mode": "static"}) |
||
198 | * @Route("/victoire-dcms/widget/update/{id}/{viewReference}/{mode}/{quantum}/{businessEntityId}", name="victoire_core_widget_update", defaults={"businessEntityId": null, "mode": "static"}) |
||
199 | * @Template() |
||
200 | */ |
||
201 | public function editAction(Widget $widget, $viewReference, $mode = Widget::MODE_STATIC, $quantum = null, $businessEntityId = null) |
||
232 | |||
233 | /** |
||
234 | * @TODO Simplify Action when reorganize with editAction |
||
235 | * |
||
236 | * Stylize a widget. |
||
237 | * |
||
238 | * @param Widget $widget The widget to stylize |
||
239 | * @param int $viewReference The current view |
||
240 | * |
||
241 | * @return JsonResponse |
||
242 | * |
||
243 | * @Route("/victoire-dcms/widget/stylize/{id}/{viewReference}", name="victoire_core_widget_stylize", options={"expose"=true}) |
||
244 | * @Template() |
||
245 | */ |
||
246 | public function stylizeAction(Request $request, Widget $widget, $viewReference) |
||
349 | |||
350 | /** |
||
351 | * Delete a Widget. |
||
352 | * |
||
353 | * @param Widget $widget The widget to delete |
||
354 | * @param int $viewReference The current view |
||
355 | * |
||
356 | * @return JsonResponse response |
||
357 | * @Route("/victoire-dcms/widget/delete/{id}/{viewReference}", name="victoire_core_widget_delete", defaults={"_format": "json"}) |
||
358 | * @Template() |
||
359 | */ |
||
360 | View Code Duplication | public function deleteAction(Widget $widget, $viewReference) |
|
377 | |||
378 | /** |
||
379 | * Delete a Widget quantum. |
||
380 | * |
||
381 | * @param Widget $widget The widget to delete |
||
382 | * @param int $viewReference The current view |
||
383 | * |
||
384 | * @return JsonResponse response |
||
385 | * @Route("/victoire-dcms/widget/delete/quantum/{id}/{viewReference}", name="victoire_core_widget_delete_bulk", defaults={"_format": "json"}) |
||
386 | * @Template() |
||
387 | */ |
||
388 | View Code Duplication | public function deleteBulkAction(Widget $widget, $viewReference) |
|
407 | |||
408 | /** |
||
409 | * Unlink a Widget by id |
||
410 | * -> used to unlink an invalid widget after a bad widget unplug. |
||
411 | * |
||
412 | * @param int $id The widgetId to unlink |
||
413 | * @param int $viewReference The current viewReference |
||
414 | * |
||
415 | * @return JsonResponse response |
||
416 | * @Route("/victoire-dcms/widget/unlink/{id}/{viewReference}", name="victoire_core_widget_unlink", defaults={"_format": "json"}, options={"expose"=true}) |
||
417 | * @Template() |
||
418 | */ |
||
419 | public function unlinkAction($id, $viewReference) |
||
447 | |||
448 | /** |
||
449 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
450 | * |
||
451 | * @param int $viewReference The current viewReference |
||
452 | * |
||
453 | * @return JsonResponse |
||
454 | * @Route("/victoire-dcms/widget/updatePosition/{viewReference}", name="victoire_core_widget_update_position", options={"expose"=true}) |
||
455 | */ |
||
456 | public function updatePositionAction(Request $request, $viewReference) |
||
483 | |||
484 | /** |
||
485 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
486 | * |
||
487 | * @param int $viewReference The current viewReference |
||
488 | * |
||
489 | * @return JsonResponse |
||
490 | * @Route("/victoire-dcms/widget/get-available-positions/{viewReference}", name="victoire_core_widget_get_available_positions", options={"expose"=true}) |
||
491 | */ |
||
492 | public function getAvailablePositionsAction(Request $request, $viewReference) |
||
501 | |||
502 | /** |
||
503 | * Get the json response by the exception and the current user. |
||
504 | * |
||
505 | * @param Exception $ex |
||
506 | * |
||
507 | * @return JsonResponse |
||
508 | */ |
||
509 | protected function getJsonReponseFromException(Exception $ex) |
||
541 | |||
542 | /** |
||
543 | * @param int $referenceId |
||
544 | */ |
||
545 | protected function getViewByReferenceId($referenceId) |
||
549 | } |
||
550 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.