Complex classes like ControllerProvider 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 ControllerProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ControllerProvider implements ControllerProviderInterface { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Generates the not found page. |
||
| 45 | * |
||
| 46 | * @param Application $app |
||
| 47 | * the Silex application |
||
| 48 | * @param string $error |
||
| 49 | * the cause of the not found error |
||
| 50 | * |
||
| 51 | * @return Response |
||
| 52 | * the rendered not found page with the status code 404 |
||
| 53 | */ |
||
| 54 | protected function getNotFoundPage(Application $app, $error) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Postprocesses the entity after modification by handling the uploaded |
||
| 64 | * files and setting the flash. |
||
| 65 | * |
||
| 66 | * @param Application $app |
||
| 67 | * the current application |
||
| 68 | * @param AbstractData $crudData |
||
| 69 | * the data instance of the entity |
||
| 70 | * @param Entity $instance |
||
| 71 | * the entity |
||
| 72 | * @param string $entity |
||
| 73 | * the name of the entity |
||
| 74 | * @param string $mode |
||
| 75 | * whether to 'edit' or to 'create' the entity |
||
| 76 | * |
||
| 77 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 78 | * the HTTP response of this modification |
||
| 79 | */ |
||
| 80 | protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) { |
||
| 81 | $id = $instance->get('id'); |
||
| 82 | $request = $app['request_stack']->getCurrentRequest(); |
||
| 83 | $result = $mode == 'edit' ? $crudData->updateFiles($request, $instance, $entity) : $crudData->createFiles($request, $instance, $entity); |
||
| 84 | if (!$result) { |
||
| 85 | return null; |
||
| 86 | } |
||
| 87 | $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.'.$mode.'.success', [ |
||
| 88 | '%label%' => $crudData->getDefinition()->getLabel(), |
||
| 89 | '%id%' => $id |
||
| 90 | ])); |
||
| 91 | return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Sets the flashes of a failed entity modification. |
||
| 96 | * |
||
| 97 | * @param Application $app |
||
| 98 | * the current application |
||
| 99 | * @param boolean $optimisticLocking |
||
| 100 | * whether the optimistic locking failed |
||
| 101 | * @param string $mode |
||
| 102 | * the modification mode, either 'create' or 'edit' |
||
| 103 | */ |
||
| 104 | protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
| 113 | * response. |
||
| 114 | * |
||
| 115 | * @param Application $app |
||
| 116 | * the current application |
||
| 117 | * @param AbstractData $crudData |
||
| 118 | * the data instance of the entity |
||
| 119 | * @param Entity $instance |
||
| 120 | * the entity |
||
| 121 | * @param string $entity |
||
| 122 | * the name of the entity |
||
| 123 | * @param boolean $edit |
||
| 124 | * whether to edit (true) or to create (false) the entity |
||
| 125 | * |
||
| 126 | * @return Response |
||
| 127 | * the HTTP response of this modification |
||
| 128 | */ |
||
| 129 | protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the parameters for the redirection after deleting an entity. |
||
| 164 | * |
||
| 165 | * @param Request $request |
||
| 166 | * the current request |
||
| 167 | * @param string $entity |
||
| 168 | * the entity name |
||
| 169 | * @param string $redirectPage |
||
| 170 | * reference, where the page to redirect to will be stored |
||
| 171 | * |
||
| 172 | * @return array<string,string> |
||
| 173 | * the parameters of the redirection, entity and id |
||
| 174 | */ |
||
| 175 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Builds up the parameters of the list page filters. |
||
| 192 | * |
||
| 193 | * @param Request $request |
||
| 194 | * the current application |
||
| 195 | * @param EntityDefinition $definition |
||
| 196 | * the current entity definition |
||
| 197 | * @param array &$filter |
||
| 198 | * will hold a map of fields to request parameters for the filters |
||
| 199 | * @param boolean $filterActive |
||
| 200 | * reference, will be true if at least one filter is active |
||
| 201 | * @param array $filterToUse |
||
| 202 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
| 203 | * @param array $filterOperators |
||
| 204 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
| 205 | */ |
||
| 206 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) { |
||
| 207 | foreach ($definition->getFilter() as $filterField) { |
||
| 208 | $type = $definition->getType($filterField); |
||
| 209 | $filter[$filterField] = $request->get('crudFilter'.$filterField); |
||
| 210 | if ($filter[$filterField]) { |
||
| 211 | $filterActive = true; |
||
| 212 | if ($type === 'boolean') { |
||
| 213 | $filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
||
| 214 | $filterOperators[$filterField] = '='; |
||
| 215 | } else if ($type === 'many') { |
||
| 216 | $filter[$filterField] = array_map(function($value) { |
||
| 217 | return ['id' => $value]; |
||
| 218 | }, $filter[$filterField]); |
||
| 219 | $filterToUse[$filterField] = $filter[$filterField]; |
||
| 220 | } else { |
||
| 221 | $filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
||
| 222 | $filterOperators[$filterField] = 'LIKE'; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Setups the templates. |
||
| 230 | * |
||
| 231 | * @param Application $app |
||
| 232 | * the Application instance of the Silex application |
||
| 233 | */ |
||
| 234 | protected function setupTemplates(Application $app) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Setups the routes. |
||
| 246 | * |
||
| 247 | * @param Application $app |
||
| 248 | * the Application instance of the Silex application |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | * the created controller factory |
||
| 252 | */ |
||
| 253 | protected function setupRoutes(Application $app) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Setups i18n. |
||
| 297 | * |
||
| 298 | * @param Application $app |
||
| 299 | * the Application instance of the Silex application |
||
| 300 | */ |
||
| 301 | protected function setupI18n(Application $app) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Implements ControllerProviderInterface::connect() connecting this |
||
| 314 | * controller. |
||
| 315 | * |
||
| 316 | * @param Application $app |
||
| 317 | * the Application instance of the Silex application |
||
| 318 | * |
||
| 319 | * @return \SilexController\Collection |
||
| 320 | * this method is expected to return the used ControllerCollection instance |
||
| 321 | */ |
||
| 322 | public function connect(Application $app) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * The controller for the "create" action. |
||
| 331 | * |
||
| 332 | * @param Application $app |
||
| 333 | * the Silex application |
||
| 334 | * @param string $entity |
||
| 335 | * the current entity |
||
| 336 | * |
||
| 337 | * @return Response |
||
| 338 | * the HTTP response of this action |
||
| 339 | */ |
||
| 340 | public function create(Application $app, $entity) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * The controller for the "show list" action. |
||
| 348 | * |
||
| 349 | * @param Request $request |
||
| 350 | * the current request |
||
| 351 | * @param Application $app |
||
| 352 | * the Silex application |
||
| 353 | * @param string $entity |
||
| 354 | * the current entity |
||
| 355 | * |
||
| 356 | * @return Response |
||
| 357 | * the HTTP response of this action or 404 on invalid input |
||
| 358 | */ |
||
| 359 | public function showList(Request $request, Application $app, $entity) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * The controller for the "show" action. |
||
| 407 | * |
||
| 408 | * @param Application $app |
||
| 409 | * the Silex application |
||
| 410 | * @param string $entity |
||
| 411 | * the current entity |
||
| 412 | * @param string $id |
||
| 413 | * the instance id to show |
||
| 414 | * |
||
| 415 | * @return Response |
||
| 416 | * the HTTP response of this action or 404 on invalid input |
||
| 417 | */ |
||
| 418 | public function show(Application $app, $entity, $id) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * The controller for the "edit" action. |
||
| 456 | * |
||
| 457 | * @param Application $app |
||
| 458 | * the Silex application |
||
| 459 | * @param string $entity |
||
| 460 | * the current entity |
||
| 461 | * @param string $id |
||
| 462 | * the instance id to edit |
||
| 463 | * |
||
| 464 | * @return Response |
||
| 465 | * the HTTP response of this action or 404 on invalid input |
||
| 466 | */ |
||
| 467 | public function edit(Application $app, $entity, $id) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * The controller for the "delete" action. |
||
| 479 | * |
||
| 480 | * @param Application $app |
||
| 481 | * the Silex application |
||
| 482 | * @param string $entity |
||
| 483 | * the current entity |
||
| 484 | * @param string $id |
||
| 485 | * the instance id to delete |
||
| 486 | * |
||
| 487 | * @return Response |
||
| 488 | * redirects to the entity list page or 404 on invalid input |
||
| 489 | */ |
||
| 490 | public function delete(Application $app, $entity, $id) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * The controller for the "render file" action. |
||
| 521 | * |
||
| 522 | * @param Application $app |
||
| 523 | * the Silex application |
||
| 524 | * @param string $entity |
||
| 525 | * the current entity |
||
| 526 | * @param string $id |
||
| 527 | * the instance id |
||
| 528 | * @param string $field |
||
| 529 | * the field of the file to render of the instance |
||
| 530 | * |
||
| 531 | * @return Response |
||
| 532 | * the rendered file |
||
| 533 | */ |
||
| 534 | public function renderFile(Application $app, $entity, $id, $field) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * The controller for the "delete file" action. |
||
| 546 | * |
||
| 547 | * @param Application $app |
||
| 548 | * the Silex application |
||
| 549 | * @param string $entity |
||
| 550 | * the current entity |
||
| 551 | * @param string $id |
||
| 552 | * the instance id |
||
| 553 | * @param string $field |
||
| 554 | * the field of the file to delete of the instance |
||
| 555 | * |
||
| 556 | * @return Response |
||
| 557 | * redirects to the instance details page or 404 on invalid input |
||
| 558 | */ |
||
| 559 | public function deleteFile(Application $app, $entity, $id, $field) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * The controller for serving static files. |
||
| 577 | * |
||
| 578 | * @param Request $request |
||
| 579 | * the current request |
||
| 580 | * @param Application $app |
||
| 581 | * the Silex application |
||
| 582 | * |
||
| 583 | * @return Response |
||
| 584 | * redirects to the instance details page or 404 on invalid input |
||
| 585 | */ |
||
| 586 | public function staticFile(Request $request, Application $app) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * The controller for setting the locale. |
||
| 610 | * |
||
| 611 | * @param Request $request |
||
| 612 | * the current request |
||
| 613 | * @param Application $app |
||
| 614 | * the Silex application |
||
| 615 | * @param string $locale |
||
| 616 | * the new locale |
||
| 617 | * |
||
| 618 | * @return Response |
||
| 619 | * redirects to the instance details page or 404 on invalid input |
||
| 620 | */ |
||
| 621 | public function setLocale(Request $request, Application $app, $locale) { |
||
| 633 | } |
||
| 634 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.