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