| Conditions | 16 |
| Paths | 160 |
| Total Lines | 150 |
| Code Lines | 87 |
| Lines | 48 |
| Ratio | 32 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function playAction() |
||
| 46 | { |
||
| 47 | $subGameIdentifier = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
||
| 48 | $subGame = $this->getGameService()->checkGame($subGameIdentifier); |
||
| 49 | |||
| 50 | if(!$subGame){ |
||
| 51 | $games = $this->getGameService()->getMissionGames($this->game, $this->user); |
||
| 52 | foreach($games as $k=>$v){ |
||
| 53 | $g = $v['game']; |
||
| 54 | $entry = $v['entry']; |
||
| 55 | if($g->getGame()->isStarted() && $g->getGame()->isOnline()){ |
||
| 56 | $subGame = $g->getGame(); |
||
| 57 | $subGameIdentifier = $subGame->getIdentifier(); |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 64 | 'mission', |
||
| 65 | array('id' => $this->game->getIdentifier()), |
||
| 66 | array('force_canonical' => true) |
||
| 67 | ); |
||
| 68 | |||
| 69 | $session = new Container('facebook'); |
||
| 70 | // Redirect to fan gate if the game require to 'like' the page before playing |
||
| 71 | if ($session->offsetExists('signed_request')) { |
||
| 72 | if($this->game->getFbFan()){ |
||
| 73 | if ($this->getGameService()->checkIsFan($this->game) === false){ |
||
| 74 | return $this->redirect()->toRoute( |
||
| 75 | $this->game->getClassType().'/fangate', |
||
| 76 | array('id' => $this->game->getIdentifier()) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (!$this->user) { |
||
| 83 | |||
| 84 | // The game is deployed on Facebook, and played from Facebook : retrieve/register user |
||
| 85 | if ($session->offsetExists('signed_request')) { |
||
| 86 | |||
| 87 | // Get Playground user from Facebook info |
||
| 88 | $viewModel = $this->buildView($this->game); |
||
| 89 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 90 | |||
| 91 | $view = $this->forward()->dispatch( |
||
| 92 | 'playgrounduser_user', |
||
| 93 | array( |
||
| 94 | 'controller' => 'playgrounduser_user', |
||
| 95 | 'action' => 'registerFacebookUser', |
||
| 96 | 'provider' => 'facebook' |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | $this->layout()->setTemplate($beforeLayout); |
||
| 101 | $this->user = $view->user; |
||
| 102 | |||
| 103 | // If the user cannot be created/retrieved from Facebook info, redirect to login/register form |
||
| 104 | View Code Duplication | if (!$this->user){ |
|
| 105 | $redirect = urlencode( |
||
| 106 | $this->frontendUrl()->fromRoute( |
||
| 107 | 'mission/play', |
||
| 108 | array('id' => $this->game->getIdentifier()), |
||
| 109 | array('force_canonical' => true) |
||
| 110 | ) |
||
| 111 | ); |
||
| 112 | if(array_search('login', $this->game->getStepsArray())){ |
||
| 113 | return $this->redirect()->toUrl( |
||
| 114 | $this->frontendUrl()->fromRoute('mission/login') . '?redirect='.$redirect |
||
| 115 | ); |
||
| 116 | } else { |
||
| 117 | return $this->redirect()->toUrl( |
||
| 118 | $this->frontendUrl()->fromRoute('zfcuser/register') . '?redirect='.$redirect |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // The game is not played from Facebook : redirect to login/register form |
||
| 124 | |||
| 125 | View Code Duplication | } elseif(!$this->game->getAnonymousAllowed()) { |
|
| 126 | $redirect = urlencode( |
||
| 127 | $this->frontendUrl()->fromRoute( |
||
| 128 | 'mission/play', |
||
| 129 | array('id' => $this->game->getIdentifier()), |
||
| 130 | array('force_canonical' => true) |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | |||
| 134 | if(array_search('login', $this->game->getStepsArray())){ |
||
| 135 | return $this->redirect()->toUrl( |
||
| 136 | $this->frontendUrl()->fromRoute('mission/login') . '?redirect='.$redirect |
||
| 137 | ); |
||
| 138 | } else { |
||
| 139 | return $this->redirect()->toUrl( |
||
| 140 | $this->frontendUrl()->fromRoute('zfcuser/register') . '?redirect='.$redirect |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 147 | View Code Duplication | if (!$entry) { |
|
| 148 | // the user has already taken part of this game and the participation limit has been reached |
||
| 149 | $this->flashMessenger()->addMessage('You have already played'); |
||
| 150 | |||
| 151 | return $this->redirect()->toUrl( |
||
| 152 | $this->frontendUrl()->fromRoute( |
||
| 153 | 'mission/result', |
||
| 154 | array('id' => $this->game->getIdentifier()) |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 160 | $subViewModel = $this->forward()->dispatch( |
||
| 161 | 'playgroundgame_'.$subGame->getClassType(), |
||
| 162 | array( |
||
| 163 | 'controller' => 'playgroundgame_'.$subGame->getClassType(), |
||
| 164 | 'action' => 'play', |
||
| 165 | 'id' => $subGameIdentifier |
||
| 166 | ) |
||
| 167 | ); |
||
| 168 | |||
| 169 | if($this->getResponse()->getStatusCode() == 302){ |
||
| 170 | return $this->redirect()->toUrl( |
||
| 171 | $this->frontendUrl()->fromRoute( |
||
| 172 | 'mission/result', |
||
| 173 | array( |
||
| 174 | 'id' => $this->game->getIdentifier(), |
||
| 175 | 'gameId' => $subGameIdentifier |
||
| 176 | ), |
||
| 177 | array('force_canonical' => true) |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
| 183 | $this->layout()->setTemplate($beforeLayout); |
||
| 184 | |||
| 185 | // give the ability to the mission to have its customized look and feel. |
||
| 186 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
| 187 | $l = $templatePathResolver->getPaths(); |
||
| 188 | // I've already added the path for the game so the base path is $l[1] |
||
| 189 | $templatePathResolver->addPath($l[1].'custom/'.$this->game->getIdentifier()); |
||
| 190 | |||
| 191 | $subViewModel->mission = $this->game; |
||
| 192 | |||
| 193 | return $subViewModel; |
||
| 194 | } |
||
| 195 | |||
| 398 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: