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 |
||
| 11 | class ItemPresenter extends BasePresenter |
||
| 12 | { |
||
| 13 | private $onPage; |
||
| 14 | |||
| 15 | private $sorting = []; |
||
| 16 | |||
| 17 | private $filter = []; |
||
| 18 | |||
| 19 | private $columns = []; |
||
| 20 | |||
| 21 | public function actionDefault($driver, $database, $type, $table, $page = 1, $onPage = FilterForm::DEFAULT_ON_PAGE, array $filter = [], array $sorting = []) |
||
| 32 | |||
| 33 | public function renderDefault($driver, $database, $type, $table, $page = 1, $onPage = FilterForm::DEFAULT_ON_PAGE, array $filter = [], array $sorting = []) |
||
| 34 | { |
||
| 35 | $this->template->driver = $driver; |
||
| 36 | $this->template->database = $database; |
||
| 37 | $this->template->type = $type; |
||
| 38 | $this->template->table = $table; |
||
| 39 | $this->template->sorting = $sorting; |
||
| 40 | $this->template->filter = $filter; |
||
| 41 | |||
| 42 | $itemsCount = $this->driver->dataManager()->itemsCount($type, $table, $filter); |
||
| 43 | $this->template->itemsCount = $itemsCount; |
||
| 44 | $this->template->items = $this->driver->dataManager()->items($type, $table, $page, $onPage, $filter, $sorting); |
||
| 45 | |||
| 46 | $this->template->columns = $this->columns; |
||
| 47 | |||
| 48 | $visualPaginator = $this['paginator']; |
||
| 49 | $paginator = $visualPaginator->getPaginator(); |
||
| 50 | $paginator->setItemCount($itemsCount); |
||
| 51 | $paginator->setItemsPerPage($onPage); |
||
| 52 | $paginator->page = $page; |
||
| 53 | |||
| 54 | foreach ($this->driver->dataManager()->getMessages() as $message => $type) { |
||
| 55 | $type = $type ?: 'info'; |
||
| 56 | $this->flashMessage($message, $type); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | View Code Duplication | public function actionCreate($driver, $database, $type, $table) |
|
| 70 | |||
| 71 | View Code Duplication | public function actionEdit($driver, $database, $type, $table, $item) |
|
| 82 | |||
| 83 | public function handleDelete($driver, $database, $type, $table, $item) |
||
| 84 | { |
||
| 85 | if (!$this->driver->permissions()->canDeleteItem($database, $type, $table, $item)) { |
||
| 86 | throw new ForbiddenRequestException('Delete item is not allowed'); |
||
| 87 | } |
||
| 88 | View Code Duplication | if ($this->driver->dataManager()->deleteItem($type, $table, $item)) { |
|
| 89 | $this->flashMessage('Item was successfully deleted', 'success'); |
||
| 90 | } else { |
||
| 91 | $this->flashMessage('Item was not deleted', 'danger'); |
||
| 92 | } |
||
| 93 | $this->redirect('this', $driver, $database, $type, $table); |
||
| 94 | } |
||
| 95 | |||
| 96 | protected function createComponentForm() |
||
| 100 | |||
| 101 | protected function createComponentFilterForm() |
||
| 102 | { |
||
| 103 | $form = $this->driver->formManager()->filterForm($this->translator, $this->columns, $this->filter, $this->sorting, $this->onPage); |
||
| 104 | $form->doRedirect[] = function ($onPage, $filter, $sorting) { |
||
| 105 | $this->redirect('Item:default', $this->driver->type(), $this->database, $this->type, $this->table, 1, $onPage, $filter, $sorting); |
||
| 106 | }; |
||
| 107 | $form->doReset[] = function () { |
||
| 108 | $this->redirect('Item:default', $this->driver->type(), $this->database, $this->type, $this->table); |
||
| 109 | }; |
||
| 110 | return $form; |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function createComponentPaginator() |
||
| 117 | |||
| 118 | protected function createComponentTablesSideBar() |
||
| 122 | } |
||
| 123 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.