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 AdminController 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 AdminController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class AdminController extends Controller |
||
| 42 | { |
||
| 43 | public static $DEPARTMENT_ENTITY_DATA = [ |
||
| 44 | 'entity' => 'department', |
||
| 45 | 'entityClassName' => 'AppBundle\Entity\Department', |
||
| 46 | 'entityFormType' => 'AppBundle\Form\Type\DepartmentType', |
||
| 47 | 'query' => 'SELECT d FROM AppBundle:Department d LEFT JOIN d.head h', |
||
| 48 | 'defaultSortFieldName' => 'd.name', |
||
| 49 | 'columns' => [ |
||
| 50 | ['size' => '5', 'sort_field' => 'd.name', 'name' => 'department.name'], |
||
| 51 | ['size' => '4', 'sort_field' => 'h.displayName', 'name' => 'department.head'], |
||
| 52 | ], |
||
| 53 | 'data_columns' => ['name', 'head'] |
||
| 54 | ]; |
||
| 55 | |||
| 56 | public static $COMPANY_ENTITY_DATA = [ |
||
| 57 | 'entity' => 'company', |
||
| 58 | 'entityClassName' => 'AppBundle\Entity\Company', |
||
| 59 | 'entityFormType' => 'AppBundle\Form\Type\CompanyType', |
||
| 60 | 'query' => 'SELECT c FROM AppBundle:Company c', |
||
| 61 | 'defaultSortFieldName' => 'c.name', |
||
| 62 | 'columns' => [ |
||
| 63 | ['size' => '2', 'sort_field' => 'c.code', 'name' => 'company.code'], |
||
| 64 | ['size' => '7', 'sort_field' => 'c.name', 'name' => 'company.name'], |
||
| 65 | ], |
||
| 66 | 'data_columns' => ['code', 'name'] |
||
| 67 | ]; |
||
| 68 | |||
| 69 | public static $WORKCENTER_ENTITY_DATA = [ |
||
| 70 | 'entity' => 'workcenter', |
||
| 71 | 'entityClassName' => 'AppBundle\Entity\Workcenter', |
||
| 72 | 'entityFormType' => 'AppBundle\Form\Type\WorkcenterType', |
||
| 73 | 'query' => 'SELECT w FROM AppBundle:Workcenter w JOIN w.company c', |
||
| 74 | 'defaultSortFieldName' => 'c.name', |
||
| 75 | 'columns' => [ |
||
| 76 | ['size' => '4', 'sort_field' => 'c.name', 'name' => 'form.company'], |
||
| 77 | ['size' => '5', 'sort_field' => 'w.name', 'name' => 'form.name'], |
||
| 78 | ], |
||
| 79 | 'data_columns' => ['company', 'name'] |
||
| 80 | ]; |
||
| 81 | |||
| 82 | public static $GROUP_ENTITY_DATA = [ |
||
| 83 | 'entity' => 'group', |
||
| 84 | 'entityClassName' => 'AppBundle\Entity\Group', |
||
| 85 | 'entityFormType' => 'AppBundle\Form\Type\GroupType', |
||
| 86 | 'query' => 'SELECT g FROM AppBundle:Group g JOIN g.training t', |
||
| 87 | 'defaultSortFieldName' => 'g.name', |
||
| 88 | 'columns' => [ |
||
| 89 | ['size' => '4', 'sort_field' => 'g.name', 'name' => 'form.name'], |
||
| 90 | ['size' => '5', 'sort_field' => 'g.training', 'name' => 'form.training'] |
||
| 91 | ], |
||
| 92 | 'data_columns' => ['name', 'training'] |
||
| 93 | ]; |
||
| 94 | |||
| 95 | public static $TRAINING_ENTITY_DATA = [ |
||
| 96 | 'entity' => 'training', |
||
| 97 | 'entityClassName' => 'AppBundle\Entity\Training', |
||
| 98 | 'entityFormType' => 'AppBundle\Form\Type\TrainingType', |
||
| 99 | 'query' => 'SELECT t FROM AppBundle:Training t JOIN t.department d', |
||
| 100 | 'defaultSortFieldName' => 't.name', |
||
| 101 | 'columns' => [ |
||
| 102 | ['size' => '4', 'sort_field' => 't.name', 'name' => 'form.name'], |
||
| 103 | ['size' => '3', 'sort_field' => 'd.name', 'name' => 'form.department'], |
||
| 104 | ['size' => '2', 'sort_field' => 't.programHours', 'name' => 'form.program_hours'] |
||
| 105 | ], |
||
| 106 | 'data_columns' => ['name', 'department', 'programHours'] |
||
| 107 | ]; |
||
| 108 | |||
| 109 | public static $NON_SCHOOL_DAY_ENTITY_DATA = [ |
||
| 110 | 'entity' => 'non_school_day', |
||
| 111 | 'entityClassName' => 'AppBundle\Entity\NonSchoolDay', |
||
| 112 | 'entityFormType' => 'AppBundle\Form\Type\NonSchoolDayType', |
||
| 113 | 'query' => 'SELECT n FROM AppBundle:NonSchoolDay n', |
||
| 114 | 'defaultSortFieldName' => 'n.date', |
||
| 115 | 'columns' => [ |
||
| 116 | ['size' => '2', 'sort_field' => 'n.date', 'name' => 'form.date'], |
||
| 117 | ['size' => '7', 'sort_field' => 'n.name', 'name' => 'form.name'] |
||
| 118 | ], |
||
| 119 | 'data_columns' => ['date', 'name'] |
||
| 120 | ]; |
||
| 121 | |||
| 122 | public static $AGREEMENT_ENTITY_DATA = [ |
||
| 123 | 'entity' => 'agreement', |
||
| 124 | 'entityClassName' => 'AppBundle\Entity\Agreement', |
||
| 125 | 'entityFormType' => 'AppBundle\Form\Type\AgreementType', |
||
| 126 | 'form_template' => 'agreement/form_agreement.html.twig', |
||
| 127 | 'query' => null, |
||
| 128 | 'defaultSortFieldName' => 's.lastName', |
||
| 129 | 'columns' => [ |
||
| 130 | ['size' => '4', 'sort_field' => 's.displayName', 'name' => 'form.student'], |
||
| 131 | ['size' => '5', 'sort_field' => 'a.workcenter', 'name' => 'form.workcenter'] |
||
| 132 | ], |
||
| 133 | 'data_columns' => ['student', 'workcenter'] |
||
| 134 | ]; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @Route("/", name="admin_menu", methods={"GET"}) |
||
| 138 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 139 | */ |
||
| 140 | public function indexAction() |
||
| 149 | |||
| 150 | public function genericIndexAction($entityData, Request $request, $items = null, Query $query = null) |
||
| 202 | |||
| 203 | public function genericFormAction($entityData, $element, Request $request) |
||
| 245 | |||
| 246 | public function genericDeleteAction($entityData, $element, Request $request) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @Route("/departamentos", name="admin_department", methods={"GET"}) |
||
| 279 | */ |
||
| 280 | public function departmentsIndexAction(Request $request) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @Route("/departamentos/nuevo", name="admin_department_new", methods={"GET", "POST"}) |
||
| 287 | * @Route("/departamentos/{id}", name="admin_department_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 288 | */ |
||
| 289 | public function departmentsFormAction(Department $element = null, Request $request) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Route("/departamentos/eliminar/{id}", name="admin_department_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 296 | */ |
||
| 297 | public function deleteElementAction(Department $element, Request $request) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Route("/empresas", name="admin_company", methods={"GET"}) |
||
| 304 | */ |
||
| 305 | public function companiesIndexAction(Request $request) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @Route("/empresas/nueva", name="admin_company_new", methods={"GET", "POST"}) |
||
| 312 | * @Route("/empresas/{id}", name="admin_company_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 313 | */ |
||
| 314 | public function companyFormAction(Company $element = null, Request $request) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @Route("/empresas/eliminar/{id}", name="admin_company_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 321 | */ |
||
| 322 | public function companyDeleteAction(Company $element, Request $request) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @Route("/grupos", name="admin_group", methods={"GET"}) |
||
| 329 | */ |
||
| 330 | public function groupIndexAction(Request $request) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @Route("/grupos/nuevo", name="admin_group_new", methods={"GET", "POST"}) |
||
| 337 | * @Route("/grupos/{id}", name="admin_group_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 338 | */ |
||
| 339 | public function groupFormAction(Group $element = null, Request $request) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Route("/grupos/eliminar/{id}", name="admin_group_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 346 | */ |
||
| 347 | public function groupDeleteAction(Group $element, Request $request) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @Route("/ensenanzas", name="admin_training", methods={"GET"}) |
||
| 354 | */ |
||
| 355 | public function trainingIndexAction(Request $request) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Route("/ensenanzas/nueva", name="admin_training_new", methods={"GET", "POST"}) |
||
| 362 | * @Route("/ensenanzas/{id}", name="admin_training_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 363 | */ |
||
| 364 | public function trainingFormAction(Training $element = null, Request $request) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @Route("/ensenanzas/eliminar/{id}", name="admin_training_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 371 | */ |
||
| 372 | public function trainingDeleteAction(Training $element, Request $request) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Route("/diasnolectivos", name="admin_non_school_day", methods={"GET"}) |
||
| 379 | */ |
||
| 380 | public function nonSchoolDayIndexAction(Request $request) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @Route("/diasnolectivos/nuevo", name="admin_non_school_day_new", methods={"GET", "POST"}) |
||
| 387 | * @Route("/diasnolectivos/{id}", name="admin_non_school_day_form", methods={"GET", "POST"}) |
||
| 388 | */ |
||
| 389 | public function nonSchoolDayFormAction(NonSchoolDay $element = null, Request $request) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Route("/diasnolectivos/eliminar/{id}", name="admin_non_school_day_delete", methods={"GET", "POST"}) |
||
| 396 | */ |
||
| 397 | public function workcenterDeleteAction(Workcenter $element, Request $request) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @Route("/centros", name="admin_workcenter", methods={"GET"}) |
||
| 404 | */ |
||
| 405 | public function workcenterIndexAction(Request $request) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @Route("/centros/nuevo", name="admin_workcenter_new", methods={"GET", "POST"}) |
||
| 412 | * @Route("/centros/{id}", name="admin_workcenter_form", methods={"GET", "POST"}) |
||
| 413 | */ |
||
| 414 | public function workcenterFormAction(Workcenter $element = null, Request $request) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Route("/centros/eliminar/{id}", name="admin_workcenter_delete", methods={"GET", "POST"}) |
||
| 421 | */ |
||
| 422 | public function nonSchoolDayDeleteAction(Workcenter $element, Request $request) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @Route("/acuerdos", name="admin_agreement", methods={"GET"}) |
||
| 429 | */ |
||
| 430 | public function agreementIndexAction(Request $request) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @Route("/acuerdos/nuevo", name="admin_agreement_new", methods={"GET", "POST"}) |
||
| 462 | * @Route("/acuerdos/{id}", name="admin_agreement_form", methods={"GET", "POST"}) |
||
| 463 | */ |
||
| 464 | public function agreementFormAction(Agreement $element = null, Request $request) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @Route("/acuerdos/eliminar/{id}", name="admin_agreement_delete", methods={"GET", "POST"}) |
||
| 471 | */ |
||
| 472 | public function agreementDeleteAction(Agreement $element, Request $request) |
||
| 476 | } |
||
| 477 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.