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