GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FormRow::render()   F
last analyzed

Complexity

Conditions 73
Paths > 20000

Size

Total Lines 248
Code Lines 171

Duplication

Lines 40
Ratio 16.13 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 40
loc 248
rs 2
cc 73
eloc 171
nc 17613297
nop 3

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Form row styled for Bootstrap 3.
5
 *
6
 * Long description for file (if any)...
7
 *
8
 * @author     Leandro Silva <[email protected]>
9
 *
10
 * @category   LosUi
11
 *
12
 * @license    https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License
13
 *
14
 * @link       http://github.com/LansoWeb/LosUi
15
 * @link       http://getbootstrap.com/css/#forms
16
 */
17
namespace LosUi\Form\View\Helper;
18
19
use Zend\Form\View\Helper\FormRow as ZfFormRow;
20
use Zend\Form\ElementInterface;
21
use Zend\Form\Element\Button;
22
use Zend\Form\Element\MonthSelect;
23
use Zend\Form\LabelAwareInterface;
24
use Zend\Form\Element\DateSelect;
25
26
/**
27
 * Form row styled for Bootstrap 3.
28
 *
29
 * @author     Leandro Silva <[email protected]>
30
 *
31
 * @category   LosUi
32
 *
33
 * @license    https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License
34
 *
35
 * @link       http://github.com/LansoWeb/LosUi
36
 * @link       http://getbootstrap.com/css/#forms
37
 */
38
class FormRow extends ZfFormRow
39
{
40
    protected $rowWrapper = '<div class="form-group%s">%s%s</div>';
41
42
    protected $horizontalRowWrapper = '<div class="form-group%s">%s<div class="col-sm-%d%s">%s</div>%s</div>';
43
44
    protected static $helpBlockFormat = '<p class="help-block">%s</p>';
45
46
    /**
47
     * @return FormElementErrors|\Zend\Form\View\Helper\FormElementErrors
48
     */
49
    protected function getElementErrorsHelper()
50
    {
51
        if ($this->elementErrorsHelper) {
52
            return $this->elementErrorsHelper;
53
        }
54
55
        if (method_exists($this->view, 'plugin')) {
56
            $this->elementErrorsHelper = $this->view->plugin('losFormElementErrors');
57
        }
58
59
        if (! $this->elementErrorsHelper instanceof FormElementErrors) {
60
            $this->elementErrorsHelper = new FormElementErrors();
61
        }
62
63
        return $this->elementErrorsHelper;
64
    }
65
66
    /**
67
     * @param array $elements
68
     * @param bool $isHorizontal
69
     * @param int $labelColumns
70
     * @return string
71
     */
72
    public function renderButtons(array $elements, $isHorizontal = false, $labelColumns = 2)
73
    {
74
        $elementHelper = $this->getElementHelper();
75
76
        $elementString = '';
77
78
        foreach ($elements as $button) {
79
            $elementString .= $elementHelper->render($button).' ';
80
        }
81
82
        if ($isHorizontal && $this->labelPosition == self::LABEL_PREPEND) {
83
            $markup = sprintf(
84
                $this->horizontalRowWrapper,
85
                '',
86
                '',
87
                12 - $labelColumns,
88
                ' col-xs-offset-'.$labelColumns,
89
                $elementString,
90
                ''
91
            );
92
        } else {
93
            $markup = sprintf($this->rowWrapper, '', $elementString, '');
94
        }
95
96
        return $markup;
97
    }
98
99
    /**
100
     * @param $addon
101
     * @return string
102
     */
103
    private function addAddon($addon)
104
    {
105
        if ($addon !== null) {
106
            if (substr($addon, 0, 3) === 'fa-') {
107
                $addon = '<i class="fa '.$addon.'"></i>';
108
            } elseif (substr($addon, 0, 5) === 'glyph') {
109
                $addon = '<span class="glyphicon '.$addon.'"></span>';
110
            }
111
112
            return '<div class="input-group-addon">'.$addon.'</div>';
113
        }
114
115
        return '';
116
    }
117
118
    /**
119
     * @param ElementInterface $element
120
     * @param bool $isHorizontal
121
     * @param int $labelColumns
122
     * @return string
123
     */
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);
0 ignored issues
show
Bug introduced by
It seems like $attrs defined by $element->getDayAttributes() on line 144 can also be of type object<Traversable>; however, Zend\Form\Element\DateSelect::setDayAttributes() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $attrs defined by $element->getMonthAttributes() on line 150 can also be of type object<Traversable>; however, Zend\Form\Element\MonthS...t::setMonthAttributes() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $attrs defined by $element->getYearAttributes() on line 156 can also be of type object<Traversable>; however, Zend\Form\Element\MonthSelect::setYearAttributes() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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
373
    protected function getHelpBlock(ElementInterface $element)
374
    {
375
        return ($helpBlock = $element->getOption('help-block')) ? sprintf(
376
            self::$helpBlockFormat,
377
            $this->getEscapeHtmlHelper()->__invoke(
378
                ($translator = $this->getTranslator()) ?
379
                    $translator->translate($helpBlock, $this->getTranslatorTextDomain()) :
380
                    $helpBlock
381
            )
382
        ) : '';
383
    }
384
}
385