Issues (138)

src/Renderer/Bootstrap5.php (3 issues)

1
<?php
2
3
/**
4
 * Class Bootstrap4
5
 */
6
class Nip_Form_Renderer_Bootstrap5 extends Nip_Form_Renderer_Bootstrap
7
{
8
    /**
9
     * @param Nip_Form_Element_Abstract $element
10
     * @return string
11
     */
12
    public function renderRow($element)
13
    {
14
        $return = '';
15
        if (!$element->isRendered()) {
16
            if ($element->hasCustomRenderer()) {
17
                return $element->render();
18
            }
19
20
            $formType = $this->getForm()->hasClass('form-horizontal') ? 'form-horizontal' : '';
21
22
            $rowClass = 'row';
23
            $rowClass .= ' row-' . $element->getUniqueId();
24
            $rowClass .= $element->isError() ? ' has-error' : '';
25
            $return .= '<div class="' . $rowClass . '">';
26
27
            $renderLabel = $element->getOption('render_label');
0 ignored issues
show
Are you sure the assignment to $renderLabel is correct as $element->getOption('render_label') targeting Nip\Form\Elements\AbstractElement::getOption() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
            if ($renderLabel !== false) {
0 ignored issues
show
The condition $renderLabel !== false is always true.
Loading history...
29
                $return .= $this->renderLabel($element);
30
            }
31
32
            $class = '';
33
            if ($formType == 'form-horizontal') {
34
                $class = $element->getType() == 'checkbox' ? 'col-sm-offset-3 col-sm-9' : 'col-sm-9';
35
            }
36
37
            $return .= '<div class="' . $class . '">';
38
            $return .= $this->renderElement($element);
39
40
            $helpBlock = $element->getOption('form-help');
41
            if ($helpBlock) {
42
                $return .= '<span class="help-block">' . $helpBlock . '</span>';
43
            }
44
45
            $return .= $element->renderErrors();
46
            $return .= '</div>';
47
            $return .= '</div>';
48
        }
49
50
        return $return;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function renderButtons()
57
    {
58
        $return = '';
59
        $buttons = $this->getForm()->getButtons();
60
61
        if ($buttons) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $buttons of type Nip\Form\Buttons\AbstractButton[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
            $return .= '<div class="row">
63
                            <div class="' . ($this->getForm()->hasClass('form-horizontal') ? 'col-sm-offset-3 col-sm-9' : '') . '">';
64
            foreach ($buttons as $button) {
65
                $return .= $button->render() . "\n";
66
            }
67
            $return .= '</div>';
68
            $return .= '</div>';
69
        }
70
71
        return $return;
72
    }
73
74
    /**
75
     * @param $label
76
     * @param bool $required
77
     * @param bool $error
78
     * @return string
79
     */
80
    public function renderLabel($label, $required = false, $error = false)
81
    {
82
        if (is_object($label)) {
83
            $element = $label;
84
            $label = $element->getLabel();
85
            $required = $element->isRequired();
86
            $error = $element->isError();
87
        }
88
89
        $return = '<label class="col-form-label ' . ($this->getForm()->hasClass('form-horizontal') ? ' col-sm-3' : '') . ($error ? '' : '') . '">';
90
        $return .= $label . ':';
91
92
        if ($required) {
93
            $return .= '<span class="required">*</span>';
94
        }
95
96
        $return .= "</label>";
97
98
        return $return;
99
    }
100
101
102
    /**
103
     * @inheritDoc
104
     */
105
    protected function getLabelClassesForElement($element)
106
    {
107
        $classes = parent::getLabelClassesForElement($element);
108
        $classes[] = 'col-form-label';
109
        if ($this->getForm()->hasClass('form-horizontal')) {
110
            $classes[] = 'col-sm-3';
111
        }
112
        return $classes;
113
    }
114
}
115