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 |
||
| 12 | abstract class AbstractAdminGridController extends AbstractAdminController |
||
| 13 | { |
||
| 14 | // Events |
||
| 15 | const EVENT_CHECK_PERMISSIONS_LIST = 'at-admin.check-permissions.list'; |
||
| 16 | const EVENT_CHECK_PERMISSIONS_CREATE = 'at-admin.check-permissions.create'; |
||
| 17 | const EVENT_CHECK_PERMISSIONS_EDIT = 'at-admin.check-permissions.edit'; |
||
| 18 | const EVENT_CHECK_PERMISSIONS_DELETE = 'at-admin.check-permissions.delete'; |
||
| 19 | |||
| 20 | const EVENT_VALIDATION_EDIT_PRE = 'at-admin.validation.edit.pre'; |
||
| 21 | |||
| 22 | const EVENT_SAVE_PRE = 'at-admin.save.pre'; |
||
| 23 | const EVENT_SAVE_POST = 'at-admin.save.post'; |
||
| 24 | const EVENT_DELETE_PRE = 'at-admin.delete.pre'; |
||
| 25 | const EVENT_DELETE_POST = 'at-admin.delete.post'; |
||
| 26 | |||
| 27 | const EVENT_SET_TEMPLATE_CREATE = 'at-admin.set-template.create'; |
||
| 28 | const EVENT_SET_TEMPLATE_EDIT = 'at-admin.set-template.edit'; |
||
| 29 | |||
| 30 | // Page titles |
||
| 31 | // @todo Remove |
||
| 32 | const TITLE_ACTION_LIST = ''; |
||
| 33 | const TITLE_ACTION_CREATE = 'Create'; |
||
| 34 | const TITLE_ACTION_EDIT = 'Edit'; |
||
| 35 | const TITLE_ACTION_DELETE = 'Delete'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var GridManager |
||
| 39 | */ |
||
| 40 | protected $gridManager; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public function getAction() |
||
|
|
|||
| 46 | { |
||
| 47 | $this->getEventManager()->trigger(self::EVENT_CHECK_PERMISSIONS_LIST, $this); |
||
| 48 | |||
| 49 | // Save back url to redirect after actions |
||
| 50 | $this->backTo()->setBackUrl(); |
||
| 51 | |||
| 52 | // Check for mass actions |
||
| 53 | if (isset($_POST['cmd'])) { |
||
| 54 | $this->forward($_POST['cmd']); // @todo refactor this |
||
| 55 | } |
||
| 56 | |||
| 57 | $gridManager = $this->getGridManager(); |
||
| 58 | $grid = $gridManager->getGrid(); |
||
| 59 | |||
| 60 | if ($this->request->getQuery('order')) { |
||
| 61 | $order = explode('~', $this->request->getQuery('order')); |
||
| 62 | $grid->setOrder([$order[0] => $order[1]]); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($this->request->getQuery('page')) { |
||
| 66 | $grid->setCurrentPage($this->request->getQuery('page')); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($this->request->getQuery('show_items')) { |
||
| 70 | $grid->setItemsPerPage($this->request->getQuery('show_items')); |
||
| 71 | } |
||
| 72 | |||
| 73 | $filtersForm = $gridManager->getFiltersForm(); |
||
| 74 | $filtersForm->setData($this->request->getQuery()); |
||
| 75 | if (!$filtersForm->isValid()) { |
||
| 76 | //$this->flashMessenger()->addMessage($filtersForm->getMessages()); |
||
| 77 | } |
||
| 78 | |||
| 79 | $grid->setFiltersData($filtersForm->getData()); |
||
| 80 | |||
| 81 | return $gridManager->render(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return mixed|ViewModel |
||
| 86 | * @throws \Exception |
||
| 87 | */ |
||
| 88 | public function createAction() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return ViewModel |
||
| 148 | * @throws \Exception |
||
| 149 | */ |
||
| 150 | public function editAction() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @throws \Exception |
||
| 223 | */ |
||
| 224 | public function deleteAction() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param GridManager $manager |
||
| 260 | */ |
||
| 261 | public function setGridManager(GridManager $manager) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return Manager |
||
| 268 | */ |
||
| 269 | public function getGridManager() |
||
| 277 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: