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 | View Code Duplication | class BadgeCompletionController extends Controller |
|
15 | { |
||
16 | /** |
||
17 | * Lists all pending badge completions. |
||
18 | * |
||
19 | * @return Response |
||
20 | */ |
||
21 | public function indexAction() |
||
30 | |||
31 | /** |
||
32 | * Reject a badge completion by removing it from the database. |
||
33 | * |
||
34 | * @param string $id |
||
35 | * |
||
36 | * @return RedirectResponse |
||
37 | */ |
||
38 | public function rejectAction($id) |
||
61 | |||
62 | /** |
||
63 | * Accept a badge completion. |
||
64 | * |
||
65 | * @param string $id |
||
66 | * |
||
67 | * @return RedirectResponse |
||
68 | */ |
||
69 | public function acceptAction($id) |
||
101 | |||
102 | /** |
||
103 | * Give a badge to a user directly by creating a completed badge completion. |
||
104 | * |
||
105 | * @return RedirectResponse |
||
106 | */ |
||
107 | public function giveAction() |
||
111 | |||
112 | /** |
||
113 | * Remove a badge from a user by removing the badge completion. |
||
114 | * |
||
115 | * @return RedirectResponse |
||
116 | */ |
||
117 | public function removeAction() |
||
121 | } |
||
122 |
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.