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() |
||
| 24 | { |
||
| 25 | $redirectFb = $this->checkFbRegistration($this->user, $this->game); |
||
| 26 | if ($redirectFb) { |
||
| 27 | return $redirectFb; |
||
| 28 | } |
||
| 29 | |||
| 30 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 31 | |||
| 32 | if (!$entry) { |
||
| 33 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 34 | if ($lastEntry === null) { |
||
| 35 | return $this->redirect()->toUrl( |
||
| 36 | $this->frontendUrl()->fromRoute( |
||
|
|
|||
| 37 | 'postvote', |
||
| 38 | array('id' => $this->game->getIdentifier()) |
||
| 39 | ) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | $lastEntryId = $lastEntry->getId(); |
||
| 44 | $lastPost = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId)); |
||
| 45 | $postId = $lastPost->getId(); |
||
| 46 | if ($lastPost->getStatus() == 2) { |
||
| 47 | // the user has already taken part to this game and the participation limit has been reached |
||
| 48 | $this->flashMessenger()->addMessage( |
||
| 49 | $this->getServiceLocator()->get('translator')->translate('You have already a Post') |
||
| 50 | ); |
||
| 51 | |||
| 52 | return $this->redirect()->toUrl( |
||
| 53 | $this->frontendUrl()->fromRoute( |
||
| 54 | 'postvote/post', |
||
| 55 | array( |
||
| 56 | 'id' => $this->game->getIdentifier(), |
||
| 57 | 'post' => $postId, |
||
| 58 | ) |
||
| 59 | ) |
||
| 60 | ); |
||
| 61 | } else { |
||
| 62 | $this->flashMessenger()->addMessage( |
||
| 63 | $this->getServiceLocator()->get('translator')->translate('Your Post is waiting for validation') |
||
| 64 | ); |
||
| 65 | |||
| 66 | return $this->redirect()->toUrl( |
||
| 67 | $this->frontendUrl()->fromRoute( |
||
| 68 | 'postvote/post', |
||
| 69 | array( |
||
| 70 | 'id' => $this->game->getIdentifier(), |
||
| 71 | 'post' => $postId, |
||
| 72 | |||
| 73 | ) |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | if (! $this->game->getForm()) { |
||
| 80 | return $this->redirect()->toUrl( |
||
| 81 | $this->frontendUrl()->fromRoute( |
||
| 82 | 'postvote', |
||
| 83 | array('id' => $this->game->getIdentifier()) |
||
| 84 | ) |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | $form = $this->getGameService()->createFormFromJson($this->game->getForm()->getForm(), 'postvoteForm'); |
||
| 89 | |||
| 90 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 91 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
| 92 | if ($post) { |
||
| 93 | foreach ($post->getPostElements() as $element) { |
||
| 94 | try { |
||
| 95 | $form->get($element->getName())->setValue($element->getValue()); |
||
| 96 | |||
| 97 | $elementType = $form->get($element->getName())->getAttribute('type'); |
||
| 98 | if ($elementType == 'file' && $element->getValue() != '') { |
||
| 99 | $filter = $form->getInputFilter(); |
||
| 100 | $elementInput = $filter->get($element->getName()); |
||
| 101 | $elementInput->setRequired(false); |
||
| 102 | $form->get($element->getName())->setAttribute('required', false); |
||
| 103 | } |
||
| 104 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $viewModel = $this->buildView($this->game); |
||
| 110 | |||
| 111 | if ($this->getRequest()->isPost()) { |
||
| 112 | // POST Request: Process form |
||
| 113 | $data = array_merge_recursive( |
||
| 114 | $this->getRequest()->getPost()->toArray(), |
||
| 115 | $this->getRequest()->getFiles()->toArray() |
||
| 116 | ); |
||
| 117 | |||
| 118 | $form->setData($data); |
||
| 119 | |||
| 120 | if ($form->isValid()) { |
||
| 121 | $data = $form->getData(); |
||
| 122 | $post = $this->getGameService()->createPost($data, $this->game, $this->user, $form); |
||
| 123 | |||
| 124 | View Code Duplication | if ($post && !empty($this->game->nextStep('play'))) { |
|
| 125 | // determine the route where the user should go |
||
| 126 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 127 | 'postvote/'.$this->game->nextStep('play'), |
||
| 128 | array('id' => $this->game->getIdentifier()) |
||
| 129 | ); |
||
| 130 | |||
| 131 | return $this->redirect()->toUrl($redirectUrl); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $messages = $form->getMessages(); |
||
| 135 | $viewModel = $this->buildView($this->game); |
||
| 136 | $viewModel->setVariables(array( |
||
| 137 | 'success' => false, |
||
| 138 | 'message' => implode(',', $messages['title']), |
||
| 139 | )); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $viewModel->setVariables(array( |
||
| 144 | 'playerData' => $entry->getPlayerData(), |
||
| 145 | 'form' => $form, |
||
| 146 | 'post' => $post, |
||
| 147 | )); |
||
| 148 | |||
| 149 | return $viewModel; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function previewAction() |
||
| 153 | { |
||
| 154 | $entry = $this->getGameService()->findLastActiveEntry($this->game, $this->user); |
||
| 155 | |||
| 156 | View Code Duplication | if (!$entry) { |
|
| 157 | // the user has already taken part of this game and the participation limit has been reached |
||
| 158 | return $this->redirect()->toUrl( |
||
| 159 | $this->frontendUrl()->fromRoute( |
||
| 160 | 'postvote/'.$this->game->nextStep('preview'), |
||
| 161 | array('id' => $this->game->getIdentifier()) |
||
| 162 | ) |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 167 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
| 168 | |||
| 169 | View Code Duplication | if (! $post) { |
|
| 170 | return $this->redirect()->toUrl( |
||
| 171 | $this->frontendUrl()->fromRoute( |
||
| 172 | 'postvote', |
||
| 173 | array('id' => $this->game->getIdentifier()) |
||
| 174 | ) |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($this->getRequest()->isPost()) { |
||
| 179 | $post = $this->getGameService()->confirmPost($this->game, $this->user); |
||
| 180 | |||
| 181 | if ($post) { |
||
| 182 | if (!($step = $this->game->nextStep('play'))) { |
||
| 183 | $step = 'result'; |
||
| 184 | } |
||
| 185 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 186 | 'postvote/'.$step, |
||
| 187 | array('id' => $this->game->getIdentifier()) |
||
| 188 | ); |
||
| 189 | |||
| 190 | return $this->redirect()->toUrl($redirectUrl); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | $viewModel = $this->buildView($this->game); |
||
| 195 | $viewModel->setVariables(array('post' => $post)); |
||
| 196 | |||
| 197 | return $viewModel; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * View the Post page |
||
| 202 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
| 203 | */ |
||
| 204 | public function postAction() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * |
||
| 321 | */ |
||
| 322 | public function resultAction() |
||
| 323 | { |
||
| 324 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 325 | if ($lastEntry == null) { |
||
| 326 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier))); |
||
| 327 | } |
||
| 328 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 329 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
| 330 | |||
| 331 | if (! $post) { |
||
| 332 | |||
| 333 | return $this->redirect()->toUrl( |
||
| 334 | $this->frontendUrl()->fromRoute( |
||
| 335 | 'postvote', |
||
| 336 | array('id' => $this->game->getIdentifier()) |
||
| 337 | ) |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | $view = $this->forward()->dispatch( |
||
| 342 | 'playgroundgame_'.$this->game->getClassType(), |
||
| 343 | array( |
||
| 344 | 'controller' => 'playgroundgame_'.$this->game->getClassType(), |
||
| 345 | 'action' => 'share', |
||
| 346 | 'id' => $this->game->getIdentifier() |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | |||
| 350 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
| 351 | $view->setVariables(array('post' => $post)); |
||
| 352 | |||
| 353 | return $view; |
||
| 354 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
| 355 | return $view; |
||
| 356 | } else { |
||
| 357 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 358 | $form->setAttribute('method', 'post'); |
||
| 359 | |||
| 360 | $viewModel = $this->buildView($this->game); |
||
| 361 | |||
| 362 | $viewModel->setVariables(array( |
||
| 363 | 'statusMail' => null, |
||
| 364 | 'post' => $post, |
||
| 365 | 'form' => $form, |
||
| 366 | )); |
||
| 367 | |||
| 368 | return $viewModel; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
| 374 | * It's now possible to send a base64 image in this case the call is the form : |
||
| 375 | * this._ajax( |
||
| 376 | * { |
||
| 377 | * url: url.dataset.url, |
||
| 378 | * method: 'post', |
||
| 379 | * body: 'photo=' + image |
||
| 380 | * }, |
||
| 381 | * |
||
| 382 | * @return \Zend\Stdlib\ResponseInterface |
||
| 383 | */ |
||
| 384 | public function ajaxuploadAction() |
||
| 385 | { |
||
| 386 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
| 387 | session_write_close(); |
||
| 388 | |||
| 389 | if (! $this->game) { |
||
| 390 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 391 | 'success' => 0 |
||
| 392 | ))); |
||
| 393 | |||
| 394 | return $this->getResponse(); |
||
| 395 | } |
||
| 396 | |||
| 397 | $entry = $this->getGameService()->findLastActiveEntry( |
||
| 398 | $this->game, |
||
| 399 | $this->user |
||
| 400 | ); |
||
| 401 | if (!$entry) { |
||
| 402 | // the user has already taken part of this game and the participation limit has been reached |
||
| 403 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 404 | 'success' => 0 |
||
| 405 | ))); |
||
| 406 | |||
| 407 | return $this->getResponse(); |
||
| 408 | } |
||
| 409 | |||
| 410 | if ($this->getRequest()->isPost()) { |
||
| 411 | $data = $this->getRequest()->getFiles()->toArray(); |
||
| 412 | |||
| 413 | if (empty($data)) { |
||
| 414 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 415 | $key = key($data); |
||
| 416 | $uploadImage = array('name' => $key.'.png', 'error' => 0, 'base64' => $data[$key]); |
||
| 417 | $data = array($key => $uploadImage); |
||
| 418 | } |
||
| 419 | $uploadFile = $this->getGameService()->uploadFileToPost( |
||
| 420 | $data, |
||
| 421 | $this->game, |
||
| 422 | $this->user |
||
| 423 | ); |
||
| 424 | } |
||
| 425 | |||
| 426 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 427 | 'success' => true, |
||
| 428 | 'fileUrl' => $uploadFile |
||
| 429 | ))); |
||
| 430 | |||
| 431 | return $this->getResponse(); |
||
| 432 | } |
||
| 433 | |||
| 434 | public function ajaxdeleteAction() |
||
| 466 | |||
| 467 | public function ajaxrejectPostAction() |
||
| 468 | { |
||
| 469 | $response = $this->getResponse(); |
||
| 470 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
| 503 | |||
| 504 | public function listAction() |
||
| 618 | |||
| 619 | public function ajaxVoteAction() |
||
| 660 | |||
| 661 | public function commentsAction() |
||
| 685 | |||
| 686 | public function commentAction() |
||
| 737 | |||
| 738 | public function ajaxRemoveCommentAction() |
||
| 778 | |||
| 779 | public function captchaAction() |
||
| 803 | |||
| 804 | public function sharePostAction() |
||
| 859 | |||
| 860 | public function shareCommentAction() |
||
| 902 | |||
| 903 | public function shareAction() |
||
| 976 | |||
| 977 | public function getGameService() |
||
| 985 | } |
||
| 986 |
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: