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 |
||
| 12 | class UserController extends Controller |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Displays list of existing User entities. |
||
| 16 | * |
||
| 17 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 18 | * |
||
| 19 | * @throws \LogicException |
||
| 20 | * @throws \UnexpectedValueException |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function indexAction() |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Displays a form to edit an existing User entity. |
||
| 37 | * |
||
| 38 | * @param Request $request |
||
| 39 | * @param int $id |
||
| 40 | * |
||
| 41 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 42 | * |
||
| 43 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 44 | * @throws \LogicException |
||
| 45 | * @throws \InvalidArgumentException |
||
| 46 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 47 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 48 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 49 | * @throws \OutOfBoundsException |
||
| 50 | */ |
||
| 51 | public function editAction(Request $request, $id) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Displays a form to edit an existing User entity. |
||
| 86 | * |
||
| 87 | * @param Request $request |
||
| 88 | * |
||
| 89 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 90 | * |
||
| 91 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 92 | * @throws \LogicException |
||
| 93 | * @throws \InvalidArgumentException |
||
| 94 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 95 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 96 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 97 | * @throws \OutOfBoundsException |
||
| 98 | */ |
||
| 99 | public function newAction(Request $request) |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Creates a form to create a User entity. |
||
| 127 | * |
||
| 128 | * @param UserInterface $entity The entity |
||
| 129 | * @param boolean $newUser Define if the for is for a new user entity |
||
| 130 | * |
||
| 131 | * @return \Symfony\Component\Form\Form The form |
||
| 132 | * |
||
| 133 | * @throws \InvalidArgumentException |
||
| 134 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 135 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 136 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 137 | */ |
||
| 138 | View Code Duplication | private function createNewEditForm(UserInterface $entity, $newUser = false) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param $id |
||
| 170 | * |
||
| 171 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function deleteConfirmAction($id) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param Request $request |
||
| 193 | * @param $id |
||
| 194 | * |
||
| 195 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 196 | * |
||
| 197 | * @throws \LogicException |
||
| 198 | * @throws \InvalidArgumentException |
||
| 199 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function deleteAction(Request $request, $id) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param User $entity |
||
| 228 | * |
||
| 229 | * @return \Symfony\Component\Form\Form |
||
| 230 | * |
||
| 231 | * @throws \InvalidArgumentException |
||
| 232 | */ |
||
| 233 | View Code Duplication | private function createDeleteForm(User $entity) |
|
| 248 | } |
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.