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 | View Code Duplication | class SpaceShipController extends BackendController |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * Lists all SpaceShip entities. |
||
23 | * |
||
24 | * @Route("/", name="acme_app_spaceship_index") |
||
25 | * @Method("GET") |
||
26 | * @Template() |
||
27 | */ |
||
28 | public function indexAction() |
||
38 | |||
39 | /** |
||
40 | * Creates a new SpaceShip entity. |
||
41 | * |
||
42 | * @Route("/", name="acme_app_spaceship_create") |
||
43 | * @Method("POST") |
||
44 | * @Template("AcmeAppBundle:SpaceShip:new.html.twig") |
||
45 | */ |
||
46 | public function createAction(Request $request) |
||
65 | |||
66 | /** |
||
67 | * Creates a form to create a SpaceShip entity. |
||
68 | * |
||
69 | * @param SpaceShip $entity The entity |
||
70 | * |
||
71 | * @return \Symfony\Component\Form\Form The form |
||
72 | */ |
||
73 | private function createCreateForm(SpaceShip $entity) |
||
84 | |||
85 | /** |
||
86 | * Displays a form to create a new SpaceShip entity. |
||
87 | * |
||
88 | * @Route("/new", name="acme_app_spaceship_new") |
||
89 | * @Method("GET") |
||
90 | * @Template() |
||
91 | */ |
||
92 | public function newAction() |
||
102 | |||
103 | /** |
||
104 | * Finds and displays a SpaceShip entity. |
||
105 | * |
||
106 | * @Route("/{id}", name="acme_app_spaceship_show") |
||
107 | * @Method("GET") |
||
108 | * @Template() |
||
109 | */ |
||
110 | public function showAction($id) |
||
127 | |||
128 | /** |
||
129 | * Displays a form to edit an existing SpaceShip entity. |
||
130 | * |
||
131 | * @Route("/{id}/edit", name="acme_app_spaceship_edit") |
||
132 | * @Method("GET") |
||
133 | * @Template() |
||
134 | */ |
||
135 | public function editAction($id) |
||
154 | |||
155 | /** |
||
156 | * Creates a form to edit a SpaceShip entity. |
||
157 | * |
||
158 | * @param SpaceShip $entity The entity |
||
159 | * |
||
160 | * @return \Symfony\Component\Form\Form The form |
||
161 | */ |
||
162 | private function createEditForm(SpaceShip $entity) |
||
173 | |||
174 | /** |
||
175 | * Edits an existing SpaceShip entity. |
||
176 | * |
||
177 | * @Route("/{id}", name="acme_app_spaceship_update") |
||
178 | * @Method("PUT") |
||
179 | * @Template("AcmeAppBundle:SpaceShip:edit.html.twig") |
||
180 | */ |
||
181 | public function updateAction(Request $request, $id) |
||
207 | |||
208 | /** |
||
209 | * Deletes a SpaceShip entity. |
||
210 | * |
||
211 | * @Route("/{id}", name="acme_app_spaceship_delete") |
||
212 | * @Method("DELETE") |
||
213 | */ |
||
214 | public function deleteAction(Request $request, $id) |
||
233 | |||
234 | /** |
||
235 | * Creates a form to delete a SpaceShip 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 |
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.