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 |
||
20 | View Code Duplication | class ContactUsController extends AbstractController |
|
|
|||
21 | { |
||
22 | /** |
||
23 | * @var \Zend\Expressive\Router\RouterInterface $router |
||
24 | */ |
||
25 | protected $router; |
||
26 | |||
27 | /** |
||
28 | * @var \Zend\Expressive\Template\TemplateRendererInterface $template |
||
29 | */ |
||
30 | protected $template; |
||
31 | |||
32 | /** |
||
33 | * @var \ContactUs\Service\ContactUsService $contactUsService |
||
34 | */ |
||
35 | protected $contactUsService; |
||
36 | |||
37 | /** |
||
38 | * ContactUsController constructor. |
||
39 | * |
||
40 | * @param Template $template |
||
41 | * @param Router $router |
||
42 | * @param ContactUsService $contactUsService |
||
43 | */ |
||
44 | public function __construct(Template $template, Router $router, ContactUsService $contactUsService) |
||
50 | |||
51 | /** |
||
52 | * @return HtmlResponse |
||
53 | */ |
||
54 | public function index(): HtmlResponse |
||
70 | |||
71 | /** |
||
72 | * @param array $errors |
||
73 | * |
||
74 | * @return HtmlResponse |
||
75 | */ |
||
76 | public function edit($errors = []): HtmlResponse |
||
99 | |||
100 | /** |
||
101 | * @return ResponseInterface |
||
102 | * |
||
103 | * @throws \Exception |
||
104 | */ |
||
105 | public function save(): ResponseInterface |
||
129 | |||
130 | /** |
||
131 | * @return ResponseInterface |
||
132 | */ |
||
133 | public function delete(): ResponseInterface |
||
152 | } |
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.