| Conditions | 18 |
| Paths | 325 |
| Total Lines | 136 |
| Code Lines | 97 |
| Lines | 24 |
| Ratio | 17.65 % |
| Changes | 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 |
||
| 198 | public function resultAction() |
||
| 199 | { |
||
| 200 | $statusMail = null; |
||
| 201 | $prediction = false; |
||
| 202 | $userTimer = array(); |
||
| 203 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 204 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 205 | 'quiz', |
||
| 206 | array('id' => $this->game->getIdentifier()), |
||
| 207 | array('force_canonical' => true) |
||
| 208 | ).'?key='.$secretKey; |
||
| 209 | |||
| 210 | // With core shortener helper |
||
| 211 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 212 | |||
| 213 | $lastEntry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
| 214 | View Code Duplication | if (!$lastEntry) { |
|
| 215 | return $this->redirect()->toUrl( |
||
| 216 | $this->frontendUrl()->fromRoute( |
||
| 217 | 'quiz', |
||
| 218 | array('id' => $this->game->getIdentifier()), |
||
| 219 | array('force_canonical' => true) |
||
| 220 | ) |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | // je compte les bonnes réponses et le ratio |
||
| 225 | $maxCorrectAnswers = $this->game->getMaxCorrectAnswers(); |
||
| 226 | $winner = $lastEntry->getWinner(); |
||
| 227 | $reply = $this->getGameService()->getQuizReplyMapper()->getLastGameReply($lastEntry); |
||
| 228 | $userCorrectAnswers = 0; |
||
| 229 | $correctAnswers = array(); |
||
| 230 | $userAnswers = array(); |
||
| 231 | |||
| 232 | foreach ($reply->getAnswers() as $answer) { |
||
| 233 | if ($answer->getCorrect()) { |
||
| 234 | $correctAnswers[$answer->getQuestionId()][$answer->getAnswerId()] = true; |
||
| 235 | ++$userCorrectAnswers; |
||
| 236 | } |
||
| 237 | $userAnswers[$answer->getQuestionId()][$answer->getAnswerId()] = true; |
||
| 238 | $userAnswers[$answer->getQuestionId()]['answer'] = $answer->getAnswer(); |
||
| 239 | } |
||
| 240 | |||
| 241 | $ratioCorrectAnswers = 0; |
||
| 242 | if ($maxCorrectAnswers > 0) { |
||
| 243 | $ratioCorrectAnswers = ($userCorrectAnswers / $maxCorrectAnswers) * 100; |
||
| 244 | } else { |
||
| 245 | $ratioCorrectAnswers = 100; |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($this->game->getTimer()) { |
||
| 249 | $timer = $this->getGameService()->getEntryMapper()->findOneBy( |
||
| 250 | array('game' => $this->game, 'user'=> $this->user) |
||
| 251 | ); |
||
| 252 | $start = $timer->getCreatedAt()->format('U'); |
||
| 253 | $end = $timer->getUpdatedAt()->format('U'); |
||
| 254 | $userTimer = array( |
||
| 255 | 'ratio' => $ratioCorrectAnswers, |
||
| 256 | 'timer' => $end - $start, |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | // Je prépare le tableau des bonnes réponses trouvées et non trouvées |
||
| 261 | $ga = array(); |
||
| 262 | $questions = $this->game->getQuestions(); |
||
| 263 | foreach ($questions as $q) { |
||
| 264 | foreach ($q->getAnswers() as $a) { |
||
| 265 | if ($a->getCorrect()) { |
||
| 266 | $ga[$q->getId()]['question'] = $q; |
||
| 267 | $ga[$q->getId()]['answers'][$a->getId()]['answer'] = $a->getAnswer(); |
||
| 268 | $ga[$q->getId()]['answers'][$a->getId()]['explanation'] = $a->getExplanation(); |
||
| 269 | $ga[$q->getId()]['answers'][$a->getId()]['userAnswer'] = isset($userAnswers[$q->getId()]) ? |
||
| 270 | $userAnswers[$q->getId()]['answer'] : |
||
| 271 | false; |
||
| 272 | |||
| 273 | View Code Duplication | if (isset($correctAnswers[$q->getId()]) && isset($correctAnswers[$q->getId()][$a->getId()])) { |
|
| 274 | $ga[$q->getId()]['answers'][$a->getId()]['found'] = true; |
||
| 275 | } else { |
||
| 276 | $ga[$q->getId()]['answers'][$a->getId()]['found'] = false; |
||
| 277 | } |
||
| 278 | |||
| 279 | View Code Duplication | if (isset($userAnswers[$q->getId()]) && isset($userAnswers[$q->getId()][$a->getId()])) { |
|
| 280 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = true; |
||
| 281 | } else { |
||
| 282 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = false; |
||
| 283 | } |
||
| 284 | |||
| 285 | $ga[$q->getId()]['answers'][$a->getId()]['correctAnswers'] = true; |
||
| 286 | } else { |
||
| 287 | $ga[$q->getId()]['question'] = $q; |
||
| 288 | $ga[$q->getId()]['answers'][$a->getId()]['answer'] = $a->getAnswer(); |
||
| 289 | $ga[$q->getId()]['answers'][$a->getId()]['explanation'] = $a->getExplanation(); |
||
| 290 | $ga[$q->getId()]['answers'][$a->getId()]['correctAnswers'] = false; |
||
| 291 | $ga[$q->getId()]['answers'][$a->getId()]['userAnswer'] = isset($userAnswers[$q->getId()]) ? |
||
| 292 | $userAnswers[$q->getId()]['answer'] : |
||
| 293 | false; |
||
| 294 | |||
| 295 | View Code Duplication | if (isset($userAnswers[$q->getId()]) && isset($userAnswers[$q->getId()][$a->getId()])) { |
|
| 296 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = true; |
||
| 297 | } else { |
||
| 298 | $ga[$q->getId()]['answers'][$a->getId()]['yourChoice'] = false; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | // if only one question is a prediction, we can't determine if it's a winner or looser |
||
| 303 | if ($q->getPrediction()) { |
||
| 304 | $prediction = true; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 309 | $form->setAttribute('method', 'post'); |
||
| 310 | |||
| 311 | $viewModel = $this->buildView($this->game); |
||
| 312 | |||
| 313 | $this->getGameService()->sendMail($this->game, $this->user, $lastEntry); |
||
| 314 | |||
| 315 | $viewModel->setVariables(array( |
||
| 316 | 'entry' => $lastEntry, |
||
| 317 | 'statusMail' => $statusMail, |
||
| 318 | 'form' => $form, |
||
| 319 | 'winner' => $winner, |
||
| 320 | 'prediction' => $prediction, |
||
| 321 | 'userCorrectAnswers' => $userCorrectAnswers, |
||
| 322 | 'maxCorrectAnswers' => $maxCorrectAnswers, |
||
| 323 | 'ratioCorrectAnswers' => $ratioCorrectAnswers, |
||
| 324 | 'gameCorrectAnswers' => $ga, |
||
| 325 | 'socialLinkUrl' => $socialLinkUrl, |
||
| 326 | 'secretKey' => $secretKey, |
||
| 327 | 'userTimer' => $userTimer, |
||
| 328 | 'userAnswers' => $userAnswers, |
||
| 329 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 330 | )); |
||
| 331 | |||
| 332 | return $viewModel; |
||
| 333 | } |
||
| 334 | |||
| 429 |
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: