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:
| 1 | <?php |
||
| 35 | class AdminController extends Controller |
||
| 36 | { |
||
| 37 | public static $DEPARTMENT_ENTITY_DATA = [ |
||
| 38 | 'entity' => 'department', |
||
| 39 | 'entityClassName' => 'AppBundle\Entity\Department', |
||
| 40 | 'entityFormType' => 'AppBundle\Form\Type\DepartmentType', |
||
| 41 | 'query' => 'SELECT d FROM AppBundle:Department d LEFT JOIN d.head h', |
||
| 42 | 'defaultSortFieldName' => 'd.name', |
||
| 43 | 'columns' => [ |
||
| 44 | ['size' => '5', 'sort_field' => 'd.name', 'name' => 'department.name'], |
||
| 45 | ['size' => '4', 'sort_field' => 'h.displayName', 'name' => 'department.head'], |
||
| 46 | ], |
||
| 47 | 'data_columns' => ['name', 'head'] |
||
| 48 | ]; |
||
| 49 | |||
| 50 | public static $GROUP_ENTITY_DATA = [ |
||
| 51 | 'entity' => 'group', |
||
| 52 | 'entityClassName' => 'AppBundle\Entity\Group', |
||
| 53 | 'entityFormType' => 'AppBundle\Form\Type\GroupType', |
||
| 54 | 'query' => 'SELECT g FROM AppBundle:Group g JOIN g.training t', |
||
| 55 | 'defaultSortFieldName' => 'g.name', |
||
| 56 | 'columns' => [ |
||
| 57 | ['size' => '4', 'sort_field' => 'g.name', 'name' => 'form.name'], |
||
| 58 | ['size' => '5', 'sort_field' => 'g.training', 'name' => 'form.training'] |
||
| 59 | ], |
||
| 60 | 'data_columns' => ['name', 'training'] |
||
| 61 | ]; |
||
| 62 | |||
| 63 | public static $TRAINING_ENTITY_DATA = [ |
||
| 64 | 'entity' => 'training', |
||
| 65 | 'entityClassName' => 'AppBundle\Entity\Training', |
||
| 66 | 'entityFormType' => 'AppBundle\Form\Type\TrainingType', |
||
| 67 | 'query' => 'SELECT t FROM AppBundle:Training t JOIN t.department d', |
||
| 68 | 'defaultSortFieldName' => 't.name', |
||
| 69 | 'columns' => [ |
||
| 70 | ['size' => '4', 'sort_field' => 't.name', 'name' => 'form.name'], |
||
| 71 | ['size' => '3', 'sort_field' => 'd.name', 'name' => 'form.department'], |
||
| 72 | ['size' => '2', 'sort_field' => 't.programHours', 'name' => 'form.program_hours'] |
||
| 73 | ], |
||
| 74 | 'data_columns' => ['name', 'department', 'programHours'] |
||
| 75 | ]; |
||
| 76 | |||
| 77 | public static $NON_SCHOOL_DAY_ENTITY_DATA = [ |
||
| 78 | 'entity' => 'non_school_day', |
||
| 79 | 'entityClassName' => 'AppBundle\Entity\NonSchoolDay', |
||
| 80 | 'entityFormType' => 'AppBundle\Form\Type\NonSchoolDayType', |
||
| 81 | 'query' => 'SELECT n FROM AppBundle:NonSchoolDay n', |
||
| 82 | 'defaultSortFieldName' => 'n.date', |
||
| 83 | 'columns' => [ |
||
| 84 | ['size' => '2', 'sort_field' => 'n.date', 'name' => 'form.date'], |
||
| 85 | ['size' => '7', 'sort_field' => 'n.name', 'name' => 'form.name'] |
||
| 86 | ], |
||
| 87 | 'data_columns' => ['date', 'name'] |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @Route("/admin", name="admin_menu", methods={"GET"}) |
||
| 92 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 93 | */ |
||
| 94 | public function indexAction() |
||
| 103 | |||
| 104 | public function genericIndexAction($entityData, Request $request, $items = null, Query $query = null) |
||
| 156 | |||
| 157 | public function genericFormAction($entityData, $element, Request $request) |
||
| 158 | { |
||
| 159 | $em = $this->getDoctrine()->getManager(); |
||
| 160 | |||
| 161 | $new = (null === $element); |
||
| 162 | if ($new) { |
||
| 163 | $element = new $entityData['entityClassName']; |
||
| 164 | $em->persist($element); |
||
| 165 | } |
||
| 166 | |||
| 167 | $form = $this->createForm($entityData['entityFormType'], $element); |
||
| 168 | |||
| 169 | $form->handleRequest($request); |
||
| 170 | |||
| 171 | $menuItem = $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_' . $entityData['entity']); |
||
| 172 | |||
| 173 | View Code Duplication | if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|||
| 174 | |||
| 175 | // Probar a guardar los cambios |
||
| 176 | try { |
||
| 177 | $em->flush(); |
||
| 178 | $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'user')); |
||
| 179 | return $this->redirectToRoute($menuItem->getRouteName(), $menuItem->getRouteParams()); |
||
| 180 | } catch (\Exception $e) { |
||
| 181 | $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user')); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | $title = ((string) $element) ?: $this->get('translator')->trans('form.new', [], $entityData['entity']); |
||
| 186 | |||
| 187 | return $this->render(isset($entityData['form_template']) ? $entityData['form_template'] : 'admin/form_generic.html.twig', [ |
||
| 188 | 'form' => $form->createView(), |
||
| 189 | 'new' => $new, |
||
| 190 | 'menu_item' => $menuItem, |
||
| 191 | 'breadcrumb' => [ |
||
| 192 | ['fixed' => $title] |
||
| 193 | ], |
||
| 194 | 'element' => $element, |
||
| 195 | 'title' => $title, |
||
| 196 | 'entity' => $entityData['entity'] |
||
| 197 | ]); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function genericDeleteAction($entityData, $element, Request $request) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @Route("/admin/departamentos", name="admin_department", methods={"GET"}) |
||
| 233 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 234 | */ |
||
| 235 | public function departmentsIndexAction(Request $request) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @Route("/admin/departamentos/nuevo", name="admin_department_new", methods={"GET", "POST"}) |
||
| 242 | * @Route("/admin/departamentos/{id}", name="admin_department_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 243 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 244 | */ |
||
| 245 | public function departmentsFormAction(Department $element = null, Request $request) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @Route("/admin/departamentos/eliminar/{id}", name="admin_department_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 252 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 253 | */ |
||
| 254 | public function deleteElementAction(Department $element, Request $request) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @Route("/admin/grupos", name="admin_group", methods={"GET"}) |
||
| 261 | * @Security("is_granted('ROLE_DEPARTMENT_HEAD')") |
||
| 262 | */ |
||
| 263 | public function groupIndexAction(Request $request) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Route("/admin/grupos/nuevo", name="admin_group_new", methods={"GET", "POST"}) |
||
| 270 | * @Route("/admin/grupos/{id}", name="admin_group_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 271 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 272 | */ |
||
| 273 | public function groupFormAction(Group $element = null, Request $request) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @Route("/admin/grupos/eliminar/{id}", name="admin_group_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 280 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 281 | */ |
||
| 282 | public function groupDeleteAction(Group $element, Request $request) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @Route("/admin/ensenanzas", name="admin_training", methods={"GET"}) |
||
| 289 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 290 | */ |
||
| 291 | public function trainingIndexAction(Request $request) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @Route("/admin/ensenanzas/nueva", name="admin_training_new", methods={"GET", "POST"}) |
||
| 298 | * @Route("/admin/ensenanzas/{id}", name="admin_training_form", methods={"GET", "POST"}, requirements={"id": "\d+"}) |
||
| 299 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 300 | */ |
||
| 301 | public function trainingFormAction(Training $element = null, Request $request) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @Route("/admin/ensenanzas/eliminar/{id}", name="admin_training_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} ) |
||
| 308 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 309 | */ |
||
| 310 | public function trainingDeleteAction(Training $element, Request $request) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Route("/admin/diasnolectivos", name="admin_non_school_day", methods={"GET"}) |
||
| 317 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 318 | */ |
||
| 319 | public function nonSchoolDayIndexAction(Request $request) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @Route("/admin/diasnolectivos/nuevo", name="admin_non_school_day_new", methods={"GET", "POST"}) |
||
| 326 | * @Route("/admin/diasnolectivos/{id}", name="admin_non_school_day_form", methods={"GET", "POST"}) |
||
| 327 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 328 | */ |
||
| 329 | public function nonSchoolDayFormAction(NonSchoolDay $element = null, Request $request) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @Route("/admin/diasnolectivos/eliminar/{id}", name="admin_non_school_day_delete", methods={"GET", "POST"}) |
||
| 336 | * @Security("is_granted('ROLE_ADMIN')") |
||
| 337 | */ |
||
| 338 | public function workcenterDeleteAction(Workcenter $element, Request $request) |
||
| 342 | } |
||
| 343 |
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.