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 QuestController extends Controller |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Lists all Quest entities. |
||
| 22 | * |
||
| 23 | * @return Response |
||
| 24 | */ |
||
| 25 | public function indexAction() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Creates a new Quest entity. |
||
| 37 | * |
||
| 38 | * @param Request $request |
||
| 39 | * |
||
| 40 | * @return RedirectResponse|Response |
||
| 41 | */ |
||
| 42 | public function newAction(Request $request) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Finds and displays a Quest entity. |
||
| 65 | * |
||
| 66 | * @param string $id |
||
| 67 | * |
||
| 68 | * @return Response |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function showAction($id) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Displays a form to edit an existing Quest entity. |
||
| 83 | * |
||
| 84 | * @param Request $request |
||
| 85 | * @param int $id |
||
| 86 | * |
||
| 87 | * @return RedirectResponse|Response |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function editAction(Request $request, $id) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Deletes a Quest entity. |
||
| 110 | * |
||
| 111 | * @param Request $request |
||
| 112 | * @param QuestInterface $quest |
||
| 113 | * |
||
| 114 | * @return RedirectResponse |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function deleteAction(Request $request, QuestInterface $quest) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Creates a form to delete a Quest entity. |
||
| 131 | * |
||
| 132 | * @param QuestInterface $quest The Quest entity |
||
| 133 | * |
||
| 134 | * @return Form The form |
||
| 135 | */ |
||
| 136 | View Code Duplication | private function createDeleteForm(QuestInterface $quest) |
|
| 144 | } |
||
| 145 |
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.