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 |
||
| 17 | class EditFormHandler |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Twig_Environment |
||
| 21 | */ |
||
| 22 | private $twig; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Router |
||
| 26 | */ |
||
| 27 | private $router; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var RequestStack |
||
| 31 | */ |
||
| 32 | private $requestStack; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * EditFormHandler constructor. |
||
| 36 | * |
||
| 37 | * @param Twig_Environment $twig |
||
| 38 | * @param Router $router |
||
| 39 | * @param RequestStack $requestStack |
||
| 40 | */ |
||
| 41 | public function __construct(Twig_Environment $twig, Router $router, RequestStack $requestStack) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Save the entity if the form is valid, and redirect to the list action if |
||
| 50 | * required |
||
| 51 | * |
||
| 52 | * @param FormInterface $form |
||
| 53 | * @param AdminInterface $admin |
||
| 54 | * |
||
| 55 | * @return RedirectResponse|Response |
||
| 56 | */ |
||
| 57 | public function handle(FormInterface $form, AdminInterface $admin) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return true if the user should be redirected to the list action. |
||
| 94 | * |
||
| 95 | * @param AdminInterface $admin |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | private function shouldRedirect(AdminInterface $admin) |
||
| 117 | } |
||
| 118 |
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.