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 | //the response is for the ajax.js from the AppVentus Ajax Bundle |
||
| 46 | try { |
||
| 47 | $view = $this->get('victoire_page.page_helper')->findPageByParameters(['id' => $viewReferenceId]); |
||
| 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 string $type The type of the widget we edit |
||
| 90 | * @param int $viewReference The view reference where attach the widget |
||
| 91 | * @param string $slot The slot where attach the widget |
||
| 92 | * |
||
| 93 | * @return JsonResponse |
||
| 94 | * |
||
| 95 | * @Route("/victoire-dcms/widget/new-quantum-item/{type}/{viewReference}/{slot}/{position}/{parentWidgetMap}", name="victoire_core_widget_new_quantum_item", defaults={"slot":null, "position":null, "parentWidgetMap":null}, options={"expose"=true}) |
||
| 96 | * @Route("/victoire-dcms/widget/new/{type}/{viewReference}/{slot}/{position}/{parentWidgetMap}", name="victoire_core_widget_new", defaults={"slot":null, "position":null, "parentWidgetMap":null}, options={"expose"=true}) |
||
| 97 | */ |
||
| 98 | public function newAction($type, $viewReference, $slot = null, $position = null, $parentWidgetMap = null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Create a widget. |
||
| 130 | * This action needs 2 routes to handle the presence or not of "businessEntityId" and 'parentWidgetMap' |
||
| 131 | * that are both integers but "businessEntityId" present only in !static mode. |
||
| 132 | * |
||
| 133 | * @param string $type The type of the widget we edit |
||
| 134 | * @param int $viewReference The view reference where attach the widget |
||
| 135 | * @param string $slot The slot where attach the widget |
||
| 136 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
| 137 | * |
||
| 138 | * @return JsonResponse |
||
| 139 | * @Route("/victoire-dcms/widget/create/static/{type}/{viewReference}/{slot}/{position}/{parentWidgetMap}", name="victoire_core_widget_create_static", defaults={"mode":"static", "slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json"}) |
||
| 140 | * @Route("/victoire-dcms/widget/create/{mode}/{type}/{viewReference}/{slot}/{businessEntityId}/{position}/{parentWidgetMap}", name="victoire_core_widget_create", defaults={"slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json"}) |
||
| 141 | * @Template() |
||
| 142 | */ |
||
| 143 | public function createAction($mode, $type, $viewReference, $slot = null, $position = null, $parentWidgetMap = null, $businessEntityId = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Edit a widget. |
||
| 184 | * |
||
| 185 | * @param Widget $widget The widget to edit |
||
| 186 | * @param int $viewReference The current view |
||
| 187 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
| 188 | * |
||
| 189 | * @return JsonResponse |
||
| 190 | * |
||
| 191 | * @Route("/victoire-dcms/widget/edit/{id}/{viewReference}/{mode}/{businessEntityId}", name="victoire_core_widget_edit", options={"expose"=true}) |
||
| 192 | * @Route("/victoire-dcms/widget/update/{id}/{viewReference}/{mode}/{businessEntityId}", name="victoire_core_widget_update", defaults={"businessEntityId": null}) |
||
| 193 | * @Template() |
||
| 194 | */ |
||
| 195 | public function editAction(Widget $widget, $viewReference, $mode = Widget::MODE_STATIC, $businessEntityId = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Stylize a widget. |
||
| 228 | * |
||
| 229 | * @param Widget $widget The widget to stylize |
||
| 230 | * @param int $viewReference The current view |
||
| 231 | * |
||
| 232 | * @return JsonResponse |
||
| 233 | * |
||
| 234 | * @Route("/victoire-dcms/widget/stylize/{id}/{viewReference}", name="victoire_core_widget_stylize", options={"expose"=true}) |
||
| 235 | * @Template() |
||
| 236 | */ |
||
| 237 | public function stylizeAction(Request $request, Widget $widget, $viewReference) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Delete a Widget. |
||
| 346 | * |
||
| 347 | * @param Widget $widget The widget to delete |
||
| 348 | * @param int $viewReference The current view |
||
| 349 | * |
||
| 350 | * @return JsonResponse response |
||
| 351 | * @Route("/victoire-dcms/widget/delete/{id}/{viewReference}", name="victoire_core_widget_delete", defaults={"_format": "json"}) |
||
| 352 | * @Template() |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function deleteAction(Widget $widget, $viewReference) |
|
| 371 | /** |
||
| 372 | * Delete a Widget quantum. |
||
| 373 | * |
||
| 374 | * @param Widget $widget The widget to delete |
||
| 375 | * @param int $viewReference The current view |
||
| 376 | * |
||
| 377 | * @return JsonResponse response |
||
| 378 | * @Route("/victoire-dcms/widget/delete/quantum/{id}/{viewReference}", name="victoire_core_widget_delete_bulk", defaults={"_format": "json"}) |
||
| 379 | * @Template() |
||
| 380 | */ |
||
| 381 | View Code Duplication | public function deleteBulkAction(Widget $widget, $viewReference) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Unlink a Widget by id |
||
| 403 | * -> used to unlink an invalid widget after a bad widget unplug. |
||
| 404 | * |
||
| 405 | * @param int $id The widgetId to unlink |
||
| 406 | * @param int $viewReference The current viewReference |
||
| 407 | * |
||
| 408 | * @return JsonResponse response |
||
| 409 | * @Route("/victoire-dcms/widget/unlink/{id}/{viewReference}", name="victoire_core_widget_unlink", defaults={"_format": "json"}, options={"expose"=true}) |
||
| 410 | * @Template() |
||
| 411 | */ |
||
| 412 | public function unlinkAction($id, $viewReference) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
| 444 | * |
||
| 445 | * @param int $viewReference The current viewReference |
||
| 446 | * |
||
| 447 | * @return JsonResponse |
||
| 448 | * @Route("/victoire-dcms/widget/updatePosition/{viewReference}", name="victoire_core_widget_update_position", options={"expose"=true}) |
||
| 449 | */ |
||
| 450 | public function updatePositionAction(Request $request, $viewReference) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
| 480 | * |
||
| 481 | * @param int $viewReference The current viewReference |
||
| 482 | * |
||
| 483 | * @return JsonResponse |
||
| 484 | * @Route("/victoire-dcms/widget/get-available-positions/{viewReference}", name="victoire_core_widget_get_available_positions", options={"expose"=true}) |
||
| 485 | */ |
||
| 486 | public function getAvailablePositionsAction(Request $request, $viewReference) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the json response by the exception and the current user. |
||
| 498 | * |
||
| 499 | * @param Exception $ex |
||
| 500 | * |
||
| 501 | * @return JsonResponse |
||
| 502 | */ |
||
| 503 | protected function getJsonReponseFromException(Exception $ex) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param int $referenceId |
||
| 538 | */ |
||
| 539 | protected function getViewByReferenceId($referenceId) |
||
| 543 | } |
||
| 544 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.