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 |
||
| 34 | class VisitController extends Controller |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @Route("", name="visit_index", methods={"GET"}) |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function teacherIndexAction() |
|
|
|
|||
| 40 | { |
||
| 41 | /** @var User $user */ |
||
| 42 | $user = $this->getUser(); |
||
| 43 | if ($this->isGranted('ROLE_ADMIN')) { |
||
| 44 | $visits = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->getEducationalTutorsAgreementSummary(); |
||
| 45 | } elseif ($this->isGranted('ROLE_DEPARTMENT_HEAD')) { |
||
| 46 | $visits = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->getEducationalTutorsByDepartmentsAgreementSummary($user->getDirects()); |
||
| 47 | } else { |
||
| 48 | return $this->visitIndexAction($user); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $this->render('visit/tutor_index.html.twig', [ |
||
| 52 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('visit_index'), |
||
| 53 | 'title' => null, |
||
| 54 | 'elements' => $visits |
||
| 55 | ]); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @Route("/{id}", name="visit_workcenter_index", methods={"GET"}) |
||
| 60 | * @Security("is_granted('USER_VISIT_TRACK', tutor)") |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function visitIndexAction(User $tutor) |
|
| 63 | { |
||
| 64 | $workcenters = $this->getDoctrine()->getManager()->getRepository('AppBundle:Visit')->getRelatedVisits($tutor); |
||
| 65 | |||
| 66 | $title = (string) $tutor; |
||
| 67 | |||
| 68 | return $this->render('visit/visit_index.html.twig', [ |
||
| 69 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('visit_index'), |
||
| 70 | 'breadcrumb' => [ |
||
| 71 | ['fixed' => $title] |
||
| 72 | ], |
||
| 73 | 'title' => $this->get('translator')->trans('browse.workcenter', ['%user%' => $title], 'visit'), |
||
| 74 | 'tutor' => $tutor, |
||
| 75 | 'elements' => $workcenters, |
||
| 76 | 'back_route_name' => $this->isGranted('ROLE_DEPARTMENT_HEAD') ? 'visit_index' : 'frontpage' |
||
| 77 | ]); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @Route("/{id}/resumen", name="visit_workcenter_summary_index", methods={"GET"}) |
||
| 82 | * @Security("is_granted('USER_VISIT_TRACK', tutor)") |
||
| 83 | */ |
||
| 84 | public function visitWorkcenterSummaryAction(User $tutor) |
||
| 85 | { |
||
| 86 | $workcenters = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workcenter')->getVisitRelatedWorkcenters($tutor); |
||
| 87 | |||
| 88 | $title = (string) $tutor; |
||
| 89 | |||
| 90 | return $this->render('visit/workcenter_summary.html.twig', [ |
||
| 91 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('visit_index'), |
||
| 92 | 'breadcrumb' => [ |
||
| 93 | ['fixed' => $title, 'path' => 'visit_workcenter_index', 'options' => ['id' => $tutor->getId()]], |
||
| 94 | ['fixed' => $this->get('translator')->trans('browse.summary', [], 'visit')] |
||
| 95 | ], |
||
| 96 | 'title' => $this->get('translator')->trans('browse.workcenter', ['%user%' => $title], 'visit'), |
||
| 97 | 'tutor' => $tutor, |
||
| 98 | 'elements' => $workcenters, |
||
| 99 | 'back_route_name' => $this->isGranted('ROLE_DEPARTMENT_HEAD') ? 'visit_index' : 'frontpage' |
||
| 100 | ]); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @Route("/{id}/modificar/{visit}", name="visit_form", methods={"GET", "POST"}) |
||
| 105 | * @Security("is_granted('USER_VISIT_TRACK', tutor) and visit.getTutor() == tutor") |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function visitFormAction(User $tutor, Visit $visit, Request $request) |
|
| 108 | { |
||
| 109 | $form = $this->createForm('AppBundle\Form\Type\VisitType', $visit); |
||
| 110 | $form->handleRequest($request); |
||
| 111 | |||
| 112 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 113 | $this->getDoctrine()->getManager()->persist($visit); |
||
| 114 | $this->getDoctrine()->getManager()->flush(); |
||
| 115 | $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'visit')); |
||
| 116 | return $this->redirectToRoute('visit_workcenter_index', ['id' => $tutor->getId()]); |
||
| 117 | } |
||
| 118 | $title = $this->get('translator')->trans($visit->getId() ? 'form.view' : 'form.new', [], 'visit'); |
||
| 119 | |||
| 120 | return $this->render('visit/form_visit.html.twig', [ |
||
| 121 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('visit_index'), |
||
| 122 | 'breadcrumb' => [ |
||
| 123 | ['fixed' => (string) $tutor, 'path' => 'visit_workcenter_index', 'options' => ['id' => $tutor->getId()]], |
||
| 124 | ['fixed' => $title] |
||
| 125 | ], |
||
| 126 | 'title' => $title, |
||
| 127 | 'visit' => $visit, |
||
| 128 | 'form' => $form->createView(), |
||
| 129 | 'tutor' => $tutor |
||
| 130 | ]); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @Route("/{id}/eliminar/{visit}", name="visit_delete", methods={"GET", "POST"}) |
||
| 135 | * @Security("is_granted('USER_VISIT_TRACK', tutor) and visit.getTutor() == tutor") |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function visitDeleteAction(User $tutor, Visit $visit, Request $request) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @Route("/{id}/registrar", name="visit_form_new", methods={"GET", "POST"}) |
||
| 171 | * @Security("is_granted('USER_VISIT_TRACK', tutor)") |
||
| 172 | */ |
||
| 173 | public function visitNewAction(User $tutor, Request $request) |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * @Route("/{id}/informe", name="visit_workcenter_report", methods={"GET"}) |
||
| 185 | * @Security("is_granted('USER_VISIT_TRACK', tutor)") |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function downloadActivityReportAction(User $tutor) |
|
| 210 | } |
||
| 211 |
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.