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 |
||
18 | class QuestionController extends Controller |
||
19 | { |
||
20 | /** |
||
21 | * @Route("/admin/question/new/{idModule}", name="create_question") |
||
22 | * @Template("@App/admin/question/createQuestion.html.twig") |
||
23 | */ |
||
24 | 1 | public function createQuestionAction(Request $request, $idModule) |
|
25 | { |
||
26 | 1 | $em = $this->getDoctrine()->getManager(); |
|
27 | 1 | $module = $em->getRepository('AppBundle:Module')->find($idModule); |
|
28 | 1 | $question = new Question(); |
|
29 | |||
30 | 1 | $maxSort = $em->getRepository('AppBundle:Question')->findMaxSortByModuleId($idModule); |
|
31 | |||
32 | 1 | $form = $this->createForm(QuestionType::class, $question, ['new_sort'=> ++$maxSort['max_sort']]); |
|
33 | |||
34 | 1 | $form->handleRequest($request); |
|
35 | |||
36 | 1 | if ($form->isValid()) { |
|
37 | if ($this->get('app.checkCount.answer')->checkCount($question)) { |
||
38 | $question->setModule($module); |
||
39 | $em->persist($question); |
||
40 | $em->flush(); |
||
41 | return $this->redirectToRoute('create_question', array('idModule' => $idModule)); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | 1 | return ['form' => $form->createView(), |
|
46 | 'idModule' => $idModule |
||
47 | 1 | ]; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @Route("/admin/question/edit/{id}/{idModule}", name="edit_question") |
||
52 | * @Template("@App/admin/question/editQuestion.html.twig") |
||
53 | */ |
||
54 | 1 | public function editQuestionAction(Request $request, $id, $idModule) |
|
55 | { |
||
56 | 1 | $em = $this->getDoctrine()->getManager(); |
|
57 | |||
58 | 1 | $question = $em->getRepository('AppBundle:Question') |
|
59 | 1 | ->find($id); |
|
60 | |||
61 | 1 | $originalAnswers = new ArrayCollection(); |
|
62 | |||
63 | // Create an ArrayCollection of the current Tag objects in the database |
||
64 | 1 | foreach ($question->getAnswers() as $answer) { |
|
65 | 1 | $originalAnswers->add($answer); |
|
66 | 1 | } |
|
67 | |||
68 | 1 | $form = $this->createForm(QuestionType::class, $question); |
|
69 | |||
70 | 1 | $form->handleRequest($request); |
|
71 | |||
72 | 1 | if ($form->isValid()) { |
|
73 | if ($this->get('app.checkCount.answer')->checkCount($question)) { |
||
74 | foreach ($originalAnswers as $answer) { |
||
75 | if (false === $question->getAnswers()->contains($answer)) { |
||
76 | $em->remove($answer); |
||
77 | } |
||
78 | } |
||
79 | $em->flush(); |
||
80 | } |
||
81 | |||
82 | return $this->redirectToRoute('edit_question', array('id' => $id, 'idModule' => $idModule)); |
||
83 | } |
||
84 | |||
85 | 1 | return ['form' => $form->createView(), |
|
86 | 'idModule' => $idModule |
||
87 | 1 | ]; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @Route("/admin/question/remove/{id}/{idModule}", name="remove_question") |
||
92 | * @Method("DELETE") |
||
93 | */ |
||
94 | 1 | public function removeQuestionAction($id, $idModule) |
|
95 | { |
||
96 | 1 | $em = $this->getDoctrine()->getManager(); |
|
97 | |||
98 | 1 | $question = $em->getRepository('AppBundle:Question') |
|
99 | 1 | ->find($id); |
|
100 | |||
101 | 1 | $em->remove($question); |
|
102 | 1 | $em->flush(); |
|
103 | |||
104 | 1 | return $this->redirectToRoute('show_question', array('idModule' => $idModule)); |
|
105 | |||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @Route("/admin/question/show/{idModule}", name="show_question") |
||
110 | * @Template("@App/admin/question/showQuestion.html.twig") |
||
111 | */ |
||
112 | 1 | public function showQuestionAction($idModule) |
|
113 | { |
||
114 | 1 | $em = $this->getDoctrine()->getManager(); |
|
115 | |||
116 | 1 | $question = $em->getRepository('AppBundle:Question') |
|
|
|||
117 | 1 | ->findByModuleWithSorting($idModule); |
|
118 | |||
119 | 1 | $form_delete = []; |
|
120 | |||
121 | 1 | foreach ($question as $item) { |
|
122 | 1 | $form_delete[$item->getId()] = $this->createFormDelete($item->getId(), $idModule)->createView(); |
|
123 | 1 | } |
|
124 | |||
125 | return [ |
||
126 | 1 | 'idModule' => $idModule, |
|
127 | 1 | 'questions' => $question, |
|
128 | 'form_remove' => $form_delete |
||
129 | 1 | ]; |
|
130 | |||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return \Symfony\Component\Form\Form |
||
135 | */ |
||
136 | 1 | View Code Duplication | private function createFormDelete($id, $idModule) |
150 | |||
151 | } |
||
152 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.