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 UserController extends Controller |
||
19 | { |
||
20 | /** |
||
21 | * Lists all User entities. |
||
22 | */ |
||
23 | public function indexAction() |
||
32 | |||
33 | /** |
||
34 | * Finds and displays a User entity. |
||
35 | * |
||
36 | * @param string $id |
||
37 | * |
||
38 | * @return Response |
||
39 | */ |
||
40 | View Code Duplication | public function showAction($id) |
|
50 | |||
51 | /** |
||
52 | * Displays a form to edit an existing User entity. |
||
53 | * |
||
54 | * @param Request $request |
||
55 | * @param string $id |
||
56 | * |
||
57 | * @return RedirectResponse|Response |
||
58 | */ |
||
59 | public function editAction(Request $request, $id) |
||
79 | |||
80 | /** |
||
81 | * Deletes a User entity. |
||
82 | * |
||
83 | * @param Request $request |
||
84 | * @param string $id |
||
85 | * |
||
86 | * @return RedirectResponse |
||
87 | */ |
||
88 | View Code Duplication | public function deleteAction(Request $request, $id) |
|
101 | |||
102 | /** |
||
103 | * Creates a form to delete a User entity. |
||
104 | * |
||
105 | * @param User $user The User entity |
||
106 | * |
||
107 | * @return Form The form |
||
108 | */ |
||
109 | View Code Duplication | private function createDeleteForm(User $user) |
|
117 | } |
||
118 |
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.