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 | trait FormTrait |
||
| 13 | { |
||
| 14 | private $entityClassName; |
||
| 15 | private $repositoryName; |
||
| 16 | private $twigFormDirectory; |
||
| 17 | private $formRoute; |
||
| 18 | private $translation; |
||
| 19 | private $formType; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Displays list of existing entities. |
||
| 23 | * |
||
| 24 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 25 | * |
||
| 26 | * @throws \LogicException |
||
| 27 | * @throws \UnexpectedValueException |
||
| 28 | */ |
||
| 29 | public function indexAction() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Displays a form to edit an existing entity. |
||
| 42 | * |
||
| 43 | * @param Request $request |
||
| 44 | * @param int $id |
||
| 45 | * |
||
| 46 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 47 | * |
||
| 48 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 49 | * @throws \LogicException |
||
| 50 | * @throws \InvalidArgumentException |
||
| 51 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 52 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 53 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 54 | */ |
||
| 55 | public function editAction(Request $request, $id) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Displays a form to edit an existing entity. |
||
| 88 | * |
||
| 89 | * @param Request $request |
||
| 90 | * |
||
| 91 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 92 | * |
||
| 93 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 94 | * @throws \LogicException |
||
| 95 | * @throws \InvalidArgumentException |
||
| 96 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 97 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 98 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 99 | */ |
||
| 100 | public function newAction(Request $request) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Creates a form to create a entity. |
||
| 129 | * |
||
| 130 | * @param $entity |
||
| 131 | * |
||
| 132 | * @return \Symfony\Component\Form\Form The form |
||
| 133 | * |
||
| 134 | * @throws \InvalidArgumentException |
||
| 135 | * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
||
| 136 | * @throws \Symfony\Component\Form\Exception\LogicException |
||
| 137 | * @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
||
| 138 | */ |
||
| 139 | private function createNewEditForm($entity) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $id |
||
| 170 | * |
||
| 171 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 172 | * |
||
| 173 | * @throws \LogicException |
||
| 174 | * @throws \InvalidArgumentException |
||
| 175 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 176 | */ |
||
| 177 | public function deleteConfirmAction($id) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param Request $request |
||
| 197 | * @param $id |
||
| 198 | * |
||
| 199 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 200 | * |
||
| 201 | * @throws \LogicException |
||
| 202 | * @throws \InvalidArgumentException |
||
| 203 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 204 | */ |
||
| 205 | public function deleteAction(Request $request, $id) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param $entity |
||
| 232 | * |
||
| 233 | * @return \Symfony\Component\Form\Form |
||
| 234 | * |
||
| 235 | * @throws \InvalidArgumentException |
||
| 236 | */ |
||
| 237 | private function createDeleteForm($entity) |
||
| 252 | } |
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.