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 |
||
19 | class JediController extends BackendController |
||
20 | { |
||
21 | /** |
||
22 | * Lists all Jedi entities. |
||
23 | * |
||
24 | * @Route("/", name="acme_app_jedi_index") |
||
25 | * @Method("GET") |
||
26 | * @Template() |
||
27 | */ |
||
|
|||
28 | View Code Duplication | public function indexAction() |
|
29 | { |
||
30 | $em = $this->getDoctrine()->getManager(); |
||
31 | |||
32 | $entities = $em->getRepository('AcmeAppBundle:Jedi')->findAll(); |
||
33 | |||
34 | return [ |
||
35 | 'entities' => $entities, |
||
36 | ]; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Creates a new Jedi entity. |
||
41 | * |
||
42 | * @Route("/", name="acme_app_jedi_create") |
||
43 | * @Method("POST") |
||
44 | * @Template("AcmeAppBundle:Jedi:new.html.twig") |
||
45 | */ |
||
46 | public function createAction(Request $request) |
||
65 | |||
66 | /** |
||
67 | * Creates a form to create a Jedi entity. |
||
68 | * |
||
69 | * @param Jedi $entity The entity |
||
70 | * |
||
71 | * @return \Symfony\Component\Form\Form The form |
||
72 | */ |
||
73 | private function createCreateForm(Jedi $entity) |
||
84 | |||
85 | /** |
||
86 | * Displays a form to create a new Jedi entity. |
||
87 | * |
||
88 | * @Route("/new", name="acme_app_jedi_new") |
||
89 | * @Method("GET") |
||
90 | * @Template() |
||
91 | */ |
||
92 | public function newAction() |
||
102 | |||
103 | /** |
||
104 | * Finds and displays a Jedi entity. |
||
105 | * |
||
106 | * @Route("/{id}", name="acme_app_jedi_show") |
||
107 | * @Method("GET") |
||
108 | * @Template() |
||
109 | */ |
||
110 | public function showAction($id) |
||
127 | |||
128 | /** |
||
129 | * Displays a form to edit an existing Jedi entity. |
||
130 | * |
||
131 | * @Route("/{id}/edit", name="acme_app_jedi_edit") |
||
132 | * @Method("GET") |
||
133 | * @Template() |
||
134 | */ |
||
135 | public function editAction($id) |
||
154 | |||
155 | /** |
||
156 | * Creates a form to edit a Jedi entity. |
||
157 | * |
||
158 | * @param Jedi $entity The entity |
||
159 | * |
||
160 | * @return \Symfony\Component\Form\Form The form |
||
161 | */ |
||
162 | private function createEditForm(Jedi $entity) |
||
173 | |||
174 | /** |
||
175 | * Edits an existing Jedi entity. |
||
176 | * |
||
177 | * @Route("/{id}", name="acme_app_jedi_update") |
||
178 | * @Method("PUT") |
||
179 | * @Template("AcmeAppBundle:Jedi:edit.html.twig") |
||
180 | */ |
||
181 | public function updateAction(Request $request, $id) |
||
207 | |||
208 | /** |
||
209 | * Deletes a Jedi entity. |
||
210 | * |
||
211 | * @Route("/{id}", name="acme_app_jedi_delete") |
||
212 | * @Method("DELETE") |
||
213 | */ |
||
214 | public function deleteAction(Request $request, $id) |
||
233 | |||
234 | /** |
||
235 | * Creates a form to delete a Jedi entity by id. |
||
236 | * |
||
237 | * @param mixed $id The entity id |
||
238 | * |
||
239 | * @return \Symfony\Component\Form\Form The form |
||
240 | */ |
||
241 | private function createDeleteForm($id) |
||
251 | |||
252 | /** |
||
253 | * Lists all Jedi entities for front display. |
||
254 | * |
||
255 | * @Route("/front/index", name="acme_app_jedi_front_index") |
||
256 | * @Method("GET") |
||
257 | * @Template() |
||
258 | */ |
||
259 | View Code Duplication | public function indexFrontAction() |
|
269 | |||
270 | /** |
||
271 | * Finds and displays a Jedi entity for front display. |
||
272 | * |
||
273 | * @Route("/front/show/{id}", name="acme_app_jedi_front_show") |
||
274 | * @Method("GET") |
||
275 | * @Template() |
||
276 | */ |
||
277 | public function showFrontAction($id) |
||
291 | } |
||
292 |