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 |
||
| 8 | trait FormTrait |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Displays list of existing entities. |
||
| 12 | * |
||
| 13 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 14 | * |
||
| 15 | * @throws \LogicException |
||
| 16 | * @throws \UnexpectedValueException |
||
| 17 | */ |
||
| 18 | public function indexAction() |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Displays a form to edit an existing entity. |
||
| 34 | * |
||
| 35 | * @param Request $request |
||
| 36 | * @param int $id |
||
| 37 | * |
||
| 38 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 39 | * |
||
| 40 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 41 | * @throws \LogicException |
||
| 42 | * @throws \InvalidArgumentException |
||
| 43 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 44 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 45 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 46 | */ |
||
| 47 | public function editAction(Request $request, $id) |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Displays a form to edit an existing entity. |
||
| 81 | * |
||
| 82 | * @param Request $request |
||
| 83 | * |
||
| 84 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 85 | * |
||
| 86 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 87 | * @throws \LogicException |
||
| 88 | * @throws \InvalidArgumentException |
||
| 89 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 90 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 91 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 92 | */ |
||
| 93 | public function newAction(Request $request) |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Creates a form to create a entity. |
||
| 123 | * |
||
| 124 | * @param $entity |
||
| 125 | * |
||
| 126 | * @return \Symfony\Component\Form\Form The form |
||
| 127 | * |
||
| 128 | * @throws \InvalidArgumentException |
||
| 129 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 130 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 131 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 132 | */ |
||
| 133 | private function createNewEditForm($entity) |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $id |
||
| 165 | * |
||
| 166 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 167 | */ |
||
| 168 | public function deleteConfirmAction($id) |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Request $request |
||
| 189 | * @param $id |
||
| 190 | * |
||
| 191 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 192 | * |
||
| 193 | * @throws \LogicException |
||
| 194 | * @throws \InvalidArgumentException |
||
| 195 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 196 | */ |
||
| 197 | public function deleteAction(Request $request, $id) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $entity |
||
| 224 | * |
||
| 225 | * @return \Symfony\Component\Form\Form |
||
| 226 | * |
||
| 227 | * @throws \InvalidArgumentException |
||
| 228 | */ |
||
| 229 | private function createDeleteForm($entity) |
||
| 244 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.