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 |
||
| 28 | class CRUDController extends Controller |
||
| 29 | { |
||
| 30 | use ControllerTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Generic list action |
||
| 34 | * |
||
| 35 | * @Template("LAGAdminBundle:CRUD:list.html.twig") |
||
| 36 | * @param Request $request |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public function listAction(Request $request) |
||
| 40 | { |
||
| 41 | // retrieve admin from request route parameters |
||
| 42 | $admin = $this->getAdminFromRequest($request); |
||
| 43 | $admin->handleRequest($request, $this->getUser()); |
||
| 44 | // creating list form |
||
| 45 | $form = $this->createForm(AdminListType::class, [ |
||
| 46 | 'entities' => $admin->getEntities() |
||
| 47 | ], [ |
||
| 48 | 'batch_actions' => $admin |
||
| 49 | ->getCurrentAction() |
||
| 50 | ->getBatchActions() |
||
| 51 | ]); |
||
| 52 | $form->handleRequest($request); |
||
| 53 | |||
| 54 | if ($request->get('export')) { |
||
| 55 | return $this->exportEntities($admin, $request->get('export')); |
||
| 56 | } |
||
| 57 | if ($form->isValid()) { |
||
| 58 | // get ids and batch action from list form data |
||
| 59 | $formHandler = new ListFormHandler(); |
||
| 60 | $data = $formHandler->handle($form); |
||
| 61 | $batchForm = $this->createForm(BatchActionType::class, [ |
||
| 62 | 'batch_action' => $data['batch_action'], |
||
| 63 | 'entity_ids' => $data['ids'] |
||
| 64 | ], [ |
||
| 65 | 'labels' => $data['labels'] |
||
| 66 | ]); |
||
| 67 | |||
| 68 | // render batch view |
||
| 69 | return $this->render('LAGAdminBundle:CRUD:batch.html.twig', [ |
||
| 70 | 'admin' => $admin, |
||
| 71 | 'form' => $batchForm->createView() |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | return [ |
||
| 75 | 'admin' => $admin, |
||
| 76 | 'action' => $admin->getCurrentAction(), |
||
| 77 | 'form' => $form->createView() |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Request $request |
||
| 83 | * @return RedirectResponse |
||
| 84 | */ |
||
| 85 | public function batchAction(Request $request) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Generic create action |
||
| 114 | * |
||
| 115 | * @Template("LAGAdminBundle:CRUD:edit.html.twig") |
||
| 116 | * @param Request $request |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function createAction(Request $request) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Generic edit action. |
||
| 155 | * |
||
| 156 | * @Template("LAGAdminBundle:CRUD:edit.html.twig") |
||
| 157 | * |
||
| 158 | * @param Request $request |
||
| 159 | * |
||
| 160 | * @return array|RedirectResponse |
||
| 161 | */ |
||
| 162 | public function editAction(Request $request) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Generic delete action |
||
| 197 | * |
||
| 198 | * @Template("LAGAdminBundle:CRUD:delete.html.twig") |
||
| 199 | * |
||
| 200 | * @param Request $request |
||
| 201 | * |
||
| 202 | * @return RedirectResponse |
||
| 203 | */ |
||
| 204 | public function deleteAction(Request $request) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Export entities according to a type (json, csv, xls...) |
||
| 230 | * |
||
| 231 | * @param AdminInterface $admin |
||
| 232 | * @param $exportType |
||
| 233 | * |
||
| 234 | * @return Response |
||
| 235 | * |
||
| 236 | * @throws MappingException |
||
| 237 | */ |
||
| 238 | protected function exportEntities(AdminInterface $admin, $exportType) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Forward to 404 if user is not allowed by configuration for an action. |
||
| 283 | * |
||
| 284 | * @param AdminInterface $admin |
||
| 285 | */ |
||
| 286 | protected function forward404IfNotAllowed(AdminInterface $admin) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return an Admin object according to the request route parameters. |
||
| 307 | * |
||
| 308 | * @param Request $request |
||
| 309 | * @return AdminInterface |
||
| 310 | * @throws Exception |
||
| 311 | */ |
||
| 312 | protected function getAdminFromRequest(Request $request) |
||
| 318 | } |
||
| 319 |
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.