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 |
||
| 36 | class TrackingController extends BaseController |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @Route("/seguimiento/seleccion/{id}", name="admin_group_student_agreements", methods={"GET"}) |
||
| 40 | * @Security("is_granted('GROUP_MANAGE', student.getStudentGroup())") |
||
| 41 | */ |
||
| 42 | public function studentAgreementIndexAction(User $student) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/seguimiento/acuerdo/{id}", name="admin_group_student_calendar", methods={"GET"}) |
||
| 61 | * @Security("is_granted('AGREEMENT_ACCESS', agreement)") |
||
| 62 | */ |
||
| 63 | public function studentCalendarAgreementIndexAction(Agreement $agreement) |
||
| 64 | { |
||
| 65 | $student = $agreement->getStudent(); |
||
| 66 | |||
| 67 | $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($agreement->getWorkdays()); |
||
| 68 | $title = (string) $agreement; |
||
| 69 | |||
| 70 | return $this->render('group/calendar_agreement.html.twig', |
||
| 71 | [ |
||
| 72 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'), |
||
| 73 | 'breadcrumb' => [ |
||
| 74 | ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]], |
||
| 75 | ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]], |
||
| 76 | ['fixed' => (string) $agreement->getWorkcenter()], |
||
| 77 | ], |
||
| 78 | 'title' => $title, |
||
| 79 | 'user' => $this->getUser(), |
||
| 80 | 'calendar' => $calendar, |
||
| 81 | 'agreement' => $agreement, |
||
| 82 | 'route_name' => 'admin_group_student_tracking', |
||
| 83 | 'activities_stats' => $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getActivitiesStats($agreement) |
||
| 84 | ]); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @Route("/seguimiento/acuerdo/{id}/operacion", name="admin_group_student_workday_operation", methods={"POST"}) |
||
| 89 | * @Security("is_granted('AGREEMENT_ACCESS', agreement)") |
||
| 90 | */ |
||
| 91 | public function operationWorkdayAction(Agreement $agreement, Request $request) |
||
| 92 | { |
||
| 93 | if ($request->request->has('delete')) { |
||
| 94 | return $this->deleteWorkdayAction($agreement, $request); |
||
| 95 | } elseif ($request->request->has('week_lock') || ($request->request->has('week_unlock'))) { |
||
| 96 | return $this->lockWeekAction($agreement, $request, $request->request->has('week_lock'), 'admin_group_student_calendar'); |
||
| 97 | } else { |
||
| 98 | return $this->lockWorkdayAction($agreement, $request, $request->request->has('lock'), 'admin_group_student_calendar'); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Route("/seguimiento/acuerdo/{id}/incorporar", name="admin_group_student_workday_add", methods={"GET", "POST"}) |
||
| 104 | * @Security("is_granted('AGREEMENT_MANAGE', agreement)") |
||
| 105 | */ |
||
| 106 | public function addAgreementCalendarAction(Agreement $agreement, Request $request) |
||
| 107 | { |
||
| 108 | $totalHours = $agreement->getStudent()->getStudentGroup()->getTraining()->getProgramHours(); |
||
| 109 | $agreementHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->countHours($agreement); |
||
| 110 | $studentHours = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->countAgreementHours($agreement->getStudent()); |
||
| 111 | |||
| 112 | $calendar = new Calendar(max(0, $totalHours - $studentHours)); |
||
| 113 | |||
| 114 | $form = $this->createForm('AppBundle\Form\Type\CalendarType', $calendar, [ |
||
| 115 | 'program_hours' => $totalHours |
||
| 116 | ]); |
||
| 117 | |||
| 118 | $form->handleRequest($request); |
||
| 119 | |||
| 120 | $workdays = new ArrayCollection(); |
||
| 121 | |||
| 122 | if ($form->isValid() && $form->isSubmitted()) { |
||
| 123 | $workdays = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->createCalendar($calendar, $agreement); |
||
| 124 | |||
| 125 | if ($request->request->has('submit')) { |
||
| 126 | $this->getDoctrine()->getManager()->flush(); |
||
| 127 | $agreement->setFromDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealFromDate($agreement)); |
||
| 128 | $agreement->setToDate($this->getDoctrine()->getManager()->getRepository('AppBundle:Agreement')->getRealToDate($agreement)); |
||
| 129 | $this->getDoctrine()->getManager()->flush(); |
||
| 130 | $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'calendar')); |
||
| 131 | return $this->redirectToRoute('admin_group_student_calendar', ['id' => $agreement->getId()]); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $student = $agreement->getStudent(); |
||
| 136 | |||
| 137 | $calendar = $this->getDoctrine()->getManager()->getRepository('AppBundle:Workday')->getArrayCalendar($workdays); |
||
| 138 | |||
| 139 | return $this->render('group/calendar_agreement_workday_add.html.twig', [ |
||
| 140 | 'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_tutor_group'), |
||
| 141 | 'breadcrumb' => [ |
||
| 142 | ['fixed' => $student->getStudentGroup()->getName(), 'path' => 'admin_group_students', 'options' => ['id' => $student->getStudentGroup()->getId()]], |
||
| 143 | ['fixed' => (string) $student, 'path' => 'admin_group_student_agreements', 'options' => ['id' => $student->getId()]], |
||
| 144 | ['fixed' => (string) $agreement->getWorkcenter()], |
||
| 145 | ['fixed' => $this->get('translator')->trans('form.add', [], 'calendar')] |
||
| 146 | ], |
||
| 147 | 'agreement' => $agreement, |
||
| 148 | 'total_hours' => $totalHours, |
||
| 149 | 'agreement_hours' => $agreementHours, |
||
| 150 | 'student_hours' => $studentHours, |
||
| 151 | 'form' => $form->createView(), |
||
| 152 | 'calendar' => $calendar |
||
| 153 | ]); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @Route("/seguimiento/acuerdo/jornada/{id}", name="admin_group_student_tracking", methods={"GET", "POST"}) |
||
| 158 | * @Security("is_granted('AGREEMENT_ACCESS', workday.getAgreement())") |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function studentWorkdayAction(Workday $workday, Request $request) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @Route("/seguimiento/acuerdo/jornada/modificar/{id}", name="admin_group_student_workday_form", methods={"GET", "POST"}) |
||
| 178 | * @Security("is_granted('AGREEMENT_MANAGE', workday.getAgreement())") |
||
| 179 | */ |
||
| 180 | public function agreementCalendarFormAction(Workday $workday, Request $request) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @Route("/seguimiento/acuerdo/nuevo/{id}", name="admin_group_student_agreement_new", methods={"GET", "POST"}) |
||
| 210 | * @Security("is_granted('GROUP_CREATE_AGREEMENT', student.getStudentGroup())") |
||
| 211 | */ |
||
| 212 | public function agreementNewFormAction(User $student, Request $request) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @Route("/seguimiento/acuerdo/modificar/{id}", name="admin_group_student_agreement_form", methods={"GET", "POST"}) |
||
| 223 | * @Security("is_granted('AGREEMENT_MANAGE', agreement)") |
||
| 224 | */ |
||
| 225 | public function agreementFormAction(Agreement $agreement, Request $request) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @Route("/seguimiento/acuerdo/eliminar/{id}", name="admin_group_student_agreement_delete", methods={"GET", "POST"}) |
||
| 267 | * @Security("is_granted('AGREEMENT_MANAGE', agreement)") |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function agreementDeleteAction(Agreement $agreement, Request $request) |
|
| 299 | } |
||
| 300 |
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.