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 |
||
| 45 | class ManageController extends AbstractActionController |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var AuthenticationService |
||
| 49 | */ |
||
| 50 | protected $auth; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var RepositoryService |
||
| 54 | */ |
||
| 55 | protected $repositoryService; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Translator |
||
| 59 | */ |
||
| 60 | protected $translator; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param AuthenticationService $auth |
||
| 64 | * @param RepositoryService $repositoryService |
||
| 65 | * @param $translator |
||
| 66 | */ |
||
| 67 | public function __construct(AuthenticationService $auth, RepositoryService $repositoryService, Translator $translator) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return $this|void |
||
| 76 | */ |
||
| 77 | public function attachDefaultListeners() |
||
| 78 | { |
||
| 79 | parent::attachDefaultListeners(); |
||
| 80 | $events = $this->getEventManager(); |
||
| 81 | $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10); |
||
| 82 | $serviceLocator = $this->serviceLocator; |
||
| 83 | $defaultServices = $serviceLocator->get('DefaultListeners'); |
||
| 84 | $events = $this->getEventManager(); |
||
| 85 | $events->attach($defaultServices); |
||
|
|
|||
| 86 | |||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Dispatch listener callback. |
||
| 92 | * |
||
| 93 | * Attaches the MailSender aggregate listener to the job event manager. |
||
| 94 | * |
||
| 95 | * @param MvcEvent $e |
||
| 96 | * @since 0.19 |
||
| 97 | */ |
||
| 98 | public function preDispatch(MvcEvent $e) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * |
||
| 117 | * |
||
| 118 | * @return null|ViewModel |
||
| 119 | */ |
||
| 120 | public function editAction() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return null|ViewModel |
||
| 134 | */ |
||
| 135 | public function saveAction() |
||
| 139 | |||
| 140 | public function channelListAction() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * save a Job-Post either by a regular request or by an async post (AJAX) |
||
| 163 | * a mandatory parameter is the ID of the Job |
||
| 164 | * in case of a regular Request you can |
||
| 165 | * |
||
| 166 | * parameter are arbitrary elements for defaults or programming flow |
||
| 167 | * |
||
| 168 | * @param array $parameter |
||
| 169 | * @return null|ViewModel |
||
| 170 | * @throws \RuntimeException |
||
| 171 | */ |
||
| 172 | protected function save() |
||
| 173 | { |
||
| 174 | $serviceLocator = $this->serviceLocator; |
||
| 175 | $formEvents = $serviceLocator->get('Jobs/JobContainer/Events'); |
||
| 176 | |||
| 177 | $user = $this->auth->getUser(); |
||
| 178 | if (empty($user->info->email)) { |
||
| 179 | return $this->getErrorViewModel('no-parent', array('cause' => 'noEmail')); |
||
| 180 | } |
||
| 181 | $userOrg = $user->getOrganization(); |
||
| 182 | if (!$userOrg->hasAssociation() || $userOrg->getOrganization()->isDraft()) { |
||
| 183 | return $this->getErrorViewModel('no-parent', array('cause' => 'noCompany')); |
||
| 184 | } |
||
| 185 | |||
| 186 | try { |
||
| 187 | $jobEntity = $this->initializeJob()->get($this->params(), true, true); |
||
| 188 | } catch (NotFoundException $e) { |
||
| 189 | $this->getResponse()->setStatusCode(Response::STATUS_CODE_404); |
||
| 190 | return [ |
||
| 191 | 'message' => sprintf($this->translator->translate('Job with id "%s" not found'), $e->getId()), |
||
| 192 | 'exception' => $e |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** @var \Zend\Http\Request $request */ |
||
| 197 | $request = $this->getRequest(); |
||
| 198 | $isAjax = $request->isXmlHttpRequest(); |
||
| 199 | |||
| 200 | $params = $this->params(); |
||
| 201 | $formIdentifier = $params->fromQuery('form'); |
||
| 202 | |||
| 203 | |||
| 204 | $viewModel = null; |
||
| 205 | $this->acl($jobEntity, 'edit'); |
||
| 206 | if ($status = $params->fromQuery('status')) { |
||
| 207 | $this->changeStatus($jobEntity, $status); |
||
| 208 | } |
||
| 209 | |||
| 210 | $form = $this->getFormular($jobEntity); |
||
| 211 | |||
| 212 | $valid = true; |
||
| 213 | $instanceForm = null; |
||
| 214 | $formErrorMessages = array(); |
||
| 215 | |||
| 216 | if (isset($formIdentifier) && $request->isPost()) { |
||
| 217 | // at this point the form get instantiated and immediately accumulated |
||
| 218 | $instanceForm = $form->getForm($formIdentifier); |
||
| 219 | if (!isset($instanceForm)) { |
||
| 220 | throw new \RuntimeException('No form found for "' . $formIdentifier . '"'); |
||
| 221 | } |
||
| 222 | // the id may be part of the postData, but it never should be altered |
||
| 223 | $postData = $request->getPost(); |
||
| 224 | if (isset($postData['id'])) { |
||
| 225 | unset($postData['id']); |
||
| 226 | } |
||
| 227 | unset($postData['applyId']); |
||
| 228 | $instanceForm->setData($postData); |
||
| 229 | $valid = $instanceForm->isValid(); |
||
| 230 | $formErrorMessages = ArrayUtils::merge($formErrorMessages, $instanceForm->getMessages()); |
||
| 231 | if ($valid) { |
||
| 232 | /* |
||
| 233 | * @todo This is a workaround for GeoJSON data insertion |
||
| 234 | * until we figured out, what we really want it to be. |
||
| 235 | */ |
||
| 236 | // if ('general.locationForm' == $formIdentifier) { |
||
| 237 | // $locElem = $instanceForm->getBaseFieldset()->get('geo-location'); |
||
| 238 | // if ($locElem instanceof \Geo\Form\GeoText) { |
||
| 239 | // $loc = $locElem->getValue('entity'); |
||
| 240 | // $locations = $jobEntity->getLocations(); |
||
| 241 | // if (count($locations)) { |
||
| 242 | // $locations->clear(); |
||
| 243 | // } |
||
| 244 | // $locations->add($loc); |
||
| 245 | // $jobEntity->setLocation($locElem->getValue()); |
||
| 246 | // } |
||
| 247 | // } |
||
| 248 | |||
| 249 | $title = $jobEntity->getTitle(); |
||
| 250 | $templateTitle = $jobEntity->getTemplateValues()->getTitle(); |
||
| 251 | |||
| 252 | if (empty($templateTitle)) { |
||
| 253 | $jobEntity->getTemplateValues()->setTitle($title); |
||
| 254 | } |
||
| 255 | $this->repositoryService->store($jobEntity); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // validation |
||
| 260 | $jobValid = true; |
||
| 261 | $errorMessage = array(); |
||
| 262 | if (empty($jobEntity->getTitle())) { |
||
| 263 | $jobValid = false; |
||
| 264 | $errorMessage[] = $this->translator->translate('No Title'); |
||
| 265 | } |
||
| 266 | if (!$jobEntity->getLocations()->count()) { |
||
| 267 | $jobValid = false; |
||
| 268 | $errorMessage[] = $this->translator->translate('No Location'); |
||
| 269 | } |
||
| 270 | if (empty($jobEntity->getTermsAccepted())) { |
||
| 271 | $jobValid = false; |
||
| 272 | $errorMessage[] = $this->translator->translate('Accept the Terms'); |
||
| 273 | } |
||
| 274 | $result = $formEvents->trigger('ValidateJob', $this, [ 'form' => $form ]); |
||
| 275 | foreach ($result as $messages) { |
||
| 276 | if (!$messages) { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | if (!is_array($messages)) { |
||
| 280 | $messages = [ $messages ]; |
||
| 281 | } |
||
| 282 | |||
| 283 | $errorMessage = array_merge($errorMessage, $messages); |
||
| 284 | $jobValid = false; |
||
| 285 | } |
||
| 286 | |||
| 287 | $errorMessage = '<br />' . implode('<br />', $errorMessage); |
||
| 288 | if ($isAjax) { |
||
| 289 | if ($instanceForm instanceof SummaryForm) { |
||
| 290 | $instanceForm->setRenderMode(SummaryForm::RENDER_SUMMARY); |
||
| 291 | $viewHelper = 'summaryform'; |
||
| 292 | } else { |
||
| 293 | $viewHelper = 'form'; |
||
| 294 | } |
||
| 295 | $viewHelperManager = $serviceLocator->get('ViewHelperManager'); |
||
| 296 | $content = $viewHelperManager->get($viewHelper)->__invoke($instanceForm); |
||
| 297 | $viewModel = new JsonModel( |
||
| 298 | array( |
||
| 299 | 'content' => $content, |
||
| 300 | 'valid' => $valid, |
||
| 301 | 'jobvalid' => $jobValid, |
||
| 302 | 'errors' => $formErrorMessages, |
||
| 303 | 'errorMessage' => $errorMessage) |
||
| 304 | ); |
||
| 305 | } else { |
||
| 306 | if ($jobEntity->isDraft()) { |
||
| 307 | $form->getForm('general.nameForm')->setDisplayMode(SummaryFormInterface::DISPLAY_FORM); |
||
| 308 | $form->getForm('general.portalForm')->setDisplayMode(SummaryFormInterface::DISPLAY_FORM); |
||
| 309 | $locElem = $form->getForm('general.locationForm')->setDisplayMode(SummaryFormInterface::DISPLAY_FORM) |
||
| 310 | ->getBaseFieldset()->get('geoLocation'); |
||
| 311 | if ($locElem instanceof \Geo\Form\GeoText) { |
||
| 312 | $loc = $jobEntity->getLocations(); |
||
| 313 | if (count($loc)) { |
||
| 314 | $locElem->setValue($loc->first()); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | $formEvents->trigger('DisableElements', $this, [ 'form' => $form, 'job'=>$jobEntity ]); |
||
| 319 | // Job is deployed, some changes are now disabled |
||
| 320 | $form->enableAll(); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | $completionLink = $this->url()->fromRoute( |
||
| 325 | 'lang/jobs/completion', |
||
| 326 | [ 'id' => $jobEntity->getId()] |
||
| 327 | ); |
||
| 328 | |||
| 329 | $viewModel = $this->getViewModel($form); |
||
| 330 | |||
| 331 | $viewModel->setVariables( |
||
| 332 | array( |
||
| 333 | 'completionLink' => $completionLink, |
||
| 334 | 'title' => $jobEntity->getTitle(), |
||
| 335 | 'job' => $jobEntity, |
||
| 336 | 'summary' => 'this is what we charge you for your offer...', |
||
| 337 | 'valid' => $valid, |
||
| 338 | 'jobvalid' => $jobValid, |
||
| 339 | 'errorMessage' => $errorMessage, |
||
| 340 | 'isDraft' => $jobEntity->isDraft() |
||
| 341 | ) |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | return $viewModel; |
||
| 345 | } |
||
| 346 | |||
| 347 | protected function changeStatus(JobInterface $job, $status) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function checkApplyIdAction() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param $job |
||
| 384 | * @return mixed |
||
| 385 | */ |
||
| 386 | protected function getFormular($job) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param $form |
||
| 408 | * @param array $params |
||
| 409 | * @return ViewModel |
||
| 410 | */ |
||
| 411 | protected function getViewModel($form, array $params = array()) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Job opening is completed. |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function completionAction() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * all actions around approve or decline jobs-offers |
||
| 468 | * |
||
| 469 | * @return array with the viewVariables |
||
| 470 | */ |
||
| 471 | public function approvalAction() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Deactivate a job posting |
||
| 540 | * |
||
| 541 | * @return null|ViewModel |
||
| 542 | */ |
||
| 543 | public function deactivateAction() |
||
| 557 | |||
| 558 | public function deleteAction() |
||
| 559 | { |
||
| 569 | /** |
||
| 570 | * Assign a template to a job posting |
||
| 571 | * |
||
| 572 | * @return JsonModel |
||
| 573 | */ |
||
| 574 | public function templateAction() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param $script |
||
| 590 | * @param array $parameter |
||
| 591 | * @return ViewModel |
||
| 592 | */ |
||
| 593 | View Code Duplication | protected function getErrorViewModel($script, $parameter = array()) |
|
| 604 | |||
| 605 | public function historyAction() |
||
| 615 | } |
||
| 616 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: