| Conditions | 30 |
| Paths | 248 |
| Total Lines | 174 |
| Code Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 23 | public function playAction() |
||
| 24 | { |
||
| 25 | // the quiz is done for the first time in this entry |
||
| 26 | $firstTime = true; |
||
| 27 | $redirectFb = $this->checkFbRegistration($this->user, $this->game); |
||
| 28 | if ($redirectFb) { |
||
| 29 | return $redirectFb; |
||
| 30 | } |
||
| 31 | |||
| 32 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 33 | if (!$entry) { |
||
| 34 | // the user has already taken part of this game and the participation limit has been reached |
||
| 35 | $this->flashMessenger()->addMessage('Vous avez déjà participé!'); |
||
| 36 | |||
| 37 | return $this->redirect()->toUrl( |
||
| 38 | $this->frontendUrl()->fromRoute( |
||
|
|
|||
| 39 | $this->game->getClassType() . '/result', |
||
| 40 | array('id' => $this->game->getIdentifier()) |
||
| 41 | ) |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | $reply = $this->getGameService()->getQuizReplyMapper()->getLastGameReply($entry); |
||
| 46 | $userAnswers = array(); |
||
| 47 | if ($reply) { |
||
| 48 | $firstTime = false; |
||
| 49 | foreach ($reply->getAnswers() as $answer) { |
||
| 50 | $userAnswers[$answer->getQuestionId()][$answer->getAnswerId()] = true; |
||
| 51 | $userAnswers[$answer->getQuestionId()]['answer'] = $answer->getAnswer(); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | $questions = $this->game->getQuestions(); |
||
| 56 | $totalQuestions = count($questions); |
||
| 57 | |||
| 58 | $form = new Form(); |
||
| 59 | |||
| 60 | $inputFilter = new \Zend\InputFilter\InputFilter(); |
||
| 61 | $factory = new InputFactory(); |
||
| 62 | |||
| 63 | $i = 0; |
||
| 64 | $j = 0; |
||
| 65 | $elementData = array(); |
||
| 66 | $explanations = array(); |
||
| 67 | |||
| 68 | foreach ($questions as $q) { |
||
| 69 | if (($this->game->getQuestionGrouping() > 0 && $i % $this->game->getQuestionGrouping() === 0) || |
||
| 70 | ($i === 0 && $this->game->getQuestionGrouping() === 0) |
||
| 71 | ) { |
||
| 72 | $fieldsetName = 'questionGroup' . ++ $j; |
||
| 73 | $fieldset = new Fieldset($fieldsetName); |
||
| 74 | } |
||
| 75 | $name = 'q' . $q->getId(); |
||
| 76 | $fieldsetFilter = new \Zend\InputFilter\InputFilter(); |
||
| 77 | if ($q->getType() === 0) { |
||
| 78 | $element = new Element\Radio($name); |
||
| 79 | $values = array(); |
||
| 80 | $valuesSortedByPosition = array(); |
||
| 81 | foreach ($q->getAnswers() as $a) { |
||
| 82 | $status = ( |
||
| 83 | isset($userAnswers[$q->getId()]) && |
||
| 84 | isset($userAnswers[$q->getId()][$a->getId()]) |
||
| 85 | )? true:false; |
||
| 86 | $values[$a->getPosition()] = array( |
||
| 87 | 'id' => $a->getId(), |
||
| 88 | 'position' => $a->getPosition(), |
||
| 89 | 'answer' => $a->getAnswer(), |
||
| 90 | 'checked' => $status |
||
| 91 | ); |
||
| 92 | $explanations[$a->getAnswer()] = $a->getExplanation(); |
||
| 93 | } |
||
| 94 | ksort($values); |
||
| 95 | foreach ($values as $key => $value) { |
||
| 96 | $valuesSortedByPosition[$value['id']] = $value['answer']; |
||
| 97 | if ($value['checked']) { |
||
| 98 | $element->setValue($value['id']); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | $element->setValueOptions($valuesSortedByPosition); |
||
| 102 | $element->setLabelOptions(array("disable_html_escape"=>true)); |
||
| 103 | $elementData[$q->getId()] = new Element\Hidden($name.'-data'); |
||
| 104 | } elseif ($q->getType() === 1) { |
||
| 105 | $element = new Element\MultiCheckbox($name); |
||
| 106 | $values = array(); |
||
| 107 | $valuesSortedByPosition = array(); |
||
| 108 | foreach ($q->getAnswers() as $a) { |
||
| 109 | $values[$a->getId()] = array( |
||
| 110 | 'id' => $a->getId(), |
||
| 111 | 'position' => $a->getPosition(), |
||
| 112 | 'answer' => $a->getAnswer(), |
||
| 113 | ); |
||
| 114 | $explanations[$a->getAnswer()] = $a->getExplanation(); |
||
| 115 | $elementData[$a->getId()] = new Element\Hidden($name.'-'.$a->getId().'-data'); |
||
| 116 | } |
||
| 117 | |||
| 118 | foreach ($values as $key => $value) { |
||
| 119 | $valuesSortedByPosition[$value['id']] = $value['answer']; |
||
| 120 | } |
||
| 121 | |||
| 122 | $element->setValueOptions($valuesSortedByPosition); |
||
| 123 | $element->setLabelOptions(array("disable_html_escape"=>true)); |
||
| 124 | } elseif ($q->getType() == 2) { |
||
| 125 | $element = new Element\Textarea($name); |
||
| 126 | if (isset($userAnswers[$q->getId()])) { |
||
| 127 | $element->setValue($userAnswers[$q->getId()]['answer']); |
||
| 128 | } |
||
| 129 | $elementData[$q->getId()] = new Element\Hidden($name.'-data'); |
||
| 130 | } |
||
| 131 | |||
| 132 | $element->setLabel($q->getQuestion()); |
||
| 133 | $fieldset->add($element); |
||
| 134 | if (is_array($elementData)) { |
||
| 135 | foreach ($elementData as $id => $e) { |
||
| 136 | $fieldset->add($e); |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $fieldset->add($elementData); |
||
| 140 | } |
||
| 141 | |||
| 142 | $fieldsetFilter->add($factory->createInput(array( |
||
| 143 | 'name' => $name, |
||
| 144 | 'required' => true, |
||
| 145 | 'validators'=>array( |
||
| 146 | array( |
||
| 147 | 'name'=>'NotEmpty', |
||
| 148 | 'options'=>array( |
||
| 149 | 'messages'=>array( |
||
| 150 | 'isEmpty' => 'Merci de répondre à la question.', |
||
| 151 | ), |
||
| 152 | ), |
||
| 153 | ), |
||
| 154 | ) |
||
| 155 | ))); |
||
| 156 | |||
| 157 | $i ++; |
||
| 158 | if (($this->game->getQuestionGrouping() > 0 && $i % $this->game->getQuestionGrouping() == 0 && $i > 0) || |
||
| 159 | $i == $totalQuestions |
||
| 160 | ) { |
||
| 161 | $form->add($fieldset); |
||
| 162 | $inputFilter->add($fieldsetFilter, $fieldsetName); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $form->setInputFilter($inputFilter); |
||
| 167 | |||
| 168 | if ($this->getRequest()->isPost()) { |
||
| 169 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 170 | $form->setData($data); |
||
| 171 | |||
| 172 | // Improve it : I don't validate the form in a timer quiz as no answer is mandatory |
||
| 173 | if ($this->game->getTimer() || $form->isValid()) { |
||
| 174 | unset($data['submitForm']); |
||
| 175 | $entry = $this->getGameService()->createQuizReply($data, $this->game, $this->user); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->redirect()->toUrl( |
||
| 179 | $this->frontendUrl()->fromRoute( |
||
| 180 | $this->game->getClassType() . '/'. $this->game->nextStep($this->params('action')), |
||
| 181 | array('id' => $this->game->getIdentifier()) |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | $viewModel = $this->buildView($this->game); |
||
| 187 | $viewModel->setVariables(array( |
||
| 188 | 'firstTime' => $firstTime, |
||
| 189 | 'questions' => $questions, |
||
| 190 | 'form' => $form, |
||
| 191 | 'explanations' => $explanations, |
||
| 192 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 193 | )); |
||
| 194 | |||
| 195 | return $viewModel; |
||
| 196 | } |
||
| 197 | |||
| 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: