FormRow::render()   F
last analyzed

Complexity

Conditions 42
Paths > 20000

Size

Total Lines 175
Code Lines 112

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 79
CRAP Score 53.278

Importance

Changes 0
Metric Value
eloc 112
dl 0
loc 175
ccs 79
cts 97
cp 0.8144
rs 0
c 0
b 0
f 0
cc 42
nc 66818
nop 2
crap 53.278

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
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Core\Form\View\Helper;
11
12
use Laminas\Form\View\Helper\FormRow as LaminasFormRow;
13
use Laminas\Form\ElementInterface;
14
use Core\Form\ViewPartialProviderInterface;
15
use Laminas\Form\Element\Button;
16
17
class FormRow extends LaminasFormRow
18
{
19
    protected $layout;
20
    protected $shouldWrap = true;
21
    protected $labelSpanWidth = 3;
22
    
23
    
24 4
    public function setShouldWrap($flag)
25
    {
26 4
        $this->shouldWrap = (bool) $flag;
27 4
        return $this;
28
    }
29
    
30 4
    public function shouldWrap()
31
    {
32 4
        return $this->shouldWrap;
33
    }
34
    
35
    /**
36
     * Utility form helper that renders a label (if it exists), an element and errors
37
     *
38
     * @param ElementInterface $element
39
     * @return string
40
     * @throws \Laminas\Form\Exception\DomainException
41
     */
42 4
    public function render(ElementInterface $element, $ignoreViewPartial = false)
43
    {
44 4
        $labelAttributes = $element->getLabelAttributes();
0 ignored issues
show
Bug introduced by
The method getLabelAttributes() does not exist on Laminas\Form\ElementInterface. Did you maybe mean getLabel()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $labelAttributes = $element->getLabelAttributes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45 4
        $labelWidth = $element->getOption('labelWidth');
46 4
        if (!$labelWidth) {
47 4
            $labelWidth = $this->labelSpanWidth;
48
        }
49
50 4
        if ($element instanceof ViewPartialProviderInterface && !$ignoreViewPartial) {
51
            return $this->getView()->partial(
0 ignored issues
show
Bug introduced by
The method partial() does not exist on Laminas\View\Renderer\RendererInterface. It seems like you code against a sub-type of Laminas\View\Renderer\RendererInterface such as Laminas\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            return $this->getView()->/** @scrutinizer ignore-call */ partial(
Loading history...
52
                $element->getViewPartial(),
53
                                             array(
54
                                                 'element' => $element,
55
                                                 'labelWitdh' => $labelWidth,
56
                                                 'label_attributes' => $labelAttributes
57
                                                            )
58
            );
59
        }
60
61 4
        $escapeHtmlHelper    = $this->getEscapeHtmlHelper();
62 4
        $labelHelper         = $this->getLabelHelper();
63 4
        $elementHelper       = $this->getElementHelper();
64 4
        $elementErrorsHelper = $this->getElementErrorsHelper();
65
    
66 4
        $inputErrorClass = $this->getInputErrorClass();
67 4
        $elementErrors   = $elementErrorsHelper->render($element);
68
        
69
        // general Class
70 4
        $form_row_class = 'row';
71 4
        if ($this->layout == Form::LAYOUT_HORIZONTAL) {
72 4
            $form_row_class = 'form-group';
73
        }
74
        
75 4
        if (($elementRowClass = $element->getOption('rowClass')) != '') {
76
            $form_row_class .= ' '.$elementRowClass;
77
        }
78
        
79
        // Does this element have errors ?
80 4
        if (!empty($elementErrors) && !empty($inputErrorClass) && $this->layout != Form::LAYOUT_BARE) {
81
            $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
82
            $classAttributes = $classAttributes . $inputErrorClass;
83
    
84
            $element->setAttribute('class', $classAttributes);
85
        }
86
    
87 4
        if (!$element->hasAttribute('id')) {
88 4
            $elementId = preg_replace(
89 4
                array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'),
90 4
                array('-'             , '-',     ''),
91 4
                $element->getName()
92
            );
93 4
            $element->setAttribute('id', $elementId);
94
        } else {
95 4
            $elementId = $element->getAttribute('id');
96
        }
97
        /*
98
         * add form-control class to all form elements, but "submit" or "reset" and Buttons!
99
         */
100 4
        if ($element->getAttribute('type') != 'submit'
101 4
            && $element->getAttribute('type') != 'reset'
102 4
            && $element->getAttribute('type') != 'checkbox'
103 4
            && !$element instanceof Button
104
        ) {
105 4
            $element->setAttribute('class', $element->getAttribute('class').' form-control ');
106
        }
107
        
108 4
        $elementString = $elementHelper->render($element, $ignoreViewPartial);
0 ignored issues
show
Unused Code introduced by
The call to Laminas\Form\View\Helper\FormElement::render() has too many arguments starting with $ignoreViewPartial. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        /** @scrutinizer ignore-call */ 
109
        $elementString = $elementHelper->render($element, $ignoreViewPartial);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
109 4
        $desc = $element->getOption('description', false);
0 ignored issues
show
Unused Code introduced by
The call to Laminas\Form\ElementInterface::getOption() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        /** @scrutinizer ignore-call */ 
110
        $desc = $element->getOption('description', false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110 4
        if ($desc && $this->layout != Form::LAYOUT_BARE) {
111
            if (null !== ($translator = $this->getTranslator())) {
112
                $desc = $translator->translate(
113
                                 $desc,
114
                                 $this->getTranslatorTextDomain()
115
                             );
116
            }
117
            $elementString .= sprintf(
118
                '<div id="%s-desc" class="cam-description alert alert-info">%s</div>',
119
                $elementId,
120
                $desc
121
            );
122
        }
123
        
124 4
        if (!$element instanceof \Laminas\Form\Element\Hidden
125 4
            && !$element instanceof \Laminas\Form\Element\Button
126 4
            && $this->layout != Form::LAYOUT_BARE
127
        ) {
128 4
            $elementString .= sprintf(
129 4
                '<div id="%s-errors" class="errors">%s</div>',
130
                $elementId,
131
                $elementErrors
132
            );
133
        }
134
        
135
        // moved label here so we can change it in the ElementViewHelper
136 4
        $label           = $element->getLabel();
137 4
        if (isset($label) && '' !== $label && !$element instanceof \Laminas\Form\Element\Button) {
138
            // Translate the label
139 4
            if (null !== ($translator = $this->getTranslator())) {
140 4
                $label = $translator->translate(
141 4
                    $label,
142 4
                    $this->getTranslatorTextDomain()
143
                );
144
            }
145
    
146 4
            $label = $escapeHtmlHelper($label);
147 4
            $labelAttributes = $element->getLabelAttributes();
148
    
149 4
            if (empty($labelAttributes)) {
150 4
                $labelAttributes = $this->labelAttributes;
151
            }
152
    
153
            // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
154
            // labels. The semantic way is to group them inside a fieldset
155 4
            $type = $element->getAttribute('type');
156 4
            if ($type === 'multi_checkbox' || $type === 'radio') {
157
                $markup = sprintf(
158
                    '<fieldset><legend>%s</legend>%s</fieldset>',
159
                    $label,
160
                    $elementString
161
                );
162
            } else {
163 4
                if ($this->layout == Form::LAYOUT_BARE) {
164
                    $markup = $elementString;
165
                } else {
166 4
                    if (!isset($labelAttributes['for'])) {
167 4
                        $labelAttributes['for'] = $elementId;
168
                    }
169 4
                    if (Form::LAYOUT_HORIZONTAL == $this->layout) {
170 4
                        if (!isset($labelAttributes['class'])) {
171 4
                            $labelAttributes['class'] = '';
172
                        }
173 4
                        $labelAttributes['class'] .= ' control-label';
174
                    }
175 4
                    $element->setLabelAttributes($labelAttributes);
0 ignored issues
show
Bug introduced by
The method setLabelAttributes() does not exist on Laminas\Form\ElementInterface. Did you maybe mean setLabel()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
                    $element->/** @scrutinizer ignore-call */ 
176
                              setLabelAttributes($labelAttributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177 4
                    $label = $labelHelper($element, $label);
178 4
                    if ($this->shouldWrap) {
179 4
                        $spanWidth = 12 - $labelWidth;
180 4
                        $elementString = sprintf(
181 4
                            '<div class="col-md-%d%s" id="' . $elementId . '-span">%s</div>',
182
                            $spanWidth,
183 4
                            $elementErrors ? " $inputErrorClass" : '',
184
                            $elementString
185
                        );
186 4
                        $label = sprintf(
187 4
                            '<div class="col-md-%d yk-label">%s</div>',
188
                            $labelWidth,
189
                            $label
190
                        );
191
                    }
192 4
                    $markup = $label . $elementString;
193
                }
194
            }
195
        } else {
196 4
            if ($this->shouldWrap
197 4
                && !$element instanceof \Laminas\Form\Element\Hidden
198 4
                && !$element instanceof \Laminas\Form\Element\Button
199 4
                && $this->layout != Form::LAYOUT_BARE
200
                ) {
201 3
                $elementString = sprintf(
202 3
                    '<div class="col-md-12">%s</div>',
203
                    $elementString
204
                );
205
            }
206 4
            $markup = $elementString;
207
        }
208 4
        if ($this->shouldWrap
209 4
            && !$element instanceof \Laminas\Form\Element\Hidden
210 4
            && !$element instanceof \Laminas\Form\Element\Button
211 4
            && $this->layout != Form::LAYOUT_BARE
212
            ) {
213 4
            $markup = sprintf('<div class="controls controls-row ' . $form_row_class . '">%s</div>', $markup);
214
        }
215
    
216 4
        return $markup;
217
    }
218
    
219
    /**
220
     * Invoke helper as functor
221
     *
222
     * Proxies to {@link render()}.
223
     *
224
     * @param null|ElementInterface $element
225
     * @param null|string           $labelPosition
226
     * @param bool                  $renderErrors
227
     * @return string|FormRow
228
     */
229 4
    public function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $layout = null)
230
    {
231 4
        if (!$element) {
232 4
            return $this;
233
        }
234
    
235 4
        if ($labelPosition !== null) {
236
            $this->setLabelPosition($labelPosition);
237
        } else {
238 4
            $this->setLabelPosition(self::LABEL_PREPEND);
239
        }
240
    
241 4
        if ($renderErrors !== null) {
242
            $this->setRenderErrors($renderErrors);
243
        }
244
        
245 4
        if (null !== $layout) {
246 4
            $this->layout = $layout;
247
        }
248
    
249 4
        return $this->render($element);
250
    }
251
}
252