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:
Complex classes like ManageController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ManageController extends AbstractActionController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * attaches further Listeners for generating / processing the output |
||
| 32 | * |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function attachDefaultListeners() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * (non-PHPdoc) |
||
| 47 | * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
||
| 48 | */ |
||
| 49 | public function onDispatch(\Zend\Mvc\MvcEvent $e) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * List applications |
||
| 63 | */ |
||
| 64 | public function indexAction() |
||
| 65 | { |
||
| 66 | $services = $this->getServiceLocator(); |
||
| 67 | /* @var \Jobs\Repository\Job $jobRepository */ |
||
| 68 | $jobRepository = $services->get('repositories')->get('Jobs/Job'); |
||
| 69 | /* @var \Applications\Repository\Application $applicationRepository */ |
||
| 70 | $applicationRepository = $services->get('repositories')->get('Applications/Application'); |
||
| 71 | $services_form = $services->get('forms'); |
||
| 72 | /* @var \Applications\Form\FilterApplication $form */ |
||
| 73 | $form = $services_form->get('Applications/Filter'); |
||
| 74 | $params = $this->getRequest()->getQuery(); |
||
| 75 | /* @var \Zend\Form\Element\Select $statusElement */ |
||
| 76 | $statusElement = $form->get('status'); |
||
| 77 | |||
| 78 | $states = $applicationRepository->getStates()->toArray(); |
||
| 79 | $states = array_merge(array(/*@translate*/ 'all'), $states); |
||
| 80 | |||
| 81 | $statesForSelections = array(); |
||
| 82 | foreach ($states as $state) { |
||
| 83 | $statesForSelections[$state] = $state; |
||
| 84 | } |
||
| 85 | $statusElement->setValueOptions($statesForSelections); |
||
| 86 | |||
| 87 | $job = $params->job ? $jobRepository->find($params->job) : null; |
||
| 88 | $paginator = $this->paginator('Applications'); |
||
| 89 | |||
| 90 | if ($job) { |
||
| 91 | $params['job_title'] = '[' . $job->getApplyId() . '] ' . $job->getTitle(); |
||
| 92 | } |
||
| 93 | |||
| 94 | $form->bind($params); |
||
| 95 | |||
| 96 | return array( |
||
| 97 | 'form' => $form, |
||
| 98 | 'applications' => $paginator, |
||
| 99 | 'byJobs' => 'jobs' == $params->get('by', 'me'), |
||
| 100 | 'sort' => $params->get('sort', 'none'), |
||
| 101 | 'search' => $params->get('search', ''), |
||
| 102 | 'job' => $job, |
||
| 103 | 'applicationStates' => $states, |
||
| 104 | 'applicationState' => $params->get('status', '') |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Detail view of an application |
||
| 110 | * |
||
| 111 | * @return array|JsonModel|ViewModel |
||
| 112 | */ |
||
| 113 | public function detailAction() |
||
| 114 | { |
||
| 115 | |||
| 116 | if ('refresh-rating' == $this->params()->fromQuery('do')) { |
||
| 117 | return $this->refreshRatingAction(); |
||
| 118 | } |
||
| 119 | |||
| 120 | $nav = $this->getServiceLocator()->get('Core/Navigation'); |
||
| 121 | $page = $nav->findByRoute('lang/applications'); |
||
| 122 | $page->setActive(); |
||
| 123 | |||
| 124 | /* @var \Applications\Repository\Application$repository */ |
||
| 125 | $repository = $this->getServiceLocator()->get('repositories')->get('Applications/Application'); |
||
| 126 | /* @var Application $application */ |
||
| 127 | $application = $repository->find($this->params('id')); |
||
| 128 | |||
| 129 | View Code Duplication | if (!$application) { |
|
| 130 | $this->response->setStatusCode(410); |
||
| 131 | $model = new ViewModel( |
||
| 132 | array( |
||
| 133 | 'content' => /*@translate*/ 'Invalid apply id' |
||
| 134 | ) |
||
| 135 | ); |
||
| 136 | $model->setTemplate('applications/error/not-found'); |
||
| 137 | return $model; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->acl($application, 'read'); |
||
| 141 | |||
| 142 | $applicationIsUnread = false; |
||
| 143 | if ($application->isUnreadBy($this->auth('id')) && $application->getStatus()) { |
||
| 144 | $application->addReadBy($this->auth('id')); |
||
| 145 | $applicationIsUnread = true; |
||
| 146 | $application->changeStatus( |
||
| 147 | $application->getStatus(), |
||
| 148 | sprintf(/*@translate*/ 'Application was read by %s' , |
||
| 149 | $this->auth()->getUser()->getInfo()->getDisplayName())); |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | $format=$this->params()->fromQuery('format'); |
||
| 155 | |||
| 156 | if ($application->isDraft()) { |
||
| 157 | $list = false; |
||
| 158 | } else { |
||
| 159 | $list = $this->paginationParams('Applications\Index', $repository); |
||
| 160 | $list->setCurrent($application->id); |
||
| 161 | } |
||
| 162 | |||
| 163 | $return = array( |
||
| 164 | 'application'=> $application, |
||
| 165 | 'list' => $list, |
||
| 166 | 'isUnread' => $applicationIsUnread, |
||
| 167 | 'format' => 'html' |
||
| 168 | ); |
||
| 169 | switch ($format) { |
||
| 170 | case 'json': |
||
| 171 | /*@deprecated - must be refactored */ |
||
| 172 | $viewModel = new JsonModel(); |
||
| 173 | $viewModel->setVariables( |
||
| 174 | /*array( |
||
| 175 | 'application' => */$this->getServiceLocator() |
||
| 176 | ->get('builders') |
||
| 177 | ->get('JsonApplication') |
||
| 178 | ->unbuild($application) |
||
| 179 | ); |
||
| 180 | $viewModel->setVariable('isUnread', $applicationIsUnread); |
||
| 181 | $return = $viewModel; |
||
| 182 | break; |
||
| 183 | case 'pdf': |
||
| 184 | $pdf = $this->getServiceLocator()->get('Core/html2pdf'); |
||
| 185 | $return['format'] = $format; |
||
| 186 | break; |
||
| 187 | default: |
||
| 188 | $contentCollector = $this->getPluginManager()->get('Core/ContentCollector'); |
||
| 189 | $contentCollector->setTemplate('applications/manage/details/action-buttons'); |
||
| 190 | $actionButtons = $contentCollector->trigger('application.detail.actionbuttons', $application); |
||
| 191 | |||
| 192 | $return = new ViewModel($return); |
||
| 193 | $return->addChild($actionButtons, 'externActionButtons'); |
||
| 194 | break; |
||
| 195 | } |
||
| 196 | |||
| 197 | return $return; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Refreshes the rating of an application |
||
| 202 | * |
||
| 203 | * @throws \DomainException |
||
| 204 | * @return \Zend\View\Model\ViewModel |
||
| 205 | */ |
||
| 206 | public function refreshRatingAction() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Attaches a social profile to an application |
||
| 224 | * @throws \InvalidArgumentException |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function socialProfileAction() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Changes the status of an application |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function statusAction() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Forwards an application via Email |
||
| 366 | * |
||
| 367 | * @throws \InvalidArgumentException |
||
| 368 | * @return \Zend\View\Model\JsonModel |
||
| 369 | */ |
||
| 370 | public function forwardAction() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Deletes an application |
||
| 415 | * |
||
| 416 | * @return array|\Zend\Http\Response |
||
| 417 | */ |
||
| 418 | public function deleteAction() |
||
| 440 | } |
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.