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() |
||
| 151 | |||
| 152 | public function previewAction() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * View the Post page |
||
| 202 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
| 203 | */ |
||
| 204 | public function postAction() |
||
| 205 | { |
||
| 206 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
| 207 | $voted = false; |
||
| 208 | $statusMail = false; |
||
| 209 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
| 210 | $to = ''; |
||
| 211 | $skinUrl = $this->getGameService()->getServiceManager()->get('ViewRenderer')->url( |
||
| 212 | 'frontend', |
||
| 213 | array(), |
||
| 214 | array('force_canonical' => true) |
||
| 215 | ); |
||
| 216 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
| 217 | if (isset($config['moderation']['email'])) { |
||
| 218 | $to = $config['moderation']['email']; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (! $postId) { |
||
| 222 | return $this->notFoundAction(); |
||
| 223 | } |
||
| 224 | |||
| 225 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 226 | $post = $this->getGameService()->getPostVotePostMapper()->findById($postId); |
||
| 227 | |||
| 228 | View Code Duplication | if (! $post || $post->getStatus() === 9) { |
|
| 229 | return $this->redirect()->toUrl( |
||
| 230 | $this->frontendUrl()->fromRoute( |
||
| 231 | 'postvote', |
||
| 232 | array('id' => $this->game->getIdentifier()) |
||
| 233 | ) |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | $formModeration = new Form(); |
||
| 238 | $formModeration->setAttribute('method', 'post'); |
||
| 239 | |||
| 240 | $formModeration->add(array( |
||
| 241 | 'name' => 'moderation', |
||
| 242 | 'attributes' => array( |
||
| 243 | 'type' => 'hidden', |
||
| 244 | 'value' => '1' |
||
| 245 | ), |
||
| 246 | )); |
||
| 247 | |||
| 248 | $form = new \PlaygroundGame\Form\Frontend\PostVoteVote( |
||
| 249 | $this->frontendUrl()->fromRoute( |
||
| 250 | 'postvote/post/captcha', |
||
| 251 | array( |
||
| 252 | 'id' => $this->game->getIdentifier(), |
||
| 253 | 'post' => $postId, |
||
| 254 | ) |
||
| 255 | ) |
||
| 256 | ); |
||
| 257 | |||
| 258 | if ($this->user) { |
||
| 259 | $form->remove('captcha'); |
||
| 260 | if (count($this->getGameService()->getPostvoteVoteMapper()->findBy(array('user' => $this->user, 'post' =>$post))) > 0) { |
||
| 261 | $voted = true; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | $alreadyVoted = ''; |
||
| 266 | $reportId = ''; |
||
| 267 | |||
| 268 | $request = $this->getRequest(); |
||
| 269 | if ($request->isPost()) { |
||
| 270 | $data = $request->getPost()->toArray(); |
||
| 271 | if (isset($data['moderation'])) { |
||
| 272 | $formModeration->setData($data); |
||
| 273 | if ($formModeration->isValid()) { |
||
| 274 | $from = $to; |
||
| 275 | $subject= 'Moderation Post and Vote'; |
||
| 276 | $result = $mailService->createHtmlMessage( |
||
| 277 | $from, |
||
| 278 | $to, |
||
| 279 | $subject, |
||
| 280 | 'playground-game/email/moderation', |
||
| 281 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
| 282 | ); |
||
| 283 | $mailService->send($result); |
||
| 284 | if ($result) { |
||
| 285 | $statusMail = true; |
||
| 286 | $reportId = $data['reportid']; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } else { |
||
| 290 | $form->setData($request->getPost()); |
||
| 291 | if ($form->isValid()) { |
||
| 292 | if ($this->getGameService()->addVote( |
||
| 293 | $this->user, |
||
| 294 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
| 295 | $post |
||
| 296 | )) { |
||
| 297 | $voted = true; |
||
| 298 | } else { |
||
| 299 | $alreadyVoted = 'Vous avez déjà voté!'; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | $viewModel = $this->buildView($this->game); |
||
| 306 | $viewModel->setVariables( |
||
| 307 | array( |
||
| 308 | 'post' => $post, |
||
| 309 | 'voted' => $voted, |
||
| 310 | 'form' => $form, |
||
| 311 | 'formModeration' => $formModeration, |
||
| 312 | 'statusMail' => $statusMail, |
||
| 313 | 'alreadyVoted' => $alreadyVoted, |
||
| 314 | 'reportId' => $reportId, |
||
| 315 | ) |
||
| 316 | ); |
||
| 317 | |||
| 318 | return $viewModel; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | public function resultAction() |
||
| 325 | { |
||
| 326 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 327 | if ($lastEntry == null) { |
||
| 328 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier))); |
||
| 329 | } |
||
| 330 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 331 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
| 332 | |||
| 333 | if (! $post) { |
||
| 334 | return $this->redirect()->toUrl( |
||
| 335 | $this->frontendUrl()->fromRoute( |
||
| 336 | 'postvote', |
||
| 337 | array('id' => $this->game->getIdentifier()) |
||
| 338 | ) |
||
| 339 | ); |
||
| 340 | } |
||
| 341 | |||
| 342 | $view = $this->forward()->dispatch( |
||
| 343 | 'playgroundgame_'.$this->game->getClassType(), |
||
| 344 | array( |
||
| 345 | 'controller' => 'playgroundgame_'.$this->game->getClassType(), |
||
| 346 | 'action' => 'share', |
||
| 347 | 'id' => $this->game->getIdentifier() |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | |||
| 351 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
| 352 | $view->setVariables(array('post' => $post)); |
||
| 353 | |||
| 354 | return $view; |
||
| 355 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
| 356 | return $view; |
||
| 357 | } else { |
||
| 358 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 359 | $form->setAttribute('method', 'post'); |
||
| 360 | |||
| 361 | $viewModel = $this->buildView($this->game); |
||
| 362 | |||
| 363 | $viewModel->setVariables(array( |
||
| 364 | 'statusMail' => null, |
||
| 365 | 'post' => $post, |
||
| 366 | 'form' => $form, |
||
| 367 | )); |
||
| 368 | |||
| 369 | return $viewModel; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
| 375 | * It's now possible to send a base64 image in this case the call is the form : |
||
| 376 | * this._ajax( |
||
| 377 | * { |
||
| 378 | * url: url.dataset.url, |
||
| 379 | * method: 'post', |
||
| 380 | * body: 'photo=' + image |
||
| 381 | * }, |
||
| 382 | * |
||
| 383 | * @return \Zend\Stdlib\ResponseInterface |
||
| 384 | */ |
||
| 385 | public function ajaxuploadAction() |
||
| 386 | { |
||
| 387 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
| 388 | session_write_close(); |
||
| 389 | |||
| 390 | if (! $this->game) { |
||
| 391 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 392 | 'success' => 0 |
||
| 393 | ))); |
||
| 394 | |||
| 395 | return $this->getResponse(); |
||
| 396 | } |
||
| 397 | |||
| 398 | $entry = $this->getGameService()->findLastActiveEntry( |
||
| 399 | $this->game, |
||
| 400 | $this->user |
||
| 401 | ); |
||
| 402 | if (!$entry) { |
||
| 403 | // the user has already taken part of this game and the participation limit has been reached |
||
| 404 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 405 | 'success' => 0 |
||
| 406 | ))); |
||
| 407 | |||
| 408 | return $this->getResponse(); |
||
| 409 | } |
||
| 410 | |||
| 411 | if ($this->getRequest()->isPost()) { |
||
| 412 | $data = $this->getRequest()->getFiles()->toArray(); |
||
| 413 | |||
| 414 | if (empty($data)) { |
||
| 415 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 416 | $key = key($data); |
||
| 417 | $uploadImage = array('name' => $key.'.png', 'error' => 0, 'base64' => $data[$key]); |
||
| 418 | $data = array($key => $uploadImage); |
||
| 419 | } |
||
| 420 | $uploadFile = $this->getGameService()->uploadFileToPost( |
||
| 421 | $data, |
||
| 422 | $this->game, |
||
| 423 | $this->user |
||
| 424 | ); |
||
| 425 | } |
||
| 426 | |||
| 427 | $this->getResponse()->setContent(\Zend\Json\Json::encode(array( |
||
| 428 | 'success' => true, |
||
| 429 | 'fileUrl' => $uploadFile |
||
| 430 | ))); |
||
| 431 | |||
| 432 | return $this->getResponse(); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function ajaxdeleteAction() |
||
| 467 | |||
| 468 | public function ajaxrejectPostAction() |
||
| 504 | |||
| 505 | public function listAction() |
||
| 506 | { |
||
| 507 | $filter = $this->getEvent()->getRouteMatch()->getParam('filter'); |
||
| 508 | $search = $this->params()->fromQuery('name'); |
||
| 509 | $postId = $this->params()->fromQuery('id'); |
||
| 510 | $statusMail = false; |
||
| 511 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
| 512 | $to = ''; |
||
| 513 | $skinUrl = $this->getGameService()->getServiceManager()->get('ViewRenderer')->url( |
||
| 514 | 'frontend', |
||
| 515 | array(), |
||
| 516 | array('force_canonical' => true) |
||
| 517 | ); |
||
| 518 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
| 519 | if (isset($config['moderation']['email'])) { |
||
| 520 | $to = $config['moderation']['email']; |
||
| 521 | } |
||
| 522 | |||
| 523 | $request = $this->getRequest(); |
||
| 524 | |||
| 525 | // Je recherche les posts validés associés à ce jeu |
||
| 526 | $posts = $this->getGameService()->findArrayOfValidatedPosts($this->game, $this->user, $filter, $search); |
||
| 527 | |||
| 528 | if (is_array($posts)) { |
||
| 529 | $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts)); |
||
| 530 | $paginator->setItemCountPerPage($this->game->getPostDisplayNumber()); |
||
| 531 | $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
||
| 532 | } else { |
||
| 533 | $paginator = $posts; |
||
| 534 | } |
||
| 535 | |||
| 536 | $form = new Form(); |
||
| 537 | $form->setAttribute('method', 'post'); |
||
| 538 | |||
| 539 | $form->add(array( |
||
| 540 | 'name' => 'moderation', |
||
| 541 | 'attributes' => array( |
||
| 542 | 'type' => 'hidden', |
||
| 543 | 'value' => '1' |
||
| 544 | ), |
||
| 545 | )); |
||
| 546 | |||
| 547 | $reportId =''; |
||
| 548 | |||
| 549 | if ($request->isPost()) { |
||
| 550 | $data = $request->getPost()->toArray(); |
||
| 551 | |||
| 552 | if (isset($data['moderation'])) { |
||
| 553 | $from = $to; |
||
| 554 | $subject= 'Moderation Post and Vote'; |
||
| 555 | $result = $mailService->createHtmlMessage( |
||
| 556 | $from, |
||
| 557 | $to, |
||
| 558 | $subject, |
||
| 559 | 'playground-game/email/moderation', |
||
| 560 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
| 561 | ); |
||
| 562 | $mailService->send($result); |
||
| 563 | if ($result) { |
||
| 564 | $statusMail = true; |
||
| 565 | $reportId = $data['reportid']; |
||
| 566 | } |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | $viewModel = $this->buildView($this->game); |
||
| 571 | |||
| 572 | if ($postId) { |
||
| 573 | $postTarget = $this->getGameService()->getPostVotePostMapper()->findById($postId); |
||
| 574 | if ($postTarget) { |
||
| 575 | View Code Duplication | foreach ($postTarget->getPostElements() as $element) { |
|
| 576 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
| 577 | '', |
||
| 578 | array(), |
||
| 579 | array('force_canonical' => true), |
||
| 580 | false |
||
| 581 | ) . $element->getValue(); |
||
| 582 | break; |
||
| 583 | } |
||
| 584 | |||
| 585 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 586 | |||
| 587 | // Without bit.ly shortener |
||
| 588 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 589 | 'postvote/list', |
||
| 590 | array( |
||
| 591 | 'id' => $this->game->getIdentifier(), |
||
| 592 | 'filter' => 'date'), |
||
| 593 | array('force_canonical' => true) |
||
| 594 | ).'?id='.$postTarget->getId().'&key='.$secretKey; |
||
| 595 | // With core shortener helper |
||
| 596 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 597 | |||
| 598 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
| 599 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
| 600 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@alfie_selfie"); |
||
| 601 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $this->game->getTwShareMessage()); |
||
| 602 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
| 603 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
| 604 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | $viewModel->setVariables(array( |
||
| 609 | 'posts' => $paginator, |
||
| 610 | 'form' => $form, |
||
| 611 | 'statusMail' => $statusMail, |
||
| 612 | 'reportId' => $reportId, |
||
| 613 | 'filter' => $filter, |
||
| 614 | 'search' => $search, |
||
| 615 | )); |
||
| 616 | |||
| 617 | return $viewModel; |
||
| 618 | } |
||
| 619 | |||
| 620 | public function ajaxVoteAction() |
||
| 621 | { |
||
| 622 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
| 623 | session_write_close(); |
||
| 624 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
| 625 | $request = $this->getRequest(); |
||
| 626 | $response = $this->getResponse(); |
||
| 627 | |||
| 628 | if (! $this->game) { |
||
| 629 | $response->setContent(\Zend\Json\Json::encode(array( |
||
| 630 | 'success' => 0 |
||
| 631 | ))); |
||
| 632 | |||
| 633 | return $response; |
||
| 634 | } |
||
| 635 | |||
| 636 | View Code Duplication | if (!$this->zfcUserAuthentication()->hasIdentity()) { |
|
| 637 | $response->setContent(\Zend\Json\Json::encode(array( |
||
| 638 | 'success' => 0 |
||
| 639 | ))); |
||
| 640 | } else { |
||
| 641 | if ($request->isPost()) { |
||
| 642 | $post = $this->getGameService()->getPostvotePostMapper()->findById($postId); |
||
| 643 | if ($this->getGameService()->addVote( |
||
| 644 | $this->user, |
||
| 645 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
| 646 | $post |
||
| 647 | )) { |
||
| 648 | $response->setContent(\Zend\Json\Json::encode(array( |
||
| 649 | 'success' => 1 |
||
| 650 | ))); |
||
| 651 | } else { |
||
| 652 | $response->setContent(\Zend\Json\Json::encode(array( |
||
| 653 | 'success' => 0 |
||
| 654 | ))); |
||
| 655 | } |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | return $response; |
||
| 660 | } |
||
| 661 | |||
| 662 | public function commentsAction() |
||
| 685 | |||
| 686 | public function commentAction() |
||
| 736 | |||
| 737 | public function ajaxRemoveCommentAction() |
||
| 777 | |||
| 778 | public function captchaAction() |
||
| 779 | { |
||
| 780 | $response = $this->getResponse(); |
||
| 781 | $response->getHeaders()->addHeaderLine('Content-Type', "image/png"); |
||
| 782 | |||
| 783 | $id = $this->params('id', false); |
||
| 784 | |||
| 785 | if ($id) { |
||
| 786 | $image = './data/captcha/' . $id; |
||
| 787 | |||
| 788 | if (file_exists($image) !== false) { |
||
| 789 | $imagegetcontent = file_get_contents($image); |
||
| 790 | |||
| 791 | $response->setStatusCode(200); |
||
| 792 | $response->setContent($imagegetcontent); |
||
| 793 | |||
| 794 | if (file_exists($image) === true) { |
||
| 795 | unlink($image); |
||
| 796 | } |
||
| 797 | } |
||
| 798 | } |
||
| 799 | |||
| 800 | return $response; |
||
| 801 | } |
||
| 802 | |||
| 803 | public function sharePostAction() |
||
| 857 | |||
| 858 | public function shareCommentAction() |
||
| 899 | |||
| 900 | public function shareAction() |
||
| 901 | { |
||
| 902 | $statusMail = null; |
||
| 903 | |||
| 904 | // Has the user finished the game ? |
||
| 905 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 973 | |||
| 974 | public function getGameService() |
||
| 982 | } |
||
| 983 |
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: