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 | class CommentController implements |
||
19 | ConfigureInterface, |
||
20 | InjectionAwareInterface |
||
21 | { |
||
22 | use ConfigureTrait, |
||
23 | InjectionAwareTrait; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @var $data description |
||
29 | */ |
||
30 | //private $data; |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * Show all items. |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | public function getIndex() |
||
58 | |||
59 | |||
60 | |||
61 | /** |
||
62 | * Handler with form to create a new item. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function getPostCreateItem() |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Handler with form to delete an item. |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | View Code Duplication | public function getPostDeleteItem() |
|
109 | |||
110 | |||
111 | |||
112 | /** |
||
113 | * Handler with form to update an item. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | View Code Duplication | public function getPostUpdateItem($id) |
|
135 | } |
||
136 |
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.