Issues (138)

src/Renderer/Bootstrap.php (4 issues)

1
<?php
2
3
use Nip\Form\Elements\AbstractElement;
4
use Nip\Form\Renderer\AbstractRenderer;
5
6
class Nip_Form_Renderer_Bootstrap extends AbstractRenderer
7
{
8
    /**
9
     * @return string
10
     */
11
    public function renderElements()
12
    {
13
        $return = '';
14
15
        $renderRows = $this->renderRows();
16
        if ($renderRows) {
17
            $return .= $renderRows;
18
        }
19
20
        return $return;
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function renderRows()
27
    {
28
        $elements = $this->getElements();
29
        $return = '';
30
        foreach ($elements as $element) {
31
            $return .= $this->renderRow($element);
32
        }
33
34
        return $return;
35
    }
36
37
    /**
38
     * @param Nip\Form\Elements\AbstractElement $element
39
     * @return string
40
     */
41
    public function renderRow($element)
42
    {
43
        $return = '';
44
        if (!$element->isRendered()) {
45
            if ($element->hasCustomRenderer()) {
46
                return $element->render();
47
            }
48
49
            $return .= '<div class="form-group row-' . $element->getUniqueId() . ($element->isError() ? ' has-error' : '') . '">';
50
51
            if ($element->isRenderLabel()) {
52
                $return .= $this->renderElementLabel($element);
53
            }
54
55
            $class = '';
56
            if ($this->getForm()->hasClass('form-horizontal')) {
57
                $class = $element->getType() == 'checkbox' ? 'col-sm-offset-3 col-sm-9' : 'col-sm-9';
58
            }
59
60
            $return .= $class ? '<div class="' . $class . '">' : '';
61
            $return .= $this->renderElement($element);
62
63
            $helpBlock = $element->getOption('form-help');
0 ignored issues
show
Are you sure the assignment to $helpBlock is correct as $element->getOption('form-help') 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...
64
            if ($helpBlock) {
0 ignored issues
show
$helpBlock is of type null, thus it always evaluated to false.
Loading history...
65
                $return .= '<span class="help-block">' . $helpBlock . '</span>';
66
            }
67
68
            $return .= $element->renderErrors();
69
            $return .= $class ? '</div>' : '';
70
            $return .= '</div>';
71
        }
72
73
        return $return;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    protected function getLabelClassesForElement($element)
80
    {
81
        $classes = parent::getLabelClassesForElement($element);
82
        $classes[] = 'control-label';
83
        if ($this->getForm()->hasClass('form-horizontal')) {
84
            $classes[] = 'col-sm-3';
85
        }
86
        return $classes;
87
    }
88
89
    /**
90
     * @param AbstractElement $element
91
     * @return mixed
92
     */
93
    public function renderElement(AbstractElement $element)
94 1
    {
95
        $element->addClass('form-control');
96 1
97
        $output = parent::renderElement($element);
98 1
        if ($element instanceof Nip_Form_Element_Money) {
99
            $output = '<div class="input-group">'
100
                . $output
101
                . '<span class="input-group-text">' . $element->getOption('currency') . '</span>'
0 ignored issues
show
Are you sure the usage of $element->getOption('currency') 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 used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
102
                . '</div>';
103
        }
104
        return $output;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function renderButtons()
111
    {
112
        $return = '';
113
        $buttons = $this->getForm()->getButtons();
114
115
        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...
116
            $return .= '<div class="form-group">
117
                            <div class="' . ($this->getForm()->hasClass('form-horizontal') ? 'col-sm-offset-3 col-sm-9' : '') . '">';
118
            foreach ($buttons as $button) {
119
                $return .= $button->render() . "\n";
120
            }
121
            $return .= '</div>';
122
            $return .= '</div>';
123
        }
124
125
        return $return;
126
    }
127
}
128