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 UserController extends AbstractController |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Lists all User entities. |
||
| 40 | * |
||
| 41 | * @Route("/", name="admin_users") |
||
| 42 | * @Method("GET") |
||
| 43 | * @Template() |
||
| 44 | */ |
||
| 45 | public function indexAction() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Finds and displays a User entity. |
||
| 63 | * |
||
| 64 | * @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"}) |
||
| 65 | * @Method("GET") |
||
| 66 | * @Template() |
||
| 67 | */ |
||
| 68 | public function showAction(User $user) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Displays a form to create a new User entity. |
||
| 80 | * |
||
| 81 | * @Route("/new", name="admin_users_new") |
||
| 82 | * @Method("GET") |
||
| 83 | * @Template() |
||
| 84 | */ |
||
| 85 | public function newAction() |
||
| 86 | { |
||
| 87 | $user = new User(); |
||
| 88 | $form = $this->createForm(new UserType(), $user); |
||
| 89 | |||
| 90 | return array( |
||
| 91 | 'user' => $user, |
||
| 92 | 'form' => $form->createView(), |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Creates a new User entity. |
||
| 98 | * |
||
| 99 | * @Route("/create", name="admin_users_create") |
||
| 100 | * @Method("POST") |
||
| 101 | * @Template("AppBundle:User:new.html.twig") |
||
| 102 | */ |
||
| 103 | public function createAction(Request $request) |
||
| 104 | { |
||
| 105 | $user = new User(); |
||
| 106 | $form = $this->createForm(new UserType(), $user); |
||
| 107 | if ($form->handleRequest($request)->isValid()) { |
||
| 108 | $user->setEnabled(true); |
||
| 109 | $userManager = $this->get('fos_user.user_manager'); |
||
| 110 | $userManager->updateUser($user); |
||
| 111 | |||
| 112 | return $this->redirectToRoute('admin_users_show', array('id', $user->getId())); |
||
| 113 | } |
||
| 114 | |||
| 115 | return array( |
||
| 116 | 'user' => $user, |
||
| 117 | 'form' => $form->createView(), |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Displays a form to edit an existing User entity. |
||
| 123 | * |
||
| 124 | * @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"}) |
||
| 125 | * @Method("GET") |
||
| 126 | * @Template() |
||
| 127 | */ |
||
| 128 | public function editAction(User $user) |
||
| 129 | { |
||
| 130 | $editForm = $this->createForm(new UserType(), $user, array( |
||
| 131 | 'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
||
| 132 | 'method' => 'PUT', |
||
| 133 | 'passwordRequired' => false, |
||
| 134 | 'lockedRequired' => true |
||
| 135 | )); |
||
| 136 | $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
| 137 | |||
| 138 | return array( |
||
| 139 | 'user' => $user, |
||
| 140 | 'edit_form' => $editForm->createView(), |
||
| 141 | 'delete_form' => $deleteForm->createView(), |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Edits an existing User entity. |
||
| 147 | * |
||
| 148 | * @Route("/{id}/update", name="admin_users_update", requirements={"id"="\d+"}) |
||
| 149 | * @Method("PUT") |
||
| 150 | * @Template("AppBundle:User:edit.html.twig") |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function updateAction(User $user, Request $request) |
|
|
|
|||
| 153 | { |
||
| 154 | $editForm = $this->createForm(new UserType(), $user, array( |
||
| 155 | 'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
||
| 156 | 'method' => 'PUT', |
||
| 157 | 'passwordRequired' => false, |
||
| 158 | 'lockedRequired' => true |
||
| 159 | )); |
||
| 160 | if ($editForm->handleRequest($request)->isValid()) { |
||
| 161 | $userManager = $this->get('fos_user.user_manager'); |
||
| 162 | $userManager->updateUser($user); |
||
| 163 | |||
| 164 | return $this->redirectToRoute('admin_users_edit', array('id' => $user->getId())); |
||
| 165 | } |
||
| 166 | $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
| 167 | |||
| 168 | return array( |
||
| 169 | 'user' => $user, |
||
| 170 | 'edit_form' => $editForm->createView(), |
||
| 171 | 'delete_form' => $deleteForm->createView(), |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Save order. |
||
| 178 | * |
||
| 179 | * @Route("/order/{field}/{type}", name="admin_users_sort") |
||
| 180 | */ |
||
| 181 | public function sortAction($field, $type) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Save filters |
||
| 190 | * |
||
| 191 | * @param FormInterface $form |
||
| 192 | * @param string $name route/entity name |
||
| 193 | * @param string $route route name, if different from entity name |
||
| 194 | * @param Request $request Request |
||
| 195 | * @param array $params possible route parameters |
||
| 196 | * @return Response |
||
| 197 | */ |
||
| 198 | protected function saveFilter( |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Filter form |
||
| 221 | * |
||
| 222 | * @param FormInterface $form |
||
| 223 | * @param QueryBuilder $qb |
||
| 224 | * @param string $name |
||
| 225 | * @return \Knp\Component\Pager\Pagination\PaginationInterface |
||
| 226 | */ |
||
| 227 | protected function filter(FormInterface $form, QueryBuilder $qb, $name) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get filters from session |
||
| 242 | * |
||
| 243 | * @param string $name |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | protected function getFilter($name) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Deletes a User entity. |
||
| 253 | * |
||
| 254 | * @Security("has_role('ROLE_SUPER_ADMIN')") |
||
| 255 | * @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"}) |
||
| 256 | * @Method("DELETE") |
||
| 257 | */ |
||
| 258 | public function deleteAction(User $user, Request $request) |
||
| 259 | { |
||
| 260 | $form = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
||
| 261 | if ($form->handleRequest($request)->isValid()) { |
||
| 262 | $em = $this->getDoctrine()->getManager(); |
||
| 263 | $em->remove($user); |
||
| 264 | $em->flush(); |
||
| 265 | } |
||
| 266 | |||
| 267 | return $this->redirectToRoute('admin_users'); |
||
| 268 | } |
||
| 269 | } |
||
| 270 |
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.