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 |
||
| 3 | class MovieController extends Zend_Controller_Action |
||
|
|
|||
| 4 | { |
||
| 5 | 3 | public function init(): void |
|
| 18 | |||
| 19 | 1 | public function indexAction(): void |
|
| 20 | { |
||
| 21 | // Check there is at least one user, otherwise the whole page will crash |
||
| 22 | 1 | if (!\mQueue\Model\User::getCurrent() && !\mQueue\Model\UserMapper::getDbTable()->fetchRow()) { |
|
| 23 | throw new Exception('At least one user must exist to access this page'); |
||
| 24 | } |
||
| 25 | |||
| 26 | 1 | $form = new \mQueue\Form\Filters(); |
|
| 27 | 1 | $this->view->formFilter = $form; |
|
| 28 | |||
| 29 | // Detect if at least one filter was submitted |
||
| 30 | 1 | $submitted = false; |
|
| 31 | 1 | foreach ($this->getRequest()->getParams() as $key => $filter) { |
|
| 32 | 1 | if (preg_match('/^filter\d+$/', $key)) { |
|
| 33 | 1 | $submitted = true; |
|
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | // If was submitted, try to validate values |
||
| 38 | 1 | if ($submitted) { |
|
| 39 | if (!$form->isValid($this->getRequest()->getParams())) { |
||
| 40 | $this->_helper->FlashMessenger(['warning' => _tr('Filter is invalid.')]); |
||
| 41 | $form->setDefaults([]); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | // If we submitted a quicksearch, set default values to search with any status |
||
| 45 | 1 | elseif ($this->_getParam('search')) { |
|
| 46 | $form->setDefaults([ |
||
| 47 | 'filter1' => [ |
||
| 48 | 'user' => \mQueue\Model\User::getCurrent() ? 0 : \mQueue\Model\UserMapper::fetchAll()->current()->id, |
||
| 49 | 'condition' => 'is', |
||
| 50 | 'status' => array_merge([0], array_keys(mQueue\Model\Status::$ratings)), |
||
| 51 | 'title' => $this->_getParam('search'), |
||
| 52 | ], |
||
| 53 | ]); |
||
| 54 | } |
||
| 55 | // Otherwise clear the filter |
||
| 56 | else { |
||
| 57 | 1 | $form->setDefaults([]); |
|
| 58 | } |
||
| 59 | |||
| 60 | // Gather users selected in filters |
||
| 61 | 1 | $this->view->users = []; |
|
| 62 | 1 | $filters = $form->getValues(); |
|
| 63 | 1 | foreach ($filters as $key => $filter) { |
|
| 64 | 1 | if (!preg_match('/^filter\d+$/', $key)) { |
|
| 65 | 1 | continue; |
|
| 66 | } |
||
| 67 | |||
| 68 | 1 | $this->view->users[$filter['user']] = \mQueue\Model\UserMapper::find($filter['user']); |
|
| 69 | } |
||
| 70 | |||
| 71 | // If we ouput rss, we force sorting by date |
||
| 72 | 1 | if ($this->_helper->contextSwitch()->getCurrentContext() == 'rss') { |
|
| 73 | $this->getRequest()->setParam('sort', $filters['filter1']['withSource'] ? 'dateSearch' : 'date'); |
||
| 74 | $this->getRequest()->setParam('sortOrder', 'desc'); |
||
| 75 | } |
||
| 76 | 1 | $this->view->permanentParams = $form->getValues(); |
|
| 77 | 1 | $this->view->filterName = $form->getValuesText(); |
|
| 78 | 1 | unset($this->view->permanentParams['addFilter']); |
|
| 79 | |||
| 80 | 1 | $allowedSortingKey = ['title', 'date', 'dateSearch']; |
|
| 81 | 1 | $usersCount = count($this->view->users); |
|
| 82 | 1 | for ($i = 0; $i < $usersCount; ++$i) { |
|
| 83 | 1 | $allowedSortingKey[] = 'status' . $i; |
|
| 84 | } |
||
| 85 | 1 | $sort = $this->_helper->createSorting('sort', $allowedSortingKey); |
|
| 86 | |||
| 87 | // Set up the paginator: Apply pagination only if there is no special context (so it is normal html rendering) |
||
| 88 | 1 | $this->view->paginator = $this->_helper->createPaginator(\mQueue\Model\MovieMapper::getFilteredQuery($filters, $sort)); |
|
| 89 | 1 | } |
|
| 90 | |||
| 91 | View Code Duplication | public function viewAction(): void |
|
| 104 | |||
| 105 | 1 | public function addAction(): void |
|
| 127 | |||
| 128 | 1 | public function importAction(): void |
|
| 184 | } |
||
| 185 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.