Conditions | 19 |
Paths | 433 |
Total Lines | 138 |
Code Lines | 97 |
Lines | 24 |
Ratio | 17.39 % |
Changes | 13 | ||
Bugs | 3 | Features | 2 |
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 |
||
167 | public function resultAction() |
||
168 | { |
||
169 | $statusMail = null; |
||
170 | $prediction = false; |
||
171 | $userTimer = array(); |
||
172 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
173 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
174 | 'quiz', |
||
175 | array('id' => $this->game->getIdentifier()), |
||
176 | array('force_canonical' => true) |
||
177 | ).'?key='.$secretKey; |
||
178 | // With core shortener helper |
||
179 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
180 | |||
181 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
182 | View Code Duplication | if (!$lastEntry) { |
|
183 | return $this->redirect()->toUrl( |
||
184 | $this->frontendUrl()->fromRoute( |
||
185 | 'quiz', |
||
186 | array('id' => $this->game->getIdentifier()), |
||
187 | array('force_canonical' => true) |
||
188 | ) |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | // je compte les bonnes réponses et le ratio |
||
193 | $maxCorrectAnswers = $this->game->getMaxCorrectAnswers(); |
||
194 | $winner = $lastEntry->getWinner(); |
||
195 | $replies = $this->getGameService()->getQuizReplyMapper()->getLastGameReply($lastEntry); |
||
196 | $userCorrectAnswers = 0; |
||
197 | $correctAnswers = array(); |
||
198 | $userAnswers = array(); |
||
199 | |||
200 | |||
201 | foreach ($replies as $reply) { |
||
202 | foreach ($reply->getAnswers() as $answer) { |
||
203 | if ($answer->getCorrect()) { |
||
204 | $correctAnswers[$answer->getQuestionId()][$answer->getAnswerId()] = true; |
||
205 | ++$userCorrectAnswers; |
||
206 | } |
||
207 | $userAnswers[$answer->getQuestionId()][$answer->getAnswerId()] = true; |
||
208 | $userAnswers[$answer->getQuestionId()]['answer'] = $answer->getAnswer(); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $ratioCorrectAnswers = 0; |
||
213 | if ($maxCorrectAnswers > 0) { |
||
214 | $ratioCorrectAnswers = ($userCorrectAnswers / $maxCorrectAnswers) * 100; |
||
215 | } else { |
||
216 | $ratioCorrectAnswers = 100; |
||
217 | } |
||
218 | |||
219 | if ($this->game->getTimer()) { |
||
220 | $timer = $this->getGameService()->getEntryMapper()->findOneBy( |
||
221 | array('game' => $this->game, 'user'=> $this->user) |
||
222 | ); |
||
223 | $start = $timer->getCreatedAt()->format('U'); |
||
224 | $end = $timer->getUpdatedAt()->format('U'); |
||
225 | $userTimer = array( |
||
226 | 'ratio' => $ratioCorrectAnswers, |
||
227 | 'timer' => $end - $start, |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | // Je prépare le tableau des bonnes réponses trouvées et non trouvées |
||
232 | $ga = array(); |
||
233 | $questions = $this->game->getQuestions(); |
||
234 | foreach ($questions as $q) { |
||
235 | foreach ($q->getAnswers() as $a) { |
||
236 | if ($a->getCorrect()) { |
||
237 | $ga[$q->getId()]['question'] = $q; |
||
238 | $ga[$q->getId()]['answers'][$a->getId()]['answer'] = $a->getAnswer(); |
||
239 | $ga[$q->getId()]['answers'][$a->getId()]['explanation'] = $a->getExplanation(); |
||
240 | $ga[$q->getId()]['answers'][$a->getId()]['userAnswer'] = isset($userAnswers[$q->getId()]) ? |
||
241 | $userAnswers[$q->getId()]['answer'] : |
||
242 | false; |
||
243 | |||
244 | View Code Duplication | if (isset($correctAnswers[$q->getId()]) && isset($correctAnswers[$q->getId()][$a->getId()])) { |
|
245 | $ga[$q->getId()]['answers'][$a->getId()]['found'] = true; |
||
246 | } else { |
||
247 | $ga[$q->getId()]['answers'][$a->getId()]['found'] = false; |
||
248 | } |
||
249 | |||
250 | View Code Duplication | if (isset($userAnswers[$q->getId()]) && isset($userAnswers[$q->getId()][$a->getId()])) { |
|
251 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = true; |
||
252 | } else { |
||
253 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = false; |
||
254 | } |
||
255 | |||
256 | $ga[$q->getId()]['answers'][$a->getId()]['correctAnswers'] = true; |
||
257 | } else { |
||
258 | $ga[$q->getId()]['question'] = $q; |
||
259 | $ga[$q->getId()]['answers'][$a->getId()]['answer'] = $a->getAnswer(); |
||
260 | $ga[$q->getId()]['answers'][$a->getId()]['explanation'] = $a->getExplanation(); |
||
261 | $ga[$q->getId()]['answers'][$a->getId()]['correctAnswers'] = false; |
||
262 | $ga[$q->getId()]['answers'][$a->getId()]['userAnswer'] = isset($userAnswers[$q->getId()]) ? |
||
263 | $userAnswers[$q->getId()]['answer'] : |
||
264 | false; |
||
265 | |||
266 | View Code Duplication | if (isset($userAnswers[$q->getId()]) && isset($userAnswers[$q->getId()][$a->getId()])) { |
|
267 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = true; |
||
268 | } else { |
||
269 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = false; |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | // if only one question is a prediction, we can't determine if it's a winner or looser |
||
274 | if ($q->getPrediction()) { |
||
275 | $prediction = true; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
280 | $form->setAttribute('method', 'post'); |
||
281 | |||
282 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
283 | $viewModel = $this->buildView($this->game); |
||
284 | |||
285 | $this->getGameService()->sendMail($this->game, $this->user, $lastEntry); |
||
286 | |||
287 | $viewModel->setVariables(array( |
||
288 | 'entry' => $lastEntry, |
||
289 | 'statusMail' => $statusMail, |
||
290 | 'form' => $form, |
||
291 | 'winner' => $winner, |
||
292 | 'prediction' => $prediction, |
||
293 | 'userCorrectAnswers' => $userCorrectAnswers, |
||
294 | 'maxCorrectAnswers' => $maxCorrectAnswers, |
||
295 | 'ratioCorrectAnswers' => $ratioCorrectAnswers, |
||
296 | 'gameCorrectAnswers' => $ga, |
||
297 | 'socialLinkUrl' => $socialLinkUrl, |
||
298 | 'secretKey' => $secretKey, |
||
299 | 'userTimer' => $userTimer, |
||
300 | 'userAnswers' => $userAnswers, |
||
301 | )); |
||
302 | |||
303 | return $viewModel; |
||
304 | } |
||
305 | |||
400 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: