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