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 |
||
16 | class AdventureController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * Lists all Adventure entities. |
||
20 | * |
||
21 | * @return Response |
||
22 | */ |
||
23 | public function indexAction() |
||
32 | |||
33 | /** |
||
34 | * Creates or edits an Adventure entity. |
||
35 | * |
||
36 | * @param Request $request |
||
37 | * @param Adventure $adventure |
||
38 | * |
||
39 | * @return RedirectResponse|Response |
||
40 | */ |
||
41 | public function formAction(Request $request, Adventure $adventure = null) |
||
61 | |||
62 | /** |
||
63 | * Finds and displays a Adventure entity. |
||
64 | * |
||
65 | * @param Adventure $adventure |
||
66 | * |
||
67 | * @return Response |
||
68 | */ |
||
69 | public function showAction(Adventure $adventure) |
||
78 | |||
79 | /** |
||
80 | * Deletes a Adventure entity. |
||
81 | * |
||
82 | * @param Request $request |
||
83 | * @param Adventure $adventure |
||
84 | * |
||
85 | * @return RedirectResponse |
||
86 | */ |
||
87 | View Code Duplication | public function deleteAction(Request $request, Adventure $adventure) |
|
99 | |||
100 | /** |
||
101 | * Creates a form to delete a Badge entity. |
||
102 | * |
||
103 | * @param Adventure $adventure The Badge entity |
||
104 | * |
||
105 | * @return Form The form |
||
106 | */ |
||
107 | View Code Duplication | private function createDeleteForm(Adventure $adventure) |
|
114 | } |
||
115 |
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.