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) |
||
|
|
|||
| 67 | { |
||
| 68 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 69 | |||
| 70 | // parameter specifying which type of objects we are treating |
||
| 71 | $objectType = 'route'; |
||
| 72 | $permLevel = $isAdmin ? ACCESS_ADMIN : ACCESS_OVERVIEW; |
||
| 73 | View Code Duplication | if (!$this->hasPermission($this->name . ':' . ucfirst($objectType) . ':', '::', $permLevel)) { |
|
| 74 | throw new AccessDeniedException(); |
||
| 75 | } |
||
| 76 | $templateParameters = [ |
||
| 77 | 'routeArea' => $isAdmin ? 'admin' : '' |
||
| 78 | ]; |
||
| 79 | |||
| 80 | return $this->redirectToRoute('zikularoutesmodule_route_' . $templateParameters['routeArea'] . 'view'); |
||
| 81 | |||
| 82 | // return index template |
||
| 83 | return $this->render('@ZikulaRoutesModule/Route/index.html.twig', $templateParameters); |
||
| 84 | } |
||
| 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) |
||
| 127 | { |
||
| 128 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 129 | |||
| 130 | // parameter specifying which type of objects we are treating |
||
| 131 | $objectType = 'route'; |
||
| 132 | $permLevel = $isAdmin ? ACCESS_ADMIN : ACCESS_READ; |
||
| 133 | View Code Duplication | if (!$this->hasPermission($this->name . ':' . ucfirst($objectType) . ':', '::', $permLevel)) { |
|
| 134 | throw new AccessDeniedException(); |
||
| 135 | } |
||
| 136 | $templateParameters = [ |
||
| 137 | 'routeArea' => $isAdmin ? 'admin' : '' |
||
| 138 | ]; |
||
| 139 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 140 | $viewHelper = $this->get('zikula_routes_module.view_helper'); |
||
| 141 | |||
| 142 | // parameter for used sort order |
||
| 143 | $sortdir = strtolower($sortdir); |
||
| 144 | $request->query->set('sort', $sort); |
||
| 145 | $request->query->set('sortdir', $sortdir); |
||
| 146 | |||
| 147 | $sortableColumns = new SortableColumns($this->get('router'), 'zikularoutesmodule_route_' . ($isAdmin ? 'admin' : '') . 'view', 'sort', 'sortdir'); |
||
| 148 | |||
| 149 | $sortableColumns->addColumns([ |
||
| 150 | new Column('routeType'), |
||
| 151 | new Column('replacedRouteName'), |
||
| 152 | new Column('bundle'), |
||
| 153 | new Column('controller'), |
||
| 154 | new Column('action'), |
||
| 155 | new Column('path'), |
||
| 156 | new Column('host'), |
||
| 157 | new Column('schemes'), |
||
| 158 | new Column('methods'), |
||
| 159 | new Column('prependBundlePrefix'), |
||
| 160 | new Column('translatable'), |
||
| 161 | new Column('translationPrefix'), |
||
| 162 | new Column('condition'), |
||
| 163 | new Column('description'), |
||
| 164 | new Column('sort'), |
||
| 165 | new Column('group'), |
||
| 166 | new Column('createdBy'), |
||
| 167 | new Column('createdDate'), |
||
| 168 | new Column('updatedBy'), |
||
| 169 | new Column('updatedDate'), |
||
| 170 | ]); |
||
| 171 | |||
| 172 | $templateParameters = $controllerHelper->processViewActionParameters($objectType, $sortableColumns, $templateParameters); |
||
| 173 | |||
| 174 | foreach ($templateParameters['items'] as $k => $entity) { |
||
| 175 | $entity->initWorkflow(); |
||
| 176 | } |
||
| 177 | |||
| 178 | // fetch and return the appropriate template |
||
| 179 | return $viewHelper->processTemplate($objectType, 'view', $templateParameters); |
||
| 180 | } |
||
| 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 route 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 route 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) |
||
| 221 | { |
||
| 222 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 223 | |||
| 224 | // parameter specifying which type of objects we are treating |
||
| 225 | $objectType = 'route'; |
||
| 226 | $permLevel = $isAdmin ? ACCESS_ADMIN : ACCESS_READ; |
||
| 227 | View Code Duplication | if (!$this->hasPermission($this->name . ':' . ucfirst($objectType) . ':', '::', $permLevel)) { |
|
| 228 | throw new AccessDeniedException(); |
||
| 229 | } |
||
| 230 | // create identifier for permission check |
||
| 231 | $instanceId = $route->createCompositeIdentifier(); |
||
| 232 | if (!$this->hasPermission($this->name . ':' . ucfirst($objectType) . ':', $instanceId . '::', $permLevel)) { |
||
| 233 | throw new AccessDeniedException(); |
||
| 234 | } |
||
| 235 | |||
| 236 | $route->initWorkflow(); |
||
| 237 | $templateParameters = [ |
||
| 238 | 'routeArea' => $isAdmin ? 'admin' : '', |
||
| 239 | $objectType => $route |
||
| 240 | ]; |
||
| 241 | |||
| 242 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 243 | $templateParameters = $controllerHelper->processDisplayActionParameters($objectType, $templateParameters); |
||
| 244 | |||
| 245 | // fetch and return the appropriate template |
||
| 246 | $response = $this->get('zikula_routes_module.view_helper')->processTemplate($objectType, 'display', $templateParameters); |
||
| 247 | |||
| 248 | $format = $request->getRequestFormat(); |
||
| 249 | if ($format == 'ics') { |
||
| 250 | $fileName = $objectType . '_' . (property_exists($route, 'slug') ? $route['slug'] : $route->getTitleFromDisplayPattern()) . '.ics'; |
||
| 251 | $response->headers->set('Content-Disposition', 'attachment; filename=' . $fileName); |
||
| 252 | } |
||
| 253 | |||
| 254 | return $response; |
||
| 255 | } |
||
| 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 route 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 route 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) |
||
| 294 | { |
||
| 295 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 296 | |||
| 297 | // parameter specifying which type of objects we are treating |
||
| 298 | $objectType = 'route'; |
||
| 299 | $permLevel = $isAdmin ? ACCESS_ADMIN : ACCESS_EDIT; |
||
| 300 | View Code Duplication | if (!$this->hasPermission($this->name . ':' . ucfirst($objectType) . ':', '::', $permLevel)) { |
|
| 301 | throw new AccessDeniedException(); |
||
| 302 | } |
||
| 303 | $templateParameters = [ |
||
| 304 | 'routeArea' => $isAdmin ? 'admin' : '' |
||
| 305 | ]; |
||
| 306 | |||
| 307 | $controllerHelper = $this->get('zikula_routes_module.controller_helper'); |
||
| 308 | $templateParameters = $controllerHelper->processEditActionParameters($objectType, $templateParameters); |
||
| 309 | |||
| 310 | // delegate form processing to the form handler |
||
| 311 | $formHandler = $this->get('zikula_routes_module.form.handler.route'); |
||
| 312 | $result = $formHandler->processForm($templateParameters); |
||
| 313 | if ($result instanceof RedirectResponse) { |
||
| 314 | return $result; |
||
| 315 | } |
||
| 316 | |||
| 317 | $templateParameters = $formHandler->getTemplateParameters(); |
||
| 318 | |||
| 319 | // fetch and return the appropriate template |
||
| 320 | return $this->get('zikula_routes_module.view_helper')->processTemplate($objectType, 'edit', $templateParameters); |
||
| 321 | } |
||
| 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 route 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 route 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 | /** |
||
| 440 | * Process status changes for multiple items. |
||
| 441 | * |
||
| 442 | * This function processes the items selected in the admin view page. |
||
| 443 | * Multiple items may have their state changed or be deleted. |
||
| 444 | * |
||
| 445 | * @param Request $request Current request instance |
||
| 446 | * |
||
| 447 | * @return RedirectResponse |
||
| 448 | * |
||
| 449 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 450 | */ |
||
| 451 | public function adminHandleSelectedEntriesAction(Request $request) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Process status changes for multiple items. |
||
| 458 | * |
||
| 459 | * This function processes the items selected in the admin view page. |
||
| 460 | * Multiple items may have their state changed or be deleted. |
||
| 461 | * |
||
| 462 | * @param Request $request Current request instance |
||
| 463 | * |
||
| 464 | * @return RedirectResponse |
||
| 465 | * |
||
| 466 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 467 | */ |
||
| 468 | public function handleSelectedEntriesAction(Request $request) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * This method includes the common implementation code for adminHandleSelectedEntriesAction() and handleSelectedEntriesAction(). |
||
| 475 | * |
||
| 476 | * @param Request $request Current request instance |
||
| 477 | * @param Boolean $isAdmin Whether the admin area is used or not |
||
| 478 | */ |
||
| 479 | protected function handleSelectedEntriesActionInternal(Request $request, $isAdmin = false) |
||
| 538 | } |
||
| 539 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.