Conditions | 23 |
Paths | 101 |
Total Lines | 149 |
Code Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 12 | ||
Bugs | 3 | 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->user, $this->game); |
||
20 | if ($redirectFb) { |
||
21 | return $redirectFb; |
||
22 | } |
||
23 | |||
24 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
25 | 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 | $questions = $this->game->getQuestions(); |
||
38 | $totalQuestions = count($questions); |
||
39 | |||
40 | $form = new Form(); |
||
41 | |||
42 | $inputFilter = new \Zend\InputFilter\InputFilter(); |
||
43 | $factory = new InputFactory(); |
||
44 | |||
45 | $i = 0; |
||
46 | $j = 0; |
||
47 | $elementData = array(); |
||
48 | $explanations = array(); |
||
49 | |||
50 | foreach ($questions as $q) { |
||
51 | if (($this->game->getQuestionGrouping() > 0 && $i % $this->game->getQuestionGrouping() === 0) || |
||
52 | ($i === 0 && $this->game->getQuestionGrouping() === 0) |
||
53 | ) { |
||
54 | $fieldsetName = 'questionGroup' . ++ $j; |
||
55 | $fieldset = new Fieldset($fieldsetName); |
||
56 | } |
||
57 | $name = 'q' . $q->getId(); |
||
58 | $fieldsetFilter = new \Zend\InputFilter\InputFilter(); |
||
59 | if ($q->getType() === 0) { |
||
60 | $element = new Element\Radio($name); |
||
61 | $values = array(); |
||
62 | $valuesSortedByPosition = array(); |
||
63 | foreach ($q->getAnswers() as $a) { |
||
64 | $values[$a->getId()] = array( |
||
65 | 'id' => $a->getId(), |
||
66 | 'position' => $a->getPosition(), |
||
67 | 'answer' => $a->getAnswer(), |
||
68 | ); |
||
69 | $explanations[$a->getAnswer()] = $a->getExplanation(); |
||
70 | } |
||
71 | sort($values); |
||
72 | foreach ($values as $key => $value) { |
||
73 | $valuesSortedByPosition[$value['id']] = $value['answer']; |
||
74 | } |
||
75 | $element->setValueOptions($valuesSortedByPosition); |
||
76 | $element->setLabelOptions(array("disable_html_escape"=>true)); |
||
77 | |||
78 | $elementData[$q->getId()] = new Element\Hidden($name.'-data'); |
||
79 | } elseif ($q->getType() === 1) { |
||
80 | $element = new Element\MultiCheckbox($name); |
||
81 | $values = array(); |
||
82 | $valuesSortedByPosition = array(); |
||
83 | foreach ($q->getAnswers() as $a) { |
||
84 | $values[$a->getId()] = array( |
||
85 | 'id' => $a->getId(), |
||
86 | 'position' => $a->getPosition(), |
||
87 | 'answer' => $a->getAnswer(), |
||
88 | ); |
||
89 | $explanations[$a->getAnswer()] = $a->getExplanation(); |
||
90 | $elementData[$a->getId()] = new Element\Hidden($name.'-'.$a->getId().'-data'); |
||
91 | } |
||
92 | |||
93 | foreach ($values as $key => $value) { |
||
94 | $valuesSortedByPosition[$value['id']] = $value['answer']; |
||
95 | } |
||
96 | |||
97 | $element->setValueOptions($valuesSortedByPosition); |
||
98 | $element->setLabelOptions(array("disable_html_escape"=>true)); |
||
99 | } elseif ($q->getType() == 2) { |
||
100 | $element = new Element\Textarea($name); |
||
101 | $elementData[$q->getId()] = new Element\Hidden($name.'-data'); |
||
102 | } |
||
103 | |||
104 | $element->setLabel($q->getQuestion()); |
||
105 | $fieldset->add($element); |
||
106 | foreach ($elementData as $id => $e) { |
||
107 | $fieldset->add($e); |
||
108 | } |
||
109 | |||
110 | $fieldsetFilter->add($factory->createInput(array( |
||
111 | 'name' => $name, |
||
112 | 'required' => true, |
||
113 | 'validators'=>array( |
||
114 | array( |
||
115 | 'name'=>'NotEmpty', |
||
116 | 'options'=>array( |
||
117 | 'messages'=>array( |
||
118 | 'isEmpty' => 'Merci de répondre à la question.', |
||
119 | ), |
||
120 | ), |
||
121 | ), |
||
122 | ) |
||
123 | ))); |
||
124 | |||
125 | $i ++; |
||
126 | if (($this->game->getQuestionGrouping() > 0 && $i % $this->game->getQuestionGrouping() == 0 && $i > 0) || |
||
127 | $i == $totalQuestions |
||
128 | ) { |
||
129 | $form->add($fieldset); |
||
130 | $inputFilter->add($fieldsetFilter, $fieldsetName); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $form->setInputFilter($inputFilter); |
||
135 | |||
136 | if ($this->getRequest()->isPost()) { |
||
137 | $data = $this->getRequest()->getPost()->toArray(); |
||
138 | $form->setData($data); |
||
139 | |||
140 | // Improve it : I don't validate the form in a timer quiz as no answer is mandatory |
||
141 | if ($this->game->getTimer() || $form->isValid()) { |
||
142 | unset($data['submitForm']); |
||
143 | $entry = $this->getGameService()->createQuizReply($data, $this->game, $this->user); |
||
144 | } |
||
145 | |||
146 | return $this->redirect()->toUrl( |
||
147 | $this->frontendUrl()->fromRoute( |
||
148 | $this->game->getClassType() . '/'. $this->game->nextStep($this->params('action')), |
||
149 | array( |
||
150 | 'id' => $this->game->getIdentifier(), |
||
151 | |||
152 | ) |
||
153 | ) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | $viewModel = $this->buildView($this->game); |
||
158 | $viewModel->setVariables(array( |
||
159 | 'questions' => $questions, |
||
160 | 'form' => $form, |
||
161 | 'explanations' => $explanations, |
||
162 | )); |
||
163 | |||
164 | return $viewModel; |
||
165 | } |
||
166 | |||
400 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: