| Conditions | 14 |
| Paths | 65 |
| Total Lines | 145 |
| Code Lines | 83 |
| Lines | 48 |
| Ratio | 33.1 % |
| 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 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 49 | View Code Duplication | if (!$entry) { |
|
| 50 | // the user has already taken part of this game and the participation limit has been reached |
||
| 51 | $this->flashMessenger()->addMessage('You have already played'); |
||
| 52 | |||
| 53 | return $this->redirect()->toUrl( |
||
| 54 | $this->frontendUrl()->fromRoute( |
||
| 55 | 'mission/result', |
||
| 56 | array('id' => $this->game->getIdentifier()) |
||
| 57 | ) |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!$subGameIdentifier) { |
||
| 62 | $subGame = $this->game->getNextPlayableGame($entry); |
||
| 63 | } else { |
||
| 64 | $subGame = $this->getGameService()->checkGame($subGameIdentifier); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!$this->game->isPlayable($subGame, $entry)) { |
||
| 68 | // this subgame is not playable |
||
| 69 | $this->flashMessenger()->addMessage('No game found'); |
||
| 70 | |||
| 71 | return $this->redirect()->toUrl( |
||
| 72 | $this->frontendUrl()->fromRoute( |
||
| 73 | 'mission', |
||
| 74 | array('id' => $this->game->getIdentifier()) |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | $session = new Container('facebook'); |
||
| 80 | // Redirect to fan gate if the game require to 'like' the page before playing |
||
| 81 | if ($session->offsetExists('signed_request')) { |
||
| 82 | if ($this->game->getFbFan()) { |
||
| 83 | if ($this->getGameService()->checkIsFan($this->game) === false) { |
||
| 84 | return $this->redirect()->toRoute( |
||
| 85 | $this->game->getClassType().'/fangate', |
||
| 86 | array('id' => $this->game->getIdentifier()) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!$this->user) { |
||
| 93 | // The game is deployed on Facebook, and played from Facebook : retrieve/register user |
||
| 94 | if ($session->offsetExists('signed_request')) { |
||
| 95 | // Get Playground user from Facebook info |
||
| 96 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 97 | |||
| 98 | $view = $this->forward()->dispatch( |
||
| 99 | 'playgrounduser_user', |
||
| 100 | array( |
||
| 101 | 'controller' => 'playgrounduser_user', |
||
| 102 | 'action' => 'registerFacebookUser', |
||
| 103 | 'provider' => 'facebook', |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | |||
| 107 | $this->layout()->setTemplate($beforeLayout); |
||
| 108 | $this->user = $view->user; |
||
| 109 | |||
| 110 | // If the user cannot be created/retrieved from Facebook info, redirect to login/register form |
||
| 111 | View Code Duplication | if (!$this->user) { |
|
| 112 | $redirect = urlencode( |
||
| 113 | $this->frontendUrl()->fromRoute( |
||
| 114 | 'mission/play', |
||
| 115 | array('id' => $this->game->getIdentifier()), |
||
| 116 | array('force_canonical' => true) |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | if (array_search('login', $this->game->getStepsArray())) { |
||
| 120 | return $this->redirect()->toUrl( |
||
| 121 | $this->frontendUrl()->fromRoute('mission/login').'?redirect='.$redirect |
||
| 122 | ); |
||
| 123 | } else { |
||
| 124 | return $this->redirect()->toUrl( |
||
| 125 | $this->frontendUrl()->fromRoute('zfcuser/register').'?redirect='.$redirect |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | // The game is not played from Facebook : redirect to login/register form |
||
| 131 | View Code Duplication | } elseif (!$this->game->getAnonymousAllowed()) { |
|
| 132 | $redirect = urlencode( |
||
| 133 | $this->frontendUrl()->fromRoute( |
||
| 134 | 'mission/play', |
||
| 135 | array('id' => $this->game->getIdentifier()), |
||
| 136 | array('force_canonical' => true) |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | |||
| 140 | if (array_search('login', $this->game->getStepsArray())) { |
||
| 141 | return $this->redirect()->toUrl( |
||
| 142 | $this->frontendUrl()->fromRoute('mission/login').'?redirect='.$redirect |
||
| 143 | ); |
||
| 144 | } else { |
||
| 145 | return $this->redirect()->toUrl( |
||
| 146 | $this->frontendUrl()->fromRoute('zfcuser/register').'?redirect='.$redirect |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 153 | $classGame = __NAMESPACE__ . '\\' . ucfirst($subGame->getClassType()); |
||
| 154 | |||
| 155 | $subViewModel = $this->forward()->dispatch( |
||
| 156 | $classGame, |
||
| 157 | array( |
||
| 158 | 'controller' => $classGame, |
||
| 159 | 'action' => 'play', |
||
| 160 | 'id' => $subGame->getIdentifier() |
||
| 161 | ) |
||
| 162 | ); |
||
| 163 | |||
| 164 | if ($this->getResponse()->getStatusCode() == 302) { |
||
| 165 | return $this->redirect()->toUrl( |
||
| 166 | $this->frontendUrl()->fromRoute( |
||
| 167 | 'mission/result', |
||
| 168 | array( |
||
| 169 | 'id' => $this->game->getIdentifier(), |
||
| 170 | 'gameId' => $subGame->getIdentifier() |
||
| 171 | ), |
||
| 172 | array('force_canonical' => true) |
||
| 173 | ) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
| 178 | $this->layout()->setTemplate($beforeLayout); |
||
| 179 | |||
| 180 | // give the ability to the mission to have its customized look and feel. |
||
| 181 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
| 182 | $l = $templatePathResolver->getPaths(); |
||
| 183 | // I've already added the path for the game so the base path is $l[1] |
||
| 184 | $templatePathResolver->addPath($l[1].'custom/'.$this->game->getIdentifier()); |
||
| 185 | |||
| 186 | $subViewModel->mission = $this->game; |
||
| 187 | |||
| 188 | return $subViewModel; |
||
| 189 | } |
||
| 190 | |||
| 363 |
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: