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 PostVoteController 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 PostVoteController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class PostVoteController extends GameController |
||
8 | { |
||
9 | /** |
||
10 | * @var gameService |
||
11 | */ |
||
12 | protected $gameService; |
||
13 | |||
14 | /** |
||
15 | * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu) |
||
16 | * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller) |
||
17 | * --DONE-- 3. don't answer to questions (form is checked controller side) |
||
18 | * 4. try to game the chrono |
||
19 | * 5. try to play again |
||
20 | * 6. try to change answers |
||
21 | * --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu) |
||
22 | */ |
||
23 | public function playAction() |
||
161 | |||
162 | public function previewAction() |
||
209 | |||
210 | /** |
||
211 | * View the Post page |
||
212 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
213 | */ |
||
214 | public function postAction() |
||
327 | |||
328 | /** |
||
329 | * |
||
330 | */ |
||
331 | public function resultAction() |
||
332 | { |
||
333 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
334 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
335 | |||
336 | if (! $post) { |
||
337 | return $this->redirect()->toUrl( |
||
338 | $this->frontendUrl()->fromRoute( |
||
339 | 'postvote', |
||
340 | array('id' => $this->game->getIdentifier()) |
||
341 | ) |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | $view = $this->forward()->dispatch( |
||
346 | 'playgroundgame_'.$this->game->getClassType(), |
||
347 | array( |
||
348 | 'controller' => 'playgroundgame_'.$this->game->getClassType(), |
||
349 | 'action' => 'share', |
||
350 | 'id' => $this->game->getIdentifier() |
||
351 | ) |
||
352 | ); |
||
353 | |||
354 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
355 | $view->setVariables(array('post' => $post)); |
||
356 | |||
357 | return $view; |
||
358 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
359 | return $view; |
||
360 | } else { |
||
361 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
362 | $form->setAttribute('method', 'post'); |
||
363 | |||
364 | $viewModel = $this->buildView($this->game); |
||
365 | |||
366 | $viewModel->setVariables(array( |
||
367 | 'statusMail' => null, |
||
368 | 'post' => $post, |
||
369 | 'form' => $form, |
||
370 | )); |
||
371 | |||
372 | return $viewModel; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
378 | * It's now possible to send a base64 image in this case the call is the form : |
||
379 | * this._ajax( |
||
380 | * { |
||
381 | * url: url.dataset.url, |
||
382 | * method: 'post', |
||
383 | * body: 'photo=' + image |
||
384 | * }, |
||
385 | * |
||
386 | * @return \Zend\Stdlib\ResponseInterface |
||
387 | */ |
||
388 | public function ajaxuploadAction() |
||
389 | { |
||
390 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
391 | session_write_close(); |
||
392 | |||
393 | if (! $this->game) { |
||
394 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
395 | 'success' => 0 |
||
396 | ))); |
||
397 | |||
398 | return $this->getResponse(); |
||
399 | } |
||
400 | |||
401 | $entry = $this->getGameService()->findLastActiveEntry( |
||
402 | $this->game, |
||
403 | $this->user |
||
404 | ); |
||
405 | if (!$entry) { |
||
406 | // the user has already taken part of this game and the participation limit has been reached |
||
407 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
408 | 'success' => 0 |
||
409 | ))); |
||
410 | |||
411 | return $this->getResponse(); |
||
412 | } |
||
413 | |||
414 | if ($this->getRequest()->isPost()) { |
||
415 | $data = $this->getRequest()->getFiles()->toArray(); |
||
416 | |||
417 | if (empty($data)) { |
||
418 | $data = $this->getRequest()->getPost()->toArray(); |
||
419 | $key = key($data); |
||
420 | $uploadImage = array('name' => $key.'.png', 'error' => 0, 'base64' => $data[$key]); |
||
421 | $data = array($key => $uploadImage); |
||
422 | } |
||
423 | $uploadFile = $this->getGameService()->uploadFileToPost( |
||
424 | $data, |
||
425 | $this->game, |
||
426 | $this->user |
||
427 | ); |
||
428 | } |
||
429 | |||
430 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
431 | 'success' => true, |
||
432 | 'fileUrl' => $uploadFile |
||
433 | ))); |
||
434 | |||
435 | return $this->getResponse(); |
||
436 | } |
||
437 | |||
438 | public function ajaxdeleteAction() |
||
470 | |||
471 | public function listAction() |
||
585 | |||
586 | public function ajaxVoteAction() |
||
587 | { |
||
588 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
589 | session_write_close(); |
||
590 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
591 | $request = $this->getRequest(); |
||
592 | $response = $this->getResponse(); |
||
593 | |||
594 | if (! $this->game) { |
||
595 | $response->setContent(\Zend\Json\Json::encode(array( |
||
596 | 'success' => 0 |
||
597 | ))); |
||
598 | |||
599 | return $response; |
||
600 | } |
||
601 | |||
602 | if (!$this->zfcUserAuthentication()->hasIdentity()) { |
||
603 | $response->setContent(\Zend\Json\Json::encode(array( |
||
604 | 'success' => 0 |
||
605 | ))); |
||
606 | } else { |
||
607 | if ($request->isPost()) { |
||
608 | $post = $this->getGameService()->getPostvotePostMapper()->findById($postId); |
||
609 | if ($this->getGameService()->addVote( |
||
610 | $this->user, |
||
611 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
612 | $post |
||
613 | )) { |
||
614 | $response->setContent(\Zend\Json\Json::encode(array( |
||
615 | 'success' => 1 |
||
616 | ))); |
||
617 | } else { |
||
618 | $response->setContent(\Zend\Json\Json::encode(array( |
||
619 | 'success' => 0 |
||
620 | ))); |
||
621 | } |
||
622 | } |
||
623 | } |
||
624 | |||
625 | return $response; |
||
626 | } |
||
627 | |||
628 | public function captchaAction() |
||
652 | |||
653 | public function shareAction() |
||
654 | { |
||
655 | $statusMail = null; |
||
656 | |||
657 | // Has the user finished the game ? |
||
658 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
659 | |||
660 | if ($lastEntry === null) { |
||
661 | return $this->redirect()->toUrl( |
||
662 | $this->frontendUrl()->fromRoute( |
||
663 | 'postvote', |
||
664 | array('id' => $this->game->getIdentifier()) |
||
665 | ) |
||
666 | ); |
||
667 | } |
||
668 | |||
669 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
670 | |||
671 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
672 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
673 | 'postvote/post', |
||
674 | array( |
||
675 | 'id' => $this->game->getIdentifier(), |
||
676 | 'post' => $post->getId(), |
||
677 | ), |
||
678 | array('force_canonical' => true) |
||
679 | ).'?key='.$secretKey; |
||
680 | // With core shortener helper |
||
681 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
682 | |||
683 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
684 | $form->setAttribute('method', 'post'); |
||
685 | |||
686 | if ($this->getRequest()->isPost()) { |
||
687 | $data = $this->getRequest()->getPost()->toArray(); |
||
688 | $form->setData($data); |
||
689 | View Code Duplication | if ($form->isValid()) { |
|
690 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
691 | if ($result) { |
||
692 | $statusMail = true; |
||
693 | } |
||
694 | } |
||
695 | } |
||
696 | |||
697 | $viewModel = $this->buildView($this->game); |
||
698 | |||
699 | View Code Duplication | foreach ($post->getPostElements() as $element) { |
|
700 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
701 | '', |
||
702 | array(), |
||
703 | array('force_canonical' => true), |
||
704 | false |
||
705 | ) . $element->getValue(); |
||
706 | break; |
||
707 | } |
||
708 | |||
709 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
710 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
711 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@playground"); |
||
712 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $this->game->getTwShareMessage()); |
||
713 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
714 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
715 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
716 | |||
717 | $viewModel->setVariables(array( |
||
718 | 'statusMail' => $statusMail, |
||
719 | 'form' => $form, |
||
720 | 'socialLinkUrl' => $socialLinkUrl, |
||
721 | 'post' => $post |
||
722 | )); |
||
723 | |||
724 | return $viewModel; |
||
725 | } |
||
726 | |||
727 | public function getGameService() |
||
735 | } |
||
736 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: