Completed
Push — master ( 8431a9...0bccd4 )
by Gabriel
04:28
created

Nip_Form_Renderer_Bootstrap::renderRow()   B

Complexity

Conditions 8
Paths 26

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 21
cp 0
rs 8.4444
cc 8
nc 26
nop 1
crap 72
1
<?php
2
3
use Nip\Form\Renderer\AbstractRenderer;
4
5
use Nip\Form\Elements\AbstractElement;
6
7
class Nip_Form_Renderer_Bootstrap extends AbstractRenderer
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
    /**
11
     * @return string
12
     */
13
    public function renderElements()
14
    {
15
        $return = '';
16
17
        $renderRows = $this->renderRows();
18
        if ($renderRows) {
19
            $return .= $renderRows;
20
        }
21
22
        return $return;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function renderRows()
29
    {
30
        $elements = $this->getElements();
31
        $return = '';
32
        foreach ($elements as $element) {
33
            $return .= $this->renderRow($element);
34
        }
35
36
        return $return;
37
    }
38
39
    /**
40
     * @param Nip_Form_Element_Abstract $element
41
     * @return string
42
     */
43
    public function renderRow($element)
44
    {
45
        $return = '';
46
        if (!$element->isRendered()) {
47
            if ($element->hasCustomRenderer()) {
48
                return $element->render();
49
            }
50
51
            $return .= '<div class="form-group row-'.$element->getUniqueId().($element->isError() ? ' has-error' : '').'">';
52
53
            $renderLabel = $element->getOption('render_label');
0 ignored issues
show
Bug introduced by
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...
54
            if ($renderLabel !== false) {
0 ignored issues
show
introduced by
The condition $renderLabel !== false is always true.
Loading history...
55
                $return .= $this->renderLabel($element);
56
            }
57
58
            $class = '';
59
            if ($this->getForm()->hasClass('form-horizontal')) {
60
                $class = $element->getType() == 'checkbox' ? 'col-sm-offset-3 col-sm-9' : 'col-sm-9';
61
            }
62
63
            $return .= '<div class="'.$class.'">';
64
            $return .= $this->renderElement($element);
65
66
            $helpBlock = $element->getOption('form-help');
67
            if ($helpBlock) {
68
                $return .= '<span class="help-block">'.$helpBlock.'</span>';
69
            }
70
71
            $return .= $element->renderErrors();
72
            $return .= '</div>';
73
            $return .= '</div>';
74
        }
75
76
        return $return;
77
    }
78
79
    /**
80
     * @param $label
81
     * @param bool $required
82
     * @param bool $error
83
     * @return string
84
     */
85
    public function renderLabel($label, $required = false, $error = false)
86
    {
87
        if (is_object($label)) {
88
            $element = $label;
89
            $label = $element->getLabel();
90
            $required = $element->isRequired();
91
            $error = $element->isError();
92
        }
93
94
        $return = '<label class="control-label'.($this->getForm()->hasClass('form-horizontal') ? ' col-sm-3' : '').($error ? ' error' : '').'">';
95
        $return .= $label.':';
96
97
        if ($required) {
98
            $return .= '<span class="required">*</span>';
99
        }
100
101
        $return .= "</label>";
102
103
        return $return;
104
    }
105
106
    /**
107
     * @param AbstractElement $element
108
     * @return mixed
109
     */
110
    public function renderElement(AbstractElement $element)
111
    {
112
        $element->addClass('form-control');
113
114
        return $element->renderElement();
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function renderButtons()
121
    {
122
        $return = '';
123
        $buttons = $this->getForm()->getButtons();
124
125
        if ($buttons) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $buttons of type Nip_Form_Button_Abstract[] 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...
126
            $return .= '<div class="form-group">
127
                            <div class="'.($this->getForm()->hasClass('form-horizontal') ? 'col-sm-offset-3 col-sm-9' : '').'">';
128
            foreach ($buttons as $button) {
129
                $return .= $button->render()."\n";
130
            }
131
            $return .= '</div>';
132
            $return .= '</div>';
133
        }
134
135
        return $return;
136
    }
137
}
138