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 |
||
| 32 | class IndexController extends AbstractActionController |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Repository\Job $jobRepository |
||
| 37 | */ |
||
| 38 | private $jobRepository; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Formular for searching job postings |
||
| 42 | * |
||
| 43 | * @var ListFilter $searchForm |
||
| 44 | */ |
||
| 45 | private $searchForm; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Construct the index controller |
||
| 49 | * |
||
| 50 | * @param Repository\Job $jobRepository |
||
| 51 | * @param ListFilter $searchForm |
||
| 52 | */ |
||
| 53 | public function __construct(Repository\Job $jobRepository, ListFilter $searchForm) |
||
| 54 | { |
||
| 55 | $this->jobRepository = $jobRepository; |
||
| 56 | $this->searchForm = $searchForm; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * attaches further Listeners for generating / processing the output |
||
| 61 | * |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | View Code Duplication | public function attachDefaultListeners() |
|
|
|
|||
| 65 | { |
||
| 66 | parent::attachDefaultListeners(); |
||
| 67 | |||
| 68 | $serviceLocator = $this->getServiceLocator(); |
||
| 69 | $defaultServices = $serviceLocator->get('DefaultListeners'); |
||
| 70 | $events = $this->getEventManager(); |
||
| 71 | $events->attach($defaultServices); |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * List job postings |
||
| 78 | * |
||
| 79 | * @return ViewModel |
||
| 80 | */ |
||
| 81 | public function indexAction() |
||
| 82 | { |
||
| 83 | /* @var $request \Zend\Http\Request */ |
||
| 84 | $request = $this->getRequest(); |
||
| 85 | $queryParams = $request->getQuery(); |
||
| 86 | $params = $queryParams->get('params', []); |
||
| 87 | $jsonFormat = 'json' == $queryParams->get('format'); |
||
| 88 | $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER); |
||
| 89 | |||
| 90 | if (!$jsonFormat && !$request->isXmlHttpRequest()) { |
||
| 91 | $session = new Session('Jobs\Index'); |
||
| 92 | $sessionKey = $this->auth()->isLoggedIn() ? 'userParams' : 'guestParams'; |
||
| 93 | $sessionParams = $session[$sessionKey]; |
||
| 94 | |||
| 95 | if ($sessionParams) { |
||
| 96 | $params = array_merge($sessionParams, $params); |
||
| 97 | } elseif ($isRecruiter) { |
||
| 98 | $params['by'] = 'me'; |
||
| 99 | } |
||
| 100 | |||
| 101 | $session[$sessionKey] = $params; |
||
| 102 | $queryParams->set('params', $params); |
||
| 103 | |||
| 104 | $this->searchForm->bind($queryParams); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!isset($params['sort'])) { |
||
| 108 | $params['sort'] = '-date'; |
||
| 109 | } |
||
| 110 | |||
| 111 | $paginator = $this->paginator('Jobs/Job', $params); |
||
| 112 | |||
| 113 | $return = array( |
||
| 114 | 'by' => $queryParams->get('by', 'all'), |
||
| 115 | 'jobs' => $paginator, |
||
| 116 | ); |
||
| 117 | if (isset($this->searchForm)) { |
||
| 118 | $return['filterForm'] = $this->searchForm; |
||
| 119 | } |
||
| 120 | |||
| 121 | $model = new ViewModel(); |
||
| 122 | $model->setVariables($return); |
||
| 123 | $model->setTemplate('jobs/index/index'); |
||
| 124 | return $model; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Handles the dashboard widget for the jobs module. |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function dashboardAction() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Action hook for Job search bar in list filter. |
||
| 155 | * |
||
| 156 | * @return JsonModel |
||
| 157 | */ |
||
| 158 | public function typeaheadAction() |
||
| 173 | } |
||
| 174 |
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.