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 |
||
18 | View Code Duplication | class VideoController extends AbstractController |
|
|
|||
19 | { |
||
20 | /** @var Template |
||
21 | */ |
||
22 | private $template; |
||
23 | |||
24 | /** @var VideoService */ |
||
25 | private $videoService; |
||
26 | |||
27 | /** @var SessionManager */ |
||
28 | private $session; |
||
29 | |||
30 | /** @var Router */ |
||
31 | private $router; |
||
32 | |||
33 | /** @var CategoryService */ |
||
34 | private $categoryService; |
||
35 | |||
36 | /** |
||
37 | * VideoController constructor. |
||
38 | * |
||
39 | * @param Template $template |
||
40 | * @param Router $router |
||
41 | * @param VideoService $videoService |
||
42 | * @param SessionManager $session |
||
43 | * @param CategoryService $categoryService |
||
44 | */ |
||
45 | public function __construct( |
||
58 | |||
59 | public function index(): HtmlResponse |
||
70 | |||
71 | /** |
||
72 | * Add/Edit show form. |
||
73 | * |
||
74 | * @return \Psr\Http\Message\ResponseInterface |
||
75 | */ |
||
76 | public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
||
98 | |||
99 | /** |
||
100 | * Add/Edit article action. |
||
101 | * |
||
102 | * @throws FilterException if filter fails |
||
103 | * @throws \Exception |
||
104 | * |
||
105 | * @return \Psr\Http\Message\ResponseInterface |
||
106 | */ |
||
107 | public function save(): \Psr\Http\Message\ResponseInterface |
||
129 | |||
130 | /** |
||
131 | * Delete video by id. |
||
132 | * |
||
133 | * @throws \Exception |
||
134 | * |
||
135 | * @return \Psr\Http\Message\ResponseInterface |
||
136 | */ |
||
137 | public function delete(): \Psr\Http\Message\ResponseInterface |
||
149 | } |
||
150 |
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.