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