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 AbstractRouteController 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 AbstractRouteController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class AbstractRouteController extends AbstractController |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * This is the default action handling the main admin area called without defining arguments. |
||
| 35 | * @Cache(expires="+7 days", public=true) |
||
| 36 | * |
||
| 37 | * @param Request $request Current request instance |
||
| 38 | * |
||
| 39 | * @return Response Output |
||
| 40 | * |
||
| 41 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 42 | */ |
||
| 43 | public function adminIndexAction(Request $request) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This is the default action handling the main area called without defining arguments. |
||
| 50 | * @Cache(expires="+7 days", public=true) |
||
| 51 | * |
||
| 52 | * @param Request $request Current request instance |
||
| 53 | * |
||
| 54 | * @return Response Output |
||
| 55 | * |
||
| 56 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 57 | */ |
||
| 58 | public function indexAction(Request $request) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * This method includes the common implementation code for adminIndex() and index(). |
||
| 65 | */ |
||
| 66 | protected function indexInternal(Request $request, $isAdmin = false) |
||
| 85 | /** |
||
| 86 | * This action provides an item list overview in the admin area. |
||
| 87 | * @Cache(expires="+2 hours", public=false) |
||
| 88 | * |
||
| 89 | * @param Request $request Current request instance |
||
| 90 | * @param string $sort Sorting field |
||
| 91 | * @param string $sortdir Sorting direction |
||
| 92 | * @param int $pos Current pager position |
||
| 93 | * @param int $num Amount of entries to display |
||
| 94 | * |
||
| 95 | * @return Response Output |
||
| 96 | * |
||
| 97 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 98 | */ |
||
| 99 | public function adminViewAction(Request $request, $sort, $sortdir, $pos, $num) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * This action provides an item list overview. |
||
| 106 | * @Cache(expires="+2 hours", public=false) |
||
| 107 | * |
||
| 108 | * @param Request $request Current request instance |
||
| 109 | * @param string $sort Sorting field |
||
| 110 | * @param string $sortdir Sorting direction |
||
| 111 | * @param int $pos Current pager position |
||
| 112 | * @param int $num Amount of entries to display |
||
| 113 | * |
||
| 114 | * @return Response Output |
||
| 115 | * |
||
| 116 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 117 | */ |
||
| 118 | public function viewAction(Request $request, $sort, $sortdir, $pos, $num) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * This method includes the common implementation code for adminView() and view(). |
||
| 125 | */ |
||
| 126 | protected function viewInternal(Request $request, $sort, $sortdir, $pos, $num, $isAdmin = false) |
||
| 180 | /** |
||
| 181 | * This action provides a item detail view in the admin area. |
||
| 182 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 183 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 184 | * |
||
| 185 | * @param Request $request Current request instance |
||
| 186 | * @param RouteEntity $route Treated route instance |
||
| 187 | * |
||
| 188 | * @return Response Output |
||
| 189 | * |
||
| 190 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 191 | * @throws NotFoundHttpException Thrown by param converter if item to be displayed isn't found |
||
| 192 | */ |
||
| 193 | public function adminDisplayAction(Request $request, RouteEntity $route) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * This action provides a item detail view. |
||
| 200 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 201 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 202 | * |
||
| 203 | * @param Request $request Current request instance |
||
| 204 | * @param RouteEntity $route Treated route instance |
||
| 205 | * |
||
| 206 | * @return Response Output |
||
| 207 | * |
||
| 208 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 209 | * @throws NotFoundHttpException Thrown by param converter if item to be displayed isn't found |
||
| 210 | */ |
||
| 211 | public function displayAction(Request $request, RouteEntity $route) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * This method includes the common implementation code for adminDisplay() and display(). |
||
| 218 | */ |
||
| 219 | protected function displayInternal(Request $request, RouteEntity $route, $isAdmin = false) |
||
| 255 | /** |
||
| 256 | * This action provides a handling of edit requests in the admin area. |
||
| 257 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 258 | * |
||
| 259 | * @param Request $request Current request instance |
||
| 260 | * |
||
| 261 | * @return Response Output |
||
| 262 | * |
||
| 263 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 264 | * @throws NotFoundHttpException Thrown by form handler if item to be edited isn't found |
||
| 265 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 266 | */ |
||
| 267 | public function adminEditAction(Request $request) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * This action provides a handling of edit requests. |
||
| 274 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 275 | * |
||
| 276 | * @param Request $request Current request instance |
||
| 277 | * |
||
| 278 | * @return Response Output |
||
| 279 | * |
||
| 280 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 281 | * @throws NotFoundHttpException Thrown by form handler if item to be edited isn't found |
||
| 282 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 283 | */ |
||
| 284 | public function editAction(Request $request) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * This method includes the common implementation code for adminEdit() and edit(). |
||
| 291 | */ |
||
| 292 | protected function editInternal(Request $request, $isAdmin = false) |
||
| 321 | /** |
||
| 322 | * This action provides a handling of simple delete requests in the admin area. |
||
| 323 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 324 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 325 | * |
||
| 326 | * @param Request $request Current request instance |
||
| 327 | * @param RouteEntity $route Treated route instance |
||
| 328 | * |
||
| 329 | * @return Response Output |
||
| 330 | * |
||
| 331 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 332 | * @throws NotFoundHttpException Thrown by param converter if item to be deleted isn't found |
||
| 333 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 334 | */ |
||
| 335 | public function adminDeleteAction(Request $request, RouteEntity $route) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * This action provides a handling of simple delete requests. |
||
| 342 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 343 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 344 | * |
||
| 345 | * @param Request $request Current request instance |
||
| 346 | * @param RouteEntity $route Treated route instance |
||
| 347 | * |
||
| 348 | * @return Response Output |
||
| 349 | * |
||
| 350 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 351 | * @throws NotFoundHttpException Thrown by param converter if item to be deleted isn't found |
||
| 352 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 353 | */ |
||
| 354 | public function deleteAction(Request $request, RouteEntity $route) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * This method includes the common implementation code for adminDelete() and delete(). |
||
| 361 | */ |
||
| 362 | protected function deleteInternal(Request $request, RouteEntity $route, $isAdmin = false) |
||
| 442 | /** |
||
| 443 | * This is a custom action in the admin area. |
||
| 444 | * |
||
| 445 | * @param Request $request Current request instance |
||
| 446 | * |
||
| 447 | * @return Response Output |
||
| 448 | * |
||
| 449 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 450 | */ |
||
| 451 | public function adminReloadAction(Request $request) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * This is a custom action. |
||
| 458 | * |
||
| 459 | * @param Request $request Current request instance |
||
| 460 | * |
||
| 461 | * @return Response Output |
||
| 462 | * |
||
| 463 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 464 | */ |
||
| 465 | public function reloadAction(Request $request) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * This method includes the common implementation code for adminReload() and reload(). |
||
| 472 | */ |
||
| 473 | View Code Duplication | protected function reloadInternal(Request $request, $isAdmin = false) |
|
| 492 | /** |
||
| 493 | * This is a custom action in the admin area. |
||
| 494 | * |
||
| 495 | * @param Request $request Current request instance |
||
| 496 | * |
||
| 497 | * @return Response Output |
||
| 498 | * |
||
| 499 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 500 | */ |
||
| 501 | public function adminRenewAction(Request $request) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * This is a custom action. |
||
| 508 | * |
||
| 509 | * @param Request $request Current request instance |
||
| 510 | * |
||
| 511 | * @return Response Output |
||
| 512 | * |
||
| 513 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 514 | */ |
||
| 515 | public function renewAction(Request $request) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * This method includes the common implementation code for adminRenew() and renew(). |
||
| 522 | */ |
||
| 523 | View Code Duplication | protected function renewInternal(Request $request, $isAdmin = false) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Process status changes for multiple items. |
||
| 545 | * |
||
| 546 | * This function processes the items selected in the admin view page. |
||
| 547 | * Multiple items may have their state changed or be deleted. |
||
| 548 | * |
||
| 549 | * @param Request $request Current request instance |
||
| 550 | * |
||
| 551 | * @return bool true on sucess, false on failure |
||
| 552 | * |
||
| 553 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 554 | */ |
||
| 555 | public function adminHandleSelectedEntriesAction(Request $request) |
||
| 559 | /** |
||
| 560 | * Process status changes for multiple items. |
||
| 561 | * |
||
| 562 | * This function processes the items selected in the admin view page. |
||
| 563 | * Multiple items may have their state changed or be deleted. |
||
| 564 | * |
||
| 565 | * @param Request $request Current request instance |
||
| 566 | * |
||
| 567 | * @return bool true on sucess, false on failure |
||
| 568 | * |
||
| 569 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 570 | */ |
||
| 571 | public function handleSelectedEntriesAction(Request $request) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * This method includes the common implementation code for adminHandleSelectedEntriesAction() and handleSelectedEntriesAction(). |
||
| 578 | */ |
||
| 579 | protected function handleSelectedEntriesActionInternal(Request $request, $isAdmin = false) |
||
| 638 | } |
||
| 639 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.