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 |
||
10 | View Code Duplication | class ViewEvent extends Event |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * @var ViewInterface |
||
14 | */ |
||
15 | private $view; |
||
16 | |||
17 | /** |
||
18 | * @var AdminInterface |
||
19 | */ |
||
20 | private $admin; |
||
21 | |||
22 | /** |
||
23 | * @var Request |
||
24 | */ |
||
25 | private $request; |
||
26 | |||
27 | /** |
||
28 | * ViewEvent constructor. |
||
29 | * |
||
30 | * @param AdminInterface $admin |
||
31 | * @param Request $request |
||
32 | */ |
||
33 | public function __construct(AdminInterface $admin, Request $request) |
||
38 | |||
39 | /** |
||
40 | * @return ViewInterface |
||
41 | */ |
||
42 | public function getView(): ViewInterface |
||
46 | |||
47 | /** |
||
48 | * @param ViewInterface $view |
||
49 | */ |
||
50 | public function setView(ViewInterface $view) |
||
54 | |||
55 | /** |
||
56 | * @return AdminInterface |
||
57 | */ |
||
58 | public function getAdmin(): AdminInterface |
||
62 | |||
63 | /** |
||
64 | * @return Request |
||
65 | */ |
||
66 | public function getRequest(): Request |
||
70 | } |
||
71 |
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.