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 |
||
13 | class CommentController implements InjectionAwareInterface |
||
14 | { |
||
15 | use InjectionAwareTrait; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * View all posts |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | View Code Duplication | public function viewAll() |
|
30 | |||
31 | /** |
||
32 | * New post |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function newPost() |
||
41 | |||
42 | /** |
||
43 | * New post page |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | public function newPostAction() |
||
52 | |||
53 | /** |
||
54 | * Show one post |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | View Code Duplication | public function showOnePost($id) |
|
65 | |||
66 | /** |
||
67 | * Edit post |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | View Code Duplication | public function editPost($id) |
|
78 | |||
79 | /** |
||
80 | * Edit post action |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public function editPostAction() |
||
89 | |||
90 | /** |
||
91 | * Remove post |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function removePost($id) |
||
99 | } |
||
100 |
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.