Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Controller { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Postprocesses the entity after modification by handling the uploaded |
||
| 46 | * files and setting the flash. |
||
| 47 | * |
||
| 48 | * @param Application $app |
||
| 49 | * the current application |
||
| 50 | * @param AbstractData $crudData |
||
| 51 | * the data instance of the entity |
||
| 52 | * @param Entity $instance |
||
| 53 | * the entity |
||
| 54 | * @param string $entity |
||
| 55 | * the name of the entity |
||
| 56 | * @param string $mode |
||
| 57 | * whether to 'edit' or to 'create' the entity |
||
| 58 | * |
||
| 59 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 60 | * the HTTP response of this modification |
||
| 61 | */ |
||
| 62 | 4 | protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the flashes of a failed entity modification. |
||
| 80 | * |
||
| 81 | * @param Application $app |
||
| 82 | * the current application |
||
| 83 | * @param boolean $optimisticLocking |
||
| 84 | * whether the optimistic locking failed |
||
| 85 | * @param string $mode |
||
| 86 | * the modification mode, either 'create' or 'edit' |
||
| 87 | */ |
||
| 88 | 2 | protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
| 98 | * response. |
||
| 99 | * |
||
| 100 | * @param Application $app |
||
| 101 | * the current application |
||
| 102 | * @param AbstractData $crudData |
||
| 103 | * the data instance of the entity |
||
| 104 | * @param Entity $instance |
||
| 105 | * the entity |
||
| 106 | * @param string $entity |
||
| 107 | * the name of the entity |
||
| 108 | * @param boolean $edit |
||
| 109 | * whether to edit (true) or to create (false) the entity |
||
| 110 | * |
||
| 111 | * @return Response |
||
| 112 | * the HTTP response of this modification |
||
| 113 | */ |
||
| 114 | 5 | protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Gets the parameters for the redirection after deleting an entity. |
||
| 151 | * |
||
| 152 | * @param Request $request |
||
| 153 | * the current request |
||
| 154 | * @param string $entity |
||
| 155 | * the entity name |
||
| 156 | * @param string $redirectPage |
||
| 157 | * reference, where the page to redirect to will be stored |
||
| 158 | * |
||
| 159 | * @return array<string,string> |
||
| 160 | * the parameters of the redirection, entity and id |
||
| 161 | */ |
||
| 162 | 1 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Builds up the parameters of the list page filters. |
||
| 180 | * |
||
| 181 | * @param Request $request |
||
| 182 | * the current application |
||
| 183 | * @param EntityDefinition $definition |
||
| 184 | * the current entity definition |
||
| 185 | * @param array &$filter |
||
| 186 | * will hold a map of fields to request parameters for the filters |
||
| 187 | * @param boolean $filterActive |
||
| 188 | * reference, will be true if at least one filter is active |
||
| 189 | * @param array $filterToUse |
||
| 190 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
| 191 | * @param array $filterOperators |
||
| 192 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
| 193 | */ |
||
| 194 | 4 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Generates the not found page. |
||
| 222 | * |
||
| 223 | * @param Application $app |
||
| 224 | * the Silex application |
||
| 225 | * @param string $error |
||
| 226 | * the cause of the not found error |
||
| 227 | * |
||
| 228 | * @return Response |
||
| 229 | * the rendered not found page with the status code 404 |
||
| 230 | */ |
||
| 231 | 9 | public function getNotFoundPage(Application $app, $error) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Transfers the locale from the translator to CRUDlex and |
||
| 243 | * |
||
| 244 | * @param Request $request |
||
| 245 | * @param Application $app |
||
| 246 | * @return Response |
||
|
|
|||
| 247 | */ |
||
| 248 | 9 | public function setLocaleAndCheckEntity(Request $request, Application $app) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * The controller for the "create" action. |
||
| 260 | * |
||
| 261 | * @param Application $app |
||
| 262 | * the Silex application |
||
| 263 | * @param string $entity |
||
| 264 | * the current entity |
||
| 265 | * |
||
| 266 | * @return Response |
||
| 267 | * the HTTP response of this action |
||
| 268 | */ |
||
| 269 | 4 | public function create(Application $app, $entity) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * The controller for the "show list" action. |
||
| 280 | * |
||
| 281 | * @param Request $request |
||
| 282 | * the current request |
||
| 283 | * @param Application $app |
||
| 284 | * the Silex application |
||
| 285 | * @param string $entity |
||
| 286 | * the current entity |
||
| 287 | * |
||
| 288 | * @return Response |
||
| 289 | * the HTTP response of this action or 404 on invalid input |
||
| 290 | */ |
||
| 291 | 4 | public function showList(Request $request, Application $app, $entity) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * The controller for the "show" action. |
||
| 340 | * |
||
| 341 | * @param Application $app |
||
| 342 | * the Silex application |
||
| 343 | * @param string $entity |
||
| 344 | * the current entity |
||
| 345 | * @param string $id |
||
| 346 | * the instance id to show |
||
| 347 | * |
||
| 348 | * @return Response |
||
| 349 | * the HTTP response of this action or 404 on invalid input |
||
| 350 | */ |
||
| 351 | 6 | public function show(Application $app, $entity, $id) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * The controller for the "edit" action. |
||
| 389 | * |
||
| 390 | * @param Application $app |
||
| 391 | * the Silex application |
||
| 392 | * @param string $entity |
||
| 393 | * the current entity |
||
| 394 | * @param string $id |
||
| 395 | * the instance id to edit |
||
| 396 | * |
||
| 397 | * @return Response |
||
| 398 | * the HTTP response of this action or 404 on invalid input |
||
| 399 | */ |
||
| 400 | 1 | public function edit(Application $app, $entity, $id) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * The controller for the "delete" action. |
||
| 413 | * |
||
| 414 | * @param Application $app |
||
| 415 | * the Silex application |
||
| 416 | * @param string $entity |
||
| 417 | * the current entity |
||
| 418 | * @param string $id |
||
| 419 | * the instance id to delete |
||
| 420 | * |
||
| 421 | * @return Response |
||
| 422 | * redirects to the entity list page or 404 on invalid input |
||
| 423 | */ |
||
| 424 | 1 | public function delete(Application $app, $entity, $id) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * The controller for the "render file" action. |
||
| 457 | * |
||
| 458 | * @param Application $app |
||
| 459 | * the Silex application |
||
| 460 | * @param string $entity |
||
| 461 | * the current entity |
||
| 462 | * @param string $id |
||
| 463 | * the instance id |
||
| 464 | * @param string $field |
||
| 465 | * the field of the file to render of the instance |
||
| 466 | * |
||
| 467 | * @return Response |
||
| 468 | * the rendered file |
||
| 469 | */ |
||
| 470 | 1 | public function renderFile(Application $app, $entity, $id, $field) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * The controller for the "delete file" action. |
||
| 484 | * |
||
| 485 | * @param Application $app |
||
| 486 | * the Silex application |
||
| 487 | * @param string $entity |
||
| 488 | * the current entity |
||
| 489 | * @param string $id |
||
| 490 | * the instance id |
||
| 491 | * @param string $field |
||
| 492 | * the field of the file to delete of the instance |
||
| 493 | * |
||
| 494 | * @return Response |
||
| 495 | * redirects to the instance details page or 404 on invalid input |
||
| 496 | */ |
||
| 497 | 1 | public function deleteFile(Application $app, $entity, $id, $field) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * The controller for serving static files. |
||
| 517 | * |
||
| 518 | * @param Request $request |
||
| 519 | * the current request |
||
| 520 | * @param Application $app |
||
| 521 | * the Silex application |
||
| 522 | * |
||
| 523 | * @return Response |
||
| 524 | * redirects to the instance details page or 404 on invalid input |
||
| 525 | */ |
||
| 526 | 1 | public function staticFile(Request $request, Application $app) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * The controller for setting the locale. |
||
| 552 | * |
||
| 553 | * @param Request $request |
||
| 554 | * the current request |
||
| 555 | * @param Application $app |
||
| 556 | * the Silex application |
||
| 557 | * @param string $locale |
||
| 558 | * the new locale |
||
| 559 | * |
||
| 560 | * @return Response |
||
| 561 | * redirects to the instance details page or 404 on invalid input |
||
| 562 | */ |
||
| 563 | 1 | public function setLocale(Request $request, Application $app, $locale) |
|
| 577 | } |
||
| 578 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.