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 InstantWinController 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 | |||
| 31 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 32 | $redirect = urlencode( |
||
| 33 | $this->frontendUrl()->fromRoute( |
||
| 34 | $game->getClassType() . '/play', |
||
| 35 | array( |
||
| 36 | 'id' => $game->getIdentifier(), |
||
| 37 | 'channel' => $channel |
||
| 38 | ), |
||
| 39 | array('force_canonical' => true) |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | return $this->redirect()->toUrl( |
||
| 44 | $this->frontendUrl()->fromRoute( |
||
| 45 | 'zfcuser/register', |
||
| 46 | array('channel' => $channel) |
||
| 47 | ) . '?redirect='.$redirect |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($game->getOccurrenceType()=='datetime') { |
||
| 52 | |||
| 53 | $entry = $sg->play($game, $user); |
||
| 54 | View Code Duplication | if (!$entry) { |
|
| 55 | // the user has already taken part of this game and the participation limit has been reached |
||
| 56 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 57 | |||
| 58 | return $this->redirect()->toUrl( |
||
| 59 | $this->frontendUrl()->fromRoute( |
||
| 60 | 'instantwin/result', |
||
| 61 | array( |
||
| 62 | 'id' => $game->getIdentifier(), |
||
| 63 | 'channel' => $channel |
||
| 64 | ) |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | // update the winner attribute in entry. |
||
| 70 | $winner = $sg->IsInstantWinner($game, $user); |
||
| 71 | |||
| 72 | $prize = null; |
||
| 73 | if ($winner) { |
||
| 74 | $prize = $winner->getPrize(); |
||
| 75 | } |
||
| 76 | $viewVariables = array( |
||
| 77 | 'winner' => $winner, |
||
| 78 | 'prize' => $prize, |
||
| 79 | 'over' => false, |
||
| 80 | ); |
||
| 81 | } elseif ($game->getOccurrenceType()=='code') { |
||
| 82 | $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
||
| 83 | $form->setAttribute( |
||
| 84 | 'action', |
||
| 85 | $this->frontendUrl()->fromRoute( |
||
| 86 | 'instantwin/play', |
||
| 87 | array( |
||
| 88 | 'id' => $game->getIdentifier(), |
||
| 89 | 'channel' => $channel |
||
| 90 | ), |
||
| 91 | array('force_canonical' => true) |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | |||
| 95 | $occurrence = null; |
||
| 96 | if ($this->getRequest()->isPost()) { |
||
| 97 | $form->setData($this->getRequest()->getPost()); |
||
| 98 | if ($form->isValid()) { |
||
| 99 | $data = $form->getData('code-input'); |
||
| 100 | $code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
||
| 101 | $occurrence = $this->getGameService()->isInstantWinner($game, $user, $code); |
||
| 102 | if (!$occurrence) { |
||
| 103 | $this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
||
| 104 | return $this->redirect()->toUrl( |
||
| 105 | $this->frontendUrl()->fromRoute( |
||
| 106 | 'instantwin/play', |
||
| 107 | array( |
||
| 108 | 'id' => $game->getIdentifier(), |
||
| 109 | 'channel' => $channel |
||
| 110 | ), |
||
| 111 | array('force_canonical' => true) |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | View Code Duplication | } else { |
|
| 115 | return $this->redirect()->toUrl( |
||
| 116 | $this->frontendUrl()->fromRoute( |
||
| 117 | 'instantwin/result', |
||
| 118 | array( |
||
| 119 | 'id' => $game->getIdentifier(), |
||
| 120 | 'channel' => $channel |
||
| 121 | ) |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | $viewVariables = array('form' => $form); |
||
| 128 | } |
||
| 129 | |||
| 130 | $viewModel = $this->buildView($game); |
||
| 131 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
| 132 | $viewModel->setVariables($viewVariables); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $viewModel; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function resultAction() |
||
| 139 | { |
||
| 140 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 141 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
| 142 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 143 | $sg = $this->getGameService(); |
||
| 144 | |||
| 145 | $game = $sg->checkGame($identifier); |
||
| 146 | if (!$game || $game->isClosed()) { |
||
| 147 | return $this->notFoundAction(); |
||
| 148 | } |
||
| 149 | |||
| 150 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
| 151 | View Code Duplication | if (!$lastEntry) { |
|
| 152 | return $this->redirect()->toUrl( |
||
| 153 | $this->frontendUrl()->fromRoute( |
||
| 154 | 'instantwin', |
||
| 155 | array('id' => $game->getIdentifier(), 'channel' => $channel), |
||
| 156 | array('force_canonical' => true) |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | $winner = $lastEntry->getWinner(); |
||
| 161 | $occurrence = null; |
||
| 162 | |||
| 163 | // On tente de récupèrer l'occurrence si elle existe pour avoir accés au lot associé |
||
| 164 | $occurrences = $sg->getInstantWinOccurrenceMapper()->findBy( |
||
| 165 | array('instantwin' => $game->getId(), 'entry' => $lastEntry->getId()) |
||
| 166 | ); |
||
| 167 | if (!empty($occurrences)) { |
||
| 168 | $occurrence = current($occurrences); |
||
| 169 | } |
||
| 170 | |||
| 171 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 172 | $redirect = urlencode( |
||
| 173 | $this->frontendUrl()->fromRoute( |
||
| 174 | 'instantwin/result', |
||
| 175 | array('id' => $game->getIdentifier(), 'channel' => $channel) |
||
| 176 | ) |
||
| 177 | ); |
||
| 178 | return $this->redirect()->toUrl( |
||
| 179 | $this->frontendUrl()->fromRoute( |
||
| 180 | 'zfcuser/register', |
||
| 181 | array('channel' => $channel) |
||
| 182 | ) . '?redirect='.$redirect |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 187 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 188 | 'instantwin', |
||
| 189 | array('id' => $game->getIdentifier(), 'channel' => $channel), |
||
| 190 | array('force_canonical' => true) |
||
| 191 | ).'?key='.$secretKey; |
||
| 192 | // With core shortener helper |
||
| 193 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 194 | |||
| 195 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 196 | $form->setAttribute('method', 'post'); |
||
| 197 | |||
| 198 | $statusMail = null; |
||
| 199 | if ($this->getRequest()->isPost()) { |
||
| 200 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 201 | $form->setData($data); |
||
| 202 | if ($form->isValid()) { |
||
| 203 | if (isset($data['email1']) || isset($data['email2']) || isset($data['email3'])) { |
||
| 204 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
| 205 | if ($result) { |
||
| 206 | $statusMail = true; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | $prize = null; |
||
| 213 | if ($occurrence instanceof \PlaygroundGame\Entity\InstantWinOccurrence) { |
||
| 214 | $prize = $occurrence->getPrize(); |
||
| 215 | } |
||
| 216 | |||
| 217 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
| 218 | $viewModel = $this->buildView($game); |
||
| 219 | |||
| 220 | $this->getGameService()->sendMail($game, $user, $lastEntry, $prize); |
||
| 221 | |||
| 222 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
| 223 | $viewModel->setVariables(array( |
||
| 224 | 'occurrence' => $occurrence, |
||
| 225 | 'statusMail' => $statusMail, |
||
| 226 | 'winner' => $winner, |
||
| 227 | 'form' => $form, |
||
| 228 | 'socialLinkUrl' => $socialLinkUrl, |
||
| 229 | 'secretKey' => $secretKey, |
||
| 230 | )); |
||
| 231 | } |
||
| 232 | return $viewModel; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getGameService() |
||
| 243 | } |
||
| 244 |
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: