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 |
||
| 5 | class LotteryController extends GameController |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var gameService |
||
| 9 | */ |
||
| 10 | protected $gameService; |
||
| 11 | |||
| 12 | public function playAction() |
||
| 13 | { |
||
| 14 | $sg = $this->getGameService(); |
||
| 15 | |||
| 16 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 17 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
| 18 | |||
| 19 | $game = $sg->checkGame($identifier); |
||
| 20 | if (! $game || $game->isClosed()) { |
||
| 21 | return $this->notFoundAction(); |
||
| 22 | } |
||
| 23 | |||
| 24 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game, $channel); |
||
|
|
|||
| 25 | if ($redirectFb) { |
||
| 26 | return $redirectFb; |
||
| 27 | } |
||
| 28 | |||
| 29 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 30 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 31 | $redirect = urlencode( |
||
| 32 | $this->frontendUrl()->fromRoute( |
||
| 33 | $game->getClassType() . '/play', |
||
| 34 | array('id' => $game->getIdentifier(), 'channel' => $channel), |
||
| 35 | array('force_canonical' => true) |
||
| 36 | ) |
||
| 37 | ); |
||
| 38 | |||
| 39 | return $this->redirect()->toUrl( |
||
| 40 | $this->frontendUrl()->fromRoute( |
||
| 41 | 'zfcuser/register', |
||
| 42 | array('channel' => $channel) |
||
| 43 | ) . '?redirect='.$redirect |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | |||
| 47 | $entry = $sg->play($game, $user); |
||
| 48 | if (!$entry) { |
||
| 49 | // the user has already taken part of this game and the participation limit has been reached |
||
| 50 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 51 | |||
| 52 | return $this->redirect()->toUrl( |
||
| 53 | $this->frontendUrl()->fromRoute( |
||
| 54 | 'lottery/result', |
||
| 55 | array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')) |
||
| 56 | ) |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | // Every entry is eligible to draw |
||
| 61 | $entry->setDrawable(true); |
||
| 62 | $entry->setActive(false); |
||
| 63 | $sg->getEntryMapper()->update($entry); |
||
| 64 | |||
| 65 | return $this->redirect()->toUrl( |
||
| 66 | $this->frontendUrl()->fromRoute( |
||
| 67 | $game->getClassType() . '/'. $game->nextStep($this->params('action')), |
||
| 68 | array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')) |
||
| 69 | ) |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function resultAction() |
||
| 74 | { |
||
| 75 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 76 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 77 | $sg = $this->getGameService(); |
||
| 78 | |||
| 79 | $statusMail = null; |
||
| 80 | |||
| 81 | $game = $sg->checkGame($identifier); |
||
| 82 | if (!$game || $game->isClosed()) { |
||
| 83 | return $this->notFoundAction(); |
||
| 84 | } |
||
| 85 | |||
| 86 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 87 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 88 | 'lottery', |
||
| 89 | array( |
||
| 90 | 'id' => $game->getIdentifier(), |
||
| 91 | 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel') |
||
| 92 | ), |
||
| 93 | array('force_canonical' => true) |
||
| 94 | ).'?key='.$secretKey; |
||
| 95 | // With core shortener helper |
||
| 96 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 97 | |||
| 98 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
| 99 | if (!$lastEntry) { |
||
| 100 | return $this->redirect()->toUrl( |
||
| 101 | $this->frontendUrl()->fromRoute( |
||
| 102 | 'lottery', |
||
| 103 | array( |
||
| 104 | 'id' => $game->getIdentifier(), |
||
| 105 | 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel') |
||
| 106 | ), |
||
| 107 | array('force_canonical' => true) |
||
| 108 | ) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 113 | $redirect = urlencode( |
||
| 114 | $this->frontendUrl()->fromRoute( |
||
| 115 | 'lottery/result', |
||
| 116 | array('id' => $game->getIdentifier(), 'channel' => $channel) |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | return $this->redirect()->toUrl( |
||
| 120 | $this->frontendUrl()->fromRoute( |
||
| 121 | 'zfcuser/register', |
||
| 122 | array('channel' => $channel) |
||
| 123 | ) . '?redirect='.$redirect |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 128 | $form->setAttribute('method', 'post'); |
||
| 129 | |||
| 130 | if ($this->getRequest()->isPost()) { |
||
| 131 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 132 | $form->setData($data); |
||
| 133 | if ($form->isValid()) { |
||
| 134 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
| 135 | if ($result) { |
||
| 136 | $statusMail = true; |
||
| 137 | $sg->addAnotherChance($game, $user, 1); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
| 143 | $viewModel = $this->buildView($game); |
||
| 144 | |||
| 145 | $this->getGameService()->sendMail($game, $user, $lastEntry); |
||
| 146 | |||
| 147 | $viewModel->setVariables(array( |
||
| 148 | 'statusMail' => $statusMail, |
||
| 149 | 'form' => $form, |
||
| 150 | 'socialLinkUrl' => $socialLinkUrl, |
||
| 151 | 'secretKey' => $secretKey, |
||
| 152 | )); |
||
| 153 | |||
| 154 | return $viewModel; |
||
| 155 | } |
||
| 156 | |||
| 157 | View Code Duplication | public function fbshareAction() |
|
| 178 | |||
| 179 | View Code Duplication | public function fbrequestAction() |
|
| 200 | |||
| 201 | View Code Duplication | public function tweetAction() |
|
| 222 | |||
| 223 | View Code Duplication | public function googleAction() |
|
| 244 | |||
| 245 | public function getGameService() |
||
| 253 | } |
||
| 254 |
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: