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 |
||
22 | class UserController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * Lists all User entities. |
||
26 | * |
||
27 | * @Route("/", name="admin_users") |
||
28 | * @Method("GET") |
||
29 | * @Template() |
||
30 | */ |
||
31 | public function indexAction() |
||
46 | |||
47 | /** |
||
48 | * Finds and displays a User entity. |
||
49 | * |
||
50 | * @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"}) |
||
51 | * @Method("GET") |
||
52 | * @Template() |
||
53 | */ |
||
54 | public function showAction(User $user) |
||
63 | |||
64 | /** |
||
65 | * Displays a form to create a new User entity. |
||
66 | * |
||
67 | * @Route("/new", name="admin_users_new") |
||
68 | * @Method("GET") |
||
69 | * @Template() |
||
70 | */ |
||
71 | public function newAction() |
||
72 | { |
||
73 | $user = new User(); |
||
74 | $form = $this->createForm(new UserType(), $user); |
||
75 | |||
76 | return array( |
||
77 | 'user' => $user, |
||
78 | 'form' => $form->createView(), |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Creates a new User entity. |
||
84 | * |
||
85 | * @Route("/create", name="admin_users_create") |
||
86 | * @Method("POST") |
||
87 | * @Template("AppBundle:User:new.html.twig") |
||
88 | */ |
||
89 | public function createAction(Request $request) |
||
90 | { |
||
91 | $user = new User(); |
||
92 | $form = $this->createForm(new UserType(), $user); |
||
93 | View Code Duplication | if ($form->handleRequest($request)->isValid()) { |
|
|
|||
94 | $user->setEnabled(true); |
||
95 | $userManager = $this->get('fos_user.user_manager'); |
||
96 | $userManager->updateUser($user); |
||
97 | |||
98 | return $this->redirect( |
||
99 | $this->generateUrl( |
||
100 | 'admin_users_show', |
||
101 | array('id' => $user->getId()) |
||
102 | ) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | return array( |
||
107 | 'user' => $user, |
||
108 | 'form' => $form->createView(), |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Displays a form to edit an existing User entity. |
||
114 | * |
||
115 | * @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"}) |
||
116 | * @Method("GET") |
||
117 | * @Template() |
||
118 | */ |
||
119 | public function editAction(User $user) |
||
120 | { |
||
121 | $editForm = $this->createForm(new UserType(), $user, array( |
||
122 | 'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
||
123 | 'method' => 'PUT', |
||
124 | 'passwordRequired' => false, |
||
125 | 'lockedRequired' => true |
||
126 | )); |
||
127 | $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
128 | |||
129 | return array( |
||
130 | 'user' => $user, |
||
131 | 'edit_form' => $editForm->createView(), |
||
132 | 'delete_form' => $deleteForm->createView(), |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Edits an existing User entity. |
||
138 | * |
||
139 | * @Route("/{id}/update", name="admin_users_update", requirements={"id"="\d+"}) |
||
140 | * @Method("PUT") |
||
141 | * @Template("AppBundle:User:edit.html.twig") |
||
142 | */ |
||
143 | public function updateAction(User $user, Request $request) |
||
144 | { |
||
145 | $editForm = $this->createForm(new UserType(), $user, array( |
||
146 | 'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
||
147 | 'method' => 'PUT', |
||
148 | 'passwordRequired' => false, |
||
149 | 'lockedRequired' => true |
||
150 | )); |
||
151 | View Code Duplication | if ($editForm->handleRequest($request)->isValid()) { |
|
152 | $userManager = $this->get('fos_user.user_manager'); |
||
153 | $userManager->updateUser($user); |
||
154 | |||
155 | return $this->redirect($this->generateUrl('admin_users_edit', array('id' => $user->getId()))); |
||
156 | } |
||
157 | $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
158 | |||
159 | return array( |
||
160 | 'user' => $user, |
||
161 | 'edit_form' => $editForm->createView(), |
||
162 | 'delete_form' => $deleteForm->createView(), |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Save order. |
||
169 | * |
||
170 | * @Route("/order/{field}/{type}", name="admin_users_sort") |
||
171 | */ |
||
172 | public function sortAction($field, $type) |
||
178 | |||
179 | /** |
||
180 | * @param string $name session name |
||
181 | * @param string $field field name |
||
182 | * @param string $type sort type ("ASC"/"DESC") |
||
183 | */ |
||
184 | protected function setOrder($name, $field, $type = 'ASC') |
||
188 | |||
189 | /** |
||
190 | * @param string $name |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function getOrder($name) |
||
199 | |||
200 | /** |
||
201 | * @param QueryBuilder $qb |
||
202 | * @param string $name |
||
203 | */ |
||
204 | View Code Duplication | protected function addQueryBuilderSort(QueryBuilder $qb, $name) |
|
211 | |||
212 | /** |
||
213 | * Save filters |
||
214 | * |
||
215 | * @param FormInterface $form |
||
216 | * @param string $name route/entity name |
||
217 | * @param string $route route name, if different from entity name |
||
218 | * @param array $params possible route parameters |
||
219 | * @return Response |
||
220 | */ |
||
221 | protected function saveFilter(FormInterface $form, $name, $route = null, array $params = null) |
||
235 | |||
236 | /** |
||
237 | * Filter form |
||
238 | * |
||
239 | * @param FormInterface $form |
||
240 | * @param QueryBuilder $qb |
||
241 | * @param string $name |
||
242 | * @return \Knp\Component\Pager\Pagination\PaginationInterface |
||
243 | */ |
||
244 | protected function filter(FormInterface $form, QueryBuilder $qb, $name) |
||
256 | |||
257 | /** |
||
258 | * Get filters from session |
||
259 | * |
||
260 | * @param string $name |
||
261 | * @return array |
||
262 | */ |
||
263 | protected function getFilter($name) |
||
267 | |||
268 | /** |
||
269 | * Deletes a User entity. |
||
270 | * |
||
271 | * @Security("has_role('ROLE_SUPER_ADMIN')") |
||
272 | * @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"}) |
||
273 | * @Method("DELETE") |
||
274 | */ |
||
275 | public function deleteAction(User $user, Request $request) |
||
276 | { |
||
277 | $form = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
278 | if ($form->handleRequest($request)->isValid()) { |
||
279 | $em = $this->getDoctrine()->getManager(); |
||
280 | $em->remove($user); |
||
281 | $em->flush(); |
||
282 | } |
||
283 | |||
284 | return $this->redirect($this->generateUrl('admin_users')); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Create Delete form |
||
289 | * |
||
290 | * @param integer $id |
||
291 | * @param string $route |
||
292 | * @return \Symfony\Component\Form\Form |
||
293 | */ |
||
294 | protected function createDeleteForm($id, $route) |
||
295 | { |
||
296 | return $this->createFormBuilder(null, array('attr' => array('id' => 'delete'))) |
||
297 | ->setAction($this->generateUrl($route, array('id' => $id))) |
||
298 | ->setMethod('DELETE') |
||
299 | ->getForm() |
||
300 | ; |
||
301 | } |
||
302 | |||
303 | } |
||
304 |
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.