Conditions | 11 |
Paths | 23 |
Total Lines | 52 |
Code Lines | 39 |
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 |
||
123 | protected function askOptions() |
||
124 | { |
||
125 | $values = []; |
||
126 | $request = $this->getRequest(); |
||
127 | echo '<form action="" method="post"><fieldset>'; |
||
128 | foreach ($this->options as $opt) { |
||
129 | $val = $request->requestVar($opt['key']); |
||
130 | if ($val === null) { |
||
131 | $val = $opt['default']; |
||
132 | } |
||
133 | |||
134 | $values[$opt['key']] = $val; |
||
135 | |||
136 | if ($opt['list']) { |
||
137 | $input = '<select name="' . $opt['key'] . '">'; |
||
138 | $input .= '<option></option>'; |
||
139 | foreach ($opt['list'] as $k => $v) { |
||
140 | $selected = ''; |
||
141 | if ($k == $val) { |
||
142 | $selected = ' selected="selected"'; |
||
143 | } |
||
144 | $input .= '<option value="' . $k . '"' . $selected . '>' . $v . '</option>'; |
||
145 | } |
||
146 | $input .= '</select>'; |
||
147 | } else { |
||
148 | $type = 'text'; |
||
149 | $input = null; |
||
150 | if (isset($opt['default'])) { |
||
151 | if (is_bool($opt['default'])) { |
||
152 | $type = 'checkbox'; |
||
153 | $checked = $val ? ' checked="checked"' : ''; |
||
154 | $input = '<input type="hidden" name="' . $opt['key'] . '" value="0" />'; |
||
155 | $input .= '<input type="' . $type . '" name="' . $opt['key'] . '" value="1"' . $checked . ' />'; |
||
156 | } else { |
||
157 | if (is_int($opt['default'])) { |
||
158 | $type = 'numeric'; |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | if (!$input) { |
||
163 | $input = '<input type="' . $type . '" name="' . $opt['key'] . '" value="' . $val . '" />'; |
||
164 | } |
||
165 | } |
||
166 | echo '<div class="field">'; |
||
167 | echo '<label> ' . $opt['title'] . ' ' . $input . '</label>'; |
||
168 | echo '</div>'; |
||
169 | echo '<br/>'; |
||
170 | } |
||
171 | echo '</fieldset><br/><input type="submit" />'; |
||
172 | echo '</form>'; |
||
173 | echo '<hr/ >'; |
||
174 | return $values; |
||
175 | } |
||
236 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.