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 BadgeController extends Controller |
||
19 | { |
||
20 | /** |
||
21 | * Lists all Badge entities. |
||
22 | * |
||
23 | * @return Response |
||
24 | */ |
||
25 | public function indexAction() |
||
34 | |||
35 | /** |
||
36 | * Creates a new Badge entity. |
||
37 | * |
||
38 | * @param Request $request |
||
39 | * |
||
40 | * @return RedirectResponse|Response |
||
41 | */ |
||
42 | public function newAction(Request $request) |
||
66 | |||
67 | /** |
||
68 | * Finds and displays a Badge entity. |
||
69 | * |
||
70 | * @param string $id |
||
71 | * |
||
72 | * @return Response |
||
73 | */ |
||
74 | View Code Duplication | public function showAction($id) |
|
84 | |||
85 | /** |
||
86 | * Displays a form to edit an existing Badge entity. |
||
87 | * |
||
88 | * @param Request $request |
||
89 | * @param string $id |
||
90 | * |
||
91 | * @return RedirectResponse|Response |
||
92 | */ |
||
93 | public function editAction(Request $request, $id) |
||
115 | |||
116 | /** |
||
117 | * Deletes a Badge entity. |
||
118 | * |
||
119 | * @param Request $request |
||
120 | * @param string $id |
||
121 | * |
||
122 | * @return RedirectResponse |
||
123 | */ |
||
124 | View Code Duplication | public function deleteAction(Request $request, $id) |
|
137 | |||
138 | /** |
||
139 | * Creates a form to delete a Badge entity. |
||
140 | * |
||
141 | * @param BadgeInterface $badge The Badge entity |
||
142 | * |
||
143 | * @return Form The form |
||
144 | */ |
||
145 | View Code Duplication | private function createDeleteForm(BadgeInterface $badge) |
|
153 | } |
||
154 |
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.