Conditions | 73 |
Paths | > 20000 |
Total Lines | 248 |
Code Lines | 171 |
Lines | 40 |
Ratio | 16.13 % |
Changes | 4 | ||
Bugs | 1 | 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 |
||
124 | public function render(ElementInterface $element, $isHorizontal = false, $labelColumns = 2) |
||
125 | { |
||
126 | $escapeHtmlHelper = $this->getEscapeHtmlHelper(); |
||
127 | $labelHelper = $this->getLabelHelper(); |
||
128 | $elementHelper = $this->getElementHelper(); |
||
129 | $elementErrorsHelper = $this->getElementErrorsHelper(); |
||
130 | |||
131 | $label = $element->getLabel(); |
||
132 | $inputErrorClass = $this->getInputErrorClass(); |
||
133 | |||
134 | if (isset($label) && '' !== $label) { |
||
135 | // Translate the label |
||
136 | if (null !== ($translator = $this->getTranslator())) { |
||
137 | $label = $translator->translate($label, $this->getTranslatorTextDomain()); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $type = $element->getAttribute('type'); |
||
142 | |||
143 | if ($element instanceof DateSelect) { |
||
144 | $attrs = $element->getDayAttributes(); |
||
145 | $classAttributes = (in_array('class', $attrs) ? $attrs['class'].' ' : ''); |
||
146 | $classAttributes = $classAttributes.'form-control'; |
||
147 | $attrs['class'] = $classAttributes; |
||
148 | $element->setDayAttributes($attrs); |
||
|
|||
149 | |||
150 | $attrs = $element->getMonthAttributes(); |
||
151 | $classAttributes = (in_array('class', $attrs) ? $attrs['class'].' ' : ''); |
||
152 | $classAttributes = $classAttributes.'form-control'; |
||
153 | $attrs['class'] = $classAttributes; |
||
154 | $element->setMonthAttributes($attrs); |
||
155 | |||
156 | $attrs = $element->getYearAttributes(); |
||
157 | $classAttributes = (in_array('class', $attrs) ? $attrs['class'].' ' : ''); |
||
158 | $classAttributes = $classAttributes.'form-control'; |
||
159 | $attrs['class'] = $classAttributes; |
||
160 | $element->setYearAttributes($attrs); |
||
161 | } elseif ($type != 'checkbox' && $type != 'submit' && $type != 'button' && $type != 'radio' && |
||
162 | $type != 'file' && $type != 'multi_checkbox' |
||
163 | ) { |
||
164 | $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class').' ' : ''); |
||
165 | $classAttributes = $classAttributes.'form-control'; |
||
166 | $element->setAttribute('class', $classAttributes); |
||
167 | } elseif ($type == 'button' || $type == 'submit') { |
||
168 | $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class').' ' : ''); |
||
169 | $classAttributes = $classAttributes.'btn'; |
||
170 | $element->setAttribute('class', $classAttributes); |
||
171 | } |
||
172 | |||
173 | // Does this element have errors ? |
||
174 | if (count($element->getMessages()) > 0 && ! empty($inputErrorClass)) { |
||
175 | $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class').' ' : ''); |
||
176 | $classAttributes = $classAttributes.$inputErrorClass; |
||
177 | |||
178 | $element->setAttribute('class', $classAttributes); |
||
179 | } |
||
180 | |||
181 | if ($this->partial) { |
||
182 | $vars = [ |
||
183 | 'element' => $element, |
||
184 | 'label' => $label, |
||
185 | 'labelAttributes' => $this->labelAttributes, |
||
186 | 'labelPosition' => $this->labelPosition, |
||
187 | 'renderErrors' => $this->renderErrors, |
||
188 | ]; |
||
189 | |||
190 | return $this->view->render($this->partial, $vars); |
||
191 | } |
||
192 | |||
193 | $elementErrors = ''; |
||
194 | if ($this->renderErrors) { |
||
195 | $elementErrors = $elementErrorsHelper->render($element, [ |
||
196 | 'class' => 'text-danger', |
||
197 | ]); |
||
198 | } |
||
199 | |||
200 | $elementString = $elementHelper->render($element); |
||
201 | $addonAppend = $element->getOption('addon-append'); |
||
202 | $addonPrepend = $element->getOption('addon-prepend'); |
||
203 | if ($addonAppend !== null || $addonPrepend !== null) { |
||
204 | $addonString = '<div class="input-group">'; |
||
205 | $addonString .= $this->addAddon($addonPrepend); |
||
206 | $addonString .= $elementString; |
||
207 | $addonString .= $this->addAddon($addonAppend); |
||
208 | $addonString .= '</div>'; |
||
209 | |||
210 | $elementString = $addonString; |
||
211 | } |
||
212 | |||
213 | $elementString .= $this->getHelpBlock($element); |
||
214 | |||
215 | // hidden elements do not need a <label> -https://github.com/zendframework/zf2/issues/5607 |
||
216 | if (isset($label) && '' !== $label && $type !== 'hidden') { |
||
217 | $labelAttributes = []; |
||
218 | |||
219 | if ($element instanceof LabelAwareInterface) { |
||
220 | $labelAttributes = $element->getLabelAttributes(); |
||
221 | } |
||
222 | |||
223 | if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) { |
||
224 | $label = $escapeHtmlHelper($label); |
||
225 | } |
||
226 | |||
227 | if (empty($labelAttributes)) { |
||
228 | $labelAttributes = $this->labelAttributes; |
||
229 | } |
||
230 | |||
231 | if (! $element->getAttribute('id') && $element->getName()) { |
||
232 | $element->setAttribute('id', $element->getName()); |
||
233 | } |
||
234 | if ($element->getAttribute('id')) { |
||
235 | $labelAttributes['for'] = $element->getAttribute('id'); |
||
236 | } |
||
237 | if ($isHorizontal) { |
||
238 | $labelAttributes['class'] = ' control-label col-sm-'.$labelColumns; |
||
239 | if ($element instanceof LabelAwareInterface) { |
||
240 | $element->setLabelAttributes([ |
||
241 | 'class' => 'control-label col-sm-'.$labelColumns, |
||
242 | ]); |
||
243 | } |
||
244 | } else { |
||
245 | $labelAttributes['class'] = ' control-label'; |
||
246 | if ($element instanceof LabelAwareInterface) { |
||
247 | $element->setLabelAttributes([ |
||
248 | 'class' => 'control-label', |
||
249 | ]); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested |
||
254 | // labels. The semantic way is to group them inside a fieldset |
||
255 | if (! $isHorizontal && ($type === 'multi_checkbox' || $type === 'radio' || |
||
256 | ($element instanceof MonthSelect && ! $element instanceof DateSelect))) { |
||
257 | $markup = sprintf( |
||
258 | '<fieldset class="radio"><legend>%s</legend>%s</fieldset>', |
||
259 | $label, |
||
260 | $elementString |
||
261 | ); |
||
262 | } elseif ($type == 'checkbox') { |
||
263 | // Checkboxes need special treatment too |
||
264 | if ($isHorizontal) { |
||
265 | $markup = '<div class="form-group"><div class="checkbox col-xs-'.(12 - $labelColumns) . |
||
266 | ' col-xs-offset-'.$labelColumns.'"><label>'.$elementString.$label.'</label></div></div>'; |
||
267 | } else { |
||
268 | $markup = '<div class="checkbox"><label>'.$elementString.$label.'</label></div>'; |
||
269 | } |
||
270 | } else { |
||
271 | // Ensure element and label will be separated if element has an `id`-attribute. |
||
272 | // If element has label option `always_wrap` it will be nested in any case. |
||
273 | if ($element->hasAttribute('id') && |
||
274 | ($element instanceof LabelAwareInterface && ! $element->getLabelOption('always_wrap')) |
||
275 | ) { |
||
276 | $labelOpen = ''; |
||
277 | $labelClose = ''; |
||
278 | $label = $labelHelper($element); |
||
279 | } else { |
||
280 | $labelOpen = $labelHelper->openTag($labelAttributes); |
||
281 | $labelClose = $labelHelper->closeTag(); |
||
282 | } |
||
283 | |||
284 | if ($label !== '' && (! $element->hasAttribute('id')) || |
||
285 | ($element instanceof LabelAwareInterface && $element->getLabelOption('always_wrap')) |
||
286 | ) { |
||
287 | $label = '<span>'.$label.'</span>'; |
||
288 | } |
||
289 | |||
290 | $addDivClass = ''; |
||
291 | // Button element is a special case, because label is always rendered inside it |
||
292 | if ($element instanceof Button) { |
||
293 | $labelOpen = $labelClose = $label = ''; |
||
294 | $addDivClass = ' col-xs-offset-'.$labelColumns; |
||
295 | } elseif ($element instanceof DateSelect) { |
||
296 | $elementString = '<div class="form-inline">'.$elementString.'</div>'; |
||
297 | } |
||
298 | if ($type == 'radio') { |
||
299 | $addDivClass = ' radio'; |
||
300 | } |
||
301 | |||
302 | switch ($this->labelPosition) { |
||
303 | View Code Duplication | case self::LABEL_PREPEND: |
|
304 | if ($isHorizontal) { |
||
305 | $markup = sprintf( |
||
306 | $this->horizontalRowWrapper, |
||
307 | ! empty($elementErrors) ? ' has-error' : '', |
||
308 | $labelOpen . $label . $labelClose, |
||
309 | 12 - $labelColumns, |
||
310 | $addDivClass, |
||
311 | $elementString . ($this->renderErrors ? $elementErrors : ''), |
||
312 | '' |
||
313 | ); |
||
314 | } else { |
||
315 | $markup = sprintf( |
||
316 | $this->rowWrapper, |
||
317 | ! empty($elementErrors) ? ' has-error' : '', |
||
318 | $labelOpen . $label . $labelClose, |
||
319 | $elementString |
||
320 | ); |
||
321 | } |
||
322 | break; |
||
323 | case self::LABEL_APPEND: |
||
324 | View Code Duplication | default: |
|
325 | if ($isHorizontal) { |
||
326 | $markup = sprintf( |
||
327 | $this->horizontalRowWrapper, |
||
328 | ! empty($elementErrors) ? ' has-error' : '', |
||
329 | '', |
||
330 | 12 - $labelColumns, |
||
331 | $addDivClass, |
||
332 | $elementString . ($this->renderErrors ? $elementErrors : ''), |
||
333 | $labelOpen . $label . $labelClose |
||
334 | ); |
||
335 | } else { |
||
336 | $markup = sprintf( |
||
337 | $this->rowWrapper, |
||
338 | ! empty($elementErrors) ? ' has-error' : '', |
||
339 | $elementString, |
||
340 | $labelOpen . $label . $labelClose |
||
341 | ); |
||
342 | } |
||
343 | break; |
||
344 | } |
||
345 | } |
||
346 | |||
347 | if (! $isHorizontal && $this->renderErrors) { |
||
348 | $markup .= $elementErrors; |
||
349 | } |
||
350 | } else { |
||
351 | if ($isHorizontal && $this->labelPosition == self::LABEL_PREPEND && $type !== 'hidden') { |
||
352 | $markup = sprintf( |
||
353 | $this->horizontalRowWrapper, |
||
354 | ! empty($elementErrors) ? ' has-error' : '', |
||
355 | '', |
||
356 | 12 - $labelColumns, |
||
357 | ' col-xs-offset-'.$labelColumns, |
||
358 | $elementString.($this->renderErrors ? $elementErrors : ''), |
||
359 | '' |
||
360 | ); |
||
361 | } else { |
||
362 | if ($this->renderErrors) { |
||
363 | $markup = $elementString.$elementErrors; |
||
364 | } else { |
||
365 | $markup = $elementString; |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | |||
370 | return $markup; |
||
371 | } |
||
372 | |||
385 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.