Passed
Push — master ( b8e171...9e4d7b )
by Gabriel
04:14 queued 12s
created

Nip_Form_Renderer_Bootstrap::renderLabel()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 12
cp 0
rs 9.6111
cc 5
nc 16
nop 3
crap 30

1 Method

Rating   Name   Duplication   Size   Complexity  
A Nip_Form_Renderer_Bootstrap::renderElement() 0 5 1
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
    /**
10
     * @return string
11
     */
12
    public function renderElements()
13
    {
14
        $return = '';
15
16
        $renderRows = $this->renderRows();
17
        if ($renderRows) {
18
            $return .= $renderRows;
19
        }
20
21
        return $return;
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    public function renderRows()
28
    {
29
        $elements = $this->getElements();
30
        $return = '';
31
        foreach ($elements as $element) {
32
            $return .= $this->renderRow($element);
33
        }
34
35
        return $return;
36
    }
37
38
    /**
39
     * @param Nip\Form\Elements\AbstractElement $element
40
     * @return string
41
     */
42 1
    public function renderRow($element)
43
    {
44 1
        $return = '';
45 1
        if (!$element->isRendered()) {
46 1
            if ($element->hasCustomRenderer()) {
47
                return $element->render();
48
            }
49
50 1
            $return .= '<div class="form-group row-' . $element->getUniqueId() . ($element->isError() ? ' has-error' : '') . '">';
51
52 1
            if ($element->isRenderLabel()) {
53 1
                $return .= $this->renderElementLabel($element);
54
            }
55
56 1
            $class = '';
57 1
            if ($this->getForm()->hasClass('form-horizontal')) {
58
                $class = $element->getType() == 'checkbox' ? 'col-sm-offset-3 col-sm-9' : 'col-sm-9';
59
            }
60
61 1
            $return .= $class ? '<div class="' . $class . '">' : '';
62 1
            $return .= $this->renderElement($element);
63
64 1
            $helpBlock = $element->getOption('form-help');
0 ignored issues
show
Bug introduced by
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...
65 1
            if ($helpBlock) {
0 ignored issues
show
introduced by
$helpBlock is of type null, thus it always evaluated to false.
Loading history...
66
                $return .= '<span class="help-block">' . $helpBlock . '</span>';
67
            }
68
69 1
            $return .= $element->renderErrors();
70 1
            $return .= $class ? '</div>' : '';
71 1
            $return .= '</div>';
72
        }
73
74 1
        return $return;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 1
    protected function getLabelClassesForElement($element)
81
    {
82 1
        $classes = parent::getLabelClassesForElement($element);
83 1
        $classes[] = 'control-label';
84 1
        if ($this->getForm()->hasClass('form-horizontal')) {
85
            $classes[] = 'col-sm-3';
86
        }
87 1
        return $classes;
88
    }
89
90
    /**
91
     * @param AbstractElement $element
92
     * @return mixed
93
     */
94 1
    public function renderElement(AbstractElement $element)
95
    {
96 1
        $element->addClass('form-control');
97
98 1
        return parent::renderElement($element);
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function renderButtons()
105
    {
106
        $return = '';
107
        $buttons = $this->getForm()->getButtons();
108
109
        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...
110
            $return .= '<div class="form-group">
111
                            <div class="' . ($this->getForm()->hasClass('form-horizontal') ? 'col-sm-offset-3 col-sm-9' : '') . '">';
112
            foreach ($buttons as $button) {
113
                $return .= $button->render() . "\n";
114
            }
115
            $return .= '</div>';
116
            $return .= '</div>';
117
        }
118
119
        return $return;
120
    }
121
}
122