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) |
||
| 181 | /** |
||
| 182 | * This action provides a item detail view in the admin area. |
||
| 183 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 184 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 185 | * |
||
| 186 | * @param Request $request Current request instance |
||
| 187 | * @param RouteEntity $route Treated route instance |
||
| 188 | * |
||
| 189 | * @return Response Output |
||
| 190 | * |
||
| 191 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 192 | * @throws NotFoundHttpException Thrown by param converter if item to be displayed isn't found |
||
| 193 | */ |
||
| 194 | public function adminDisplayAction(Request $request, RouteEntity $route) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * This action provides a item detail view. |
||
| 201 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 202 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 203 | * |
||
| 204 | * @param Request $request Current request instance |
||
| 205 | * @param RouteEntity $route Treated route instance |
||
| 206 | * |
||
| 207 | * @return Response Output |
||
| 208 | * |
||
| 209 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 210 | * @throws NotFoundHttpException Thrown by param converter if item to be displayed isn't found |
||
| 211 | */ |
||
| 212 | public function displayAction(Request $request, RouteEntity $route) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * This method includes the common implementation code for adminDisplay() and display(). |
||
| 219 | */ |
||
| 220 | protected function displayInternal(Request $request, RouteEntity $route, $isAdmin = false) |
||
| 256 | /** |
||
| 257 | * This action provides a handling of edit requests in the admin area. |
||
| 258 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 259 | * |
||
| 260 | * @param Request $request Current request instance |
||
| 261 | * |
||
| 262 | * @return Response Output |
||
| 263 | * |
||
| 264 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 265 | * @throws NotFoundHttpException Thrown by form handler if item to be edited isn't found |
||
| 266 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 267 | */ |
||
| 268 | public function adminEditAction(Request $request) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * This action provides a handling of edit requests. |
||
| 275 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 276 | * |
||
| 277 | * @param Request $request Current request instance |
||
| 278 | * |
||
| 279 | * @return Response Output |
||
| 280 | * |
||
| 281 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 282 | * @throws NotFoundHttpException Thrown by form handler if item to be edited isn't found |
||
| 283 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 284 | */ |
||
| 285 | public function editAction(Request $request) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * This method includes the common implementation code for adminEdit() and edit(). |
||
| 292 | */ |
||
| 293 | protected function editInternal(Request $request, $isAdmin = false) |
||
| 322 | /** |
||
| 323 | * This action provides a handling of simple delete requests in the admin area. |
||
| 324 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 325 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 326 | * |
||
| 327 | * @param Request $request Current request instance |
||
| 328 | * @param RouteEntity $route Treated route instance |
||
| 329 | * |
||
| 330 | * @return Response Output |
||
| 331 | * |
||
| 332 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 333 | * @throws NotFoundHttpException Thrown by param converter if item to be deleted isn't found |
||
| 334 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 335 | */ |
||
| 336 | public function adminDeleteAction(Request $request, RouteEntity $route) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * This action provides a handling of simple delete requests. |
||
| 343 | * @ParamConverter("route", class="ZikulaRoutesModule:RouteEntity", options={"id" = "id", "repository_method" = "selectById"}) |
||
| 344 | * @Cache(lastModified="route.getUpdatedDate()", ETag="'Route' ~ route.getid() ~ route.getUpdatedDate().format('U')") |
||
| 345 | * |
||
| 346 | * @param Request $request Current request instance |
||
| 347 | * @param RouteEntity $route Treated route instance |
||
| 348 | * |
||
| 349 | * @return Response Output |
||
| 350 | * |
||
| 351 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 352 | * @throws NotFoundHttpException Thrown by param converter if item to be deleted isn't found |
||
| 353 | * @throws RuntimeException Thrown if another critical error occurs (e.g. workflow actions not available) |
||
| 354 | */ |
||
| 355 | public function deleteAction(Request $request, RouteEntity $route) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * This method includes the common implementation code for adminDelete() and delete(). |
||
| 362 | */ |
||
| 363 | protected function deleteInternal(Request $request, RouteEntity $route, $isAdmin = false) |
||
| 438 | /** |
||
| 439 | * This is a custom action in the admin area. |
||
| 440 | * |
||
| 441 | * @param Request $request Current request instance |
||
| 442 | * |
||
| 443 | * @return Response Output |
||
| 444 | * |
||
| 445 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 446 | */ |
||
| 447 | public function adminReloadAction(Request $request) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * This is a custom action. |
||
| 454 | * |
||
| 455 | * @param Request $request Current request instance |
||
| 456 | * |
||
| 457 | * @return Response Output |
||
| 458 | * |
||
| 459 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 460 | */ |
||
| 461 | public function reloadAction(Request $request) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * This method includes the common implementation code for adminReload() and reload(). |
||
| 468 | */ |
||
| 469 | View Code Duplication | protected function reloadInternal(Request $request, $isAdmin = false) |
|
| 488 | /** |
||
| 489 | * This is a custom action in the admin area. |
||
| 490 | * |
||
| 491 | * @param Request $request Current request instance |
||
| 492 | * |
||
| 493 | * @return Response Output |
||
| 494 | * |
||
| 495 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 496 | */ |
||
| 497 | public function adminRenewAction(Request $request) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * This is a custom action. |
||
| 504 | * |
||
| 505 | * @param Request $request Current request instance |
||
| 506 | * |
||
| 507 | * @return Response Output |
||
| 508 | * |
||
| 509 | * @throws AccessDeniedException Thrown if the user doesn't have required permissions |
||
| 510 | */ |
||
| 511 | public function renewAction(Request $request) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * This method includes the common implementation code for adminRenew() and renew(). |
||
| 518 | */ |
||
| 519 | View Code Duplication | protected function renewInternal(Request $request, $isAdmin = false) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Process status changes for multiple items. |
||
| 541 | * |
||
| 542 | * This function processes the items selected in the admin view page. |
||
| 543 | * Multiple items may have their state changed or be deleted. |
||
| 544 | * |
||
| 545 | * @param Request $request Current request instance |
||
| 546 | * |
||
| 547 | * @return bool true on sucess, false on failure |
||
| 548 | * |
||
| 549 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 550 | */ |
||
| 551 | public function adminHandleSelectedEntriesAction(Request $request) |
||
| 555 | /** |
||
| 556 | * Process status changes for multiple items. |
||
| 557 | * |
||
| 558 | * This function processes the items selected in the admin view page. |
||
| 559 | * Multiple items may have their state changed or be deleted. |
||
| 560 | * |
||
| 561 | * @param Request $request Current request instance |
||
| 562 | * |
||
| 563 | * @return bool true on sucess, false on failure |
||
| 564 | * |
||
| 565 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 566 | */ |
||
| 567 | public function handleSelectedEntriesAction(Request $request) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * This method includes the common implementation code for adminHandleSelectedEntriesAction() and handleSelectedEntriesAction(). |
||
| 574 | */ |
||
| 575 | protected function handleSelectedEntriesActionInternal(Request $request, $isAdmin = false) |
||
| 634 | } |
||
| 635 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.