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 |
||
14 | class CommentController implements InjectionAwareInterface |
||
15 | { |
||
16 | use InjectionAwareTrait; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Get ALL comments from an article. |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | View Code Duplication | public function getComments() |
|
34 | |||
35 | |||
36 | /** |
||
37 | * Get ONE comment from an article. |
||
38 | * |
||
39 | * @param string $key for the article |
||
40 | * @param int $id for the comment id |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | 2 | public function getComment($id) |
|
49 | |||
50 | |||
51 | /** |
||
52 | * Get ONE comment for editing. |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | View Code Duplication | public function getCommentToEdit() |
|
68 | |||
69 | |||
70 | /** |
||
71 | * Edit a comment. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | public function editComment() |
||
93 | |||
94 | |||
95 | |||
96 | /** |
||
97 | * Post a comment, with name and email. |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | View Code Duplication | public function postComment() |
|
111 | |||
112 | |||
113 | /** |
||
114 | * Update old comment with new comment |
||
115 | * |
||
116 | * @param int $id id for comment |
||
117 | * @param array $comment the comment-array (name, email, comment, id) |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | 1 | public function updateComment($id, $comment) |
|
125 | |||
126 | |||
127 | /** |
||
128 | * Delete comment with id |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | View Code Duplication | public function deleteComment() |
|
142 | } |
||
143 |
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.