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 |
||
| 21 | class WidgetController extends Controller |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Show a widget. |
||
| 25 | * |
||
| 26 | * @param Request $request |
||
| 27 | * @param Widget $widget |
||
| 28 | * @param int $viewReferenceId |
||
| 29 | * |
||
| 30 | * @Route("/victoire-dcms-public/widget/show/{id}/{viewReferenceId}", name="victoire_core_widget_show", options={"expose"=true}) |
||
| 31 | * @Template() |
||
| 32 | * @ParamConverter("id", class="VictoireWidgetBundle:Widget") |
||
| 33 | * |
||
| 34 | * @throws Exception |
||
| 35 | * |
||
| 36 | * @return Response |
||
| 37 | */ |
||
| 38 | public function showAction(Request $request, Widget $widget, $viewReferenceId) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * API widgets function. |
||
| 59 | * |
||
| 60 | * @param string $widgetIds the widget ids to fetch in json |
||
| 61 | * @param int $viewReferenceId |
||
| 62 | * |
||
| 63 | * @Route("/victoire-dcms-public/api/widgets/{widgetIds}/{viewReferenceId}", name="victoire_core_widget_apiWidgets", options={"expose"=true}) |
||
| 64 | * |
||
| 65 | * @return JsonResponse |
||
| 66 | */ |
||
| 67 | public function apiWidgetsAction($widgetIds, $viewReferenceId) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * New Widget. |
||
| 83 | * |
||
| 84 | * @param string $type The type of the widget we edit |
||
| 85 | * @param int $viewReference The view reference where attach the widget |
||
| 86 | * @param string $slot The slot where attach the widget |
||
| 87 | * |
||
| 88 | * @return JsonResponse |
||
| 89 | * |
||
| 90 | * @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}) |
||
| 91 | * @Template() |
||
| 92 | */ |
||
| 93 | public function newAction($type, $viewReference, $slot = null, $position = null, $parentWidgetMap = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Create a widget. |
||
| 117 | * This action needs 2 routes to handle the presence or not of "businessEntityId" and 'parentWidgetMap' |
||
| 118 | * that are both integers but "businessEntityId" present only in !static mode |
||
| 119 | * |
||
| 120 | * @param string $type The type of the widget we edit |
||
| 121 | * @param int $viewReference The view reference where attach the widget |
||
| 122 | * @param string $slot The slot where attach the widget |
||
| 123 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
| 124 | * |
||
| 125 | * @return JsonResponse |
||
| 126 | * @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"}) |
||
| 127 | * @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"}) |
||
| 128 | * @Template() |
||
| 129 | */ |
||
| 130 | public function createAction($mode, $type, $viewReference, $slot = null, $position = null, $parentWidgetMap = null, $businessEntityId = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Edit a widget. |
||
| 170 | * |
||
| 171 | * @param Widget $widget The widget to edit |
||
| 172 | * @param int $viewReference The current view |
||
| 173 | * @param string $businessEntityId The BusinessEntity::id (can be null if the submitted form is in static mode) |
||
| 174 | * |
||
| 175 | * @return JsonResponse |
||
| 176 | * |
||
| 177 | * @Route("/victoire-dcms/widget/edit/{id}/{viewReference}/{mode}/{businessEntityId}", name="victoire_core_widget_edit", options={"expose"=true}) |
||
| 178 | * @Route("/victoire-dcms/widget/update/{id}/{viewReference}/{mode}/{businessEntityId}", name="victoire_core_widget_update", defaults={"businessEntityId": null}) |
||
| 179 | * @Template() |
||
| 180 | */ |
||
| 181 | public function editAction(Widget $widget, $viewReference, $mode = Widget::MODE_STATIC, $businessEntityId = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Stylize a widget. |
||
| 214 | * |
||
| 215 | * @param Widget $widget The widget to stylize |
||
| 216 | * @param int $viewReference The current view |
||
| 217 | * |
||
| 218 | * @return JsonResponse |
||
| 219 | * |
||
| 220 | * @Route("/victoire-dcms/widget/stylize/{id}/{viewReference}", name="victoire_core_widget_stylize", options={"expose"=true}) |
||
| 221 | * @Template() |
||
| 222 | */ |
||
| 223 | public function stylizeAction(Request $request, Widget $widget, $viewReference) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Delete a Widget. |
||
| 286 | * |
||
| 287 | * @param Widget $widget The widget to delete |
||
| 288 | * @param int $viewReference The current view |
||
| 289 | * |
||
| 290 | * @return JsonResponse response |
||
| 291 | * @Route("/victoire-dcms/widget/delete/{id}/{viewReference}", name="victoire_core_widget_delete", defaults={"_format": "json"}) |
||
| 292 | * @Template() |
||
| 293 | */ |
||
| 294 | public function deleteAction(Widget $widget, $viewReference) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Unlink a Widget by id |
||
| 314 | * -> used to unlink an invalid widget after a bad widget unplug. |
||
| 315 | * |
||
| 316 | * @param int $id The widgetId to unlink |
||
| 317 | * @param int $viewReference The current viewReference |
||
| 318 | * |
||
| 319 | * @return JsonResponse response |
||
| 320 | * @Route("/victoire-dcms/widget/unlink/{id}/{viewReference}", name="victoire_core_widget_unlink", defaults={"_format": "json"}, options={"expose"=true}) |
||
| 321 | * @Template() |
||
| 322 | */ |
||
| 323 | public function unlinkAction($id, $viewReference) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
| 354 | * |
||
| 355 | * @param int $viewReference The current viewReference |
||
| 356 | * |
||
| 357 | * @return JsonResponse |
||
| 358 | * @Route("/victoire-dcms/widget/updatePosition/{viewReference}", name="victoire_core_widget_update_position", options={"expose"=true}) |
||
| 359 | */ |
||
| 360 | public function updatePositionAction(Request $request, $viewReference) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Update widget positions accross the view. If moved widget is a Reference, ask to detach the view from template. |
||
| 390 | * |
||
| 391 | * @param int $viewReference The current viewReference |
||
| 392 | * |
||
| 393 | * @return JsonResponse |
||
| 394 | * @Route("/victoire-dcms/widget/get-available-positions/{viewReference}", name="victoire_core_widget_get_available_positions", options={"expose"=true}) |
||
| 395 | */ |
||
| 396 | public function getAvailablePositionsAction(Request $request, $viewReference) |
||
| 397 | { |
||
| 398 | $view = $this->getViewByReferenceId($viewReference); |
||
| 399 | |||
| 400 | $this->get('victoire_widget_map.builder')->build($view); |
||
| 401 | $availablePositions = $this->get('victoire_widget_map.builder')->getAvailablePosition($view); |
||
| 402 | |||
| 403 | return new JsonResponse($availablePositions); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the json response by the exception and the current user. |
||
| 408 | * |
||
| 409 | * @param Exception $ex |
||
| 410 | * |
||
| 411 | * @return JsonResponse |
||
| 412 | */ |
||
| 413 | protected function getJsonReponseFromException(Exception $ex) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param int $referenceId |
||
| 448 | */ |
||
| 449 | protected function getViewByReferenceId($referenceId) |
||
| 453 | } |
||
| 454 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.