| Conditions | 9 |
| Paths | 54 |
| Total Lines | 53 |
| Code Lines | 35 |
| 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 |
||
| 60 | public function render(FormValidator $form, $questionData = [], $answers = []) |
||
| 61 | { |
||
| 62 | $question = new ch_yesno(); |
||
| 63 | $otherId = 0; |
||
| 64 | foreach ($questionData['options'] as $key => $option) { |
||
| 65 | if ('other' === $option) { |
||
| 66 | $otherId = $key; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | foreach ($questionData['options'] as &$option) { |
||
| 71 | if ($option === 'other') { |
||
| 72 | $option = '<p>'.get_lang('SurveyOtherAnswerSpecify').'</p>'; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | $questionId = $questionData['question_id']; |
||
| 76 | $question->render($form, $questionData, $answers); |
||
| 77 | $form->addHtml( |
||
| 78 | '<script> |
||
| 79 | $(function() { |
||
| 80 | $("input:radio[name=\"question'.$questionId.'\"]").change(function() { |
||
| 81 | if ($(this).val() == "'.$otherId.'") { |
||
| 82 | $("#other_div_'.$questionId.'").show(); |
||
| 83 | } else { |
||
| 84 | $("#other_div_'.$questionId.'").hide(); |
||
| 85 | $("#other_question'.$questionId.'").val(""); |
||
| 86 | } |
||
| 87 | }); |
||
| 88 | }); |
||
| 89 | </script>' |
||
| 90 | ); |
||
| 91 | |||
| 92 | $display = 'display:none'; |
||
| 93 | $defaultOtherData = ''; |
||
| 94 | if (!empty($answers)) { |
||
| 95 | $answers = self::decodeOptionValue($answers); |
||
| 96 | if (isset($answers[1])) { |
||
| 97 | $display = ''; |
||
| 98 | $defaultOtherData = $answers[1]; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | $form->addHtml('<div id="other_div_'.$questionId.'" class="multiple_choice_other" style="'.$display.'">'); |
||
| 102 | $element = $form->addText( |
||
| 103 | 'other_question'.$questionId, |
||
| 104 | get_lang('SurveyOtherAnswer'), |
||
| 105 | false, |
||
| 106 | ['id' => 'other_question'.$questionId] |
||
| 107 | ); |
||
| 108 | $form->addHtml('</div>'); |
||
| 109 | |||
| 110 | if (!empty($answers) && !empty($defaultOtherData)) { |
||
| 111 | $element->setValue($defaultOtherData); |
||
| 112 | $element->freeze(); |
||
| 113 | } |
||
| 121 |