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

renderAttributes()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.1429

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 21
ccs 12
cts 14
cp 0.8571
rs 8.8333
cc 7
nc 12
nop 1
crap 7.1429
1
<?php
2
3
use Nip\Form\Renderer\AbstractRenderer;
4
5
use Nip\Form\Elements\AbstractElement;
6
7
abstract class Nip_Form_Renderer_Elements_Abstract
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
    protected $_renderer;
10
    protected $_element;
11
12
    /**
13
     * @return AbstractRenderer
14
     */
15 3
    public function getRenderer()
16
    {
17 3
        return $this->_renderer;
18
    }
19
20
    /**
21
     * @param AbstractRenderer $renderer
22
     * @return $this
23
     */
24 4
    public function setRenderer(AbstractRenderer $renderer)
25
    {
26 4
        $this->_renderer = $renderer;
27
28 4
        return $this;
29
    }
30
31
    /**
32
     * @return string
33
     */
34 5
    public function render()
35
    {
36 5
        $return = '';
37 5
        $return .= $this->renderElement();
38
39 5
        $renderErrors = $this->getElement()->getForm()->getOption('render_input_errors');
40 5
        if ($renderErrors !== false) {
41 5
            $return .= $this->renderErrors();
42
        }
43 5
        $this->getElement()->setRendered(true);
44
45 5
        return $return;
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51 5
    public function renderElement()
52
    {
53 5
        $return = $this->renderDecorators($this->generateElement(), 'element');
0 ignored issues
show
Bug introduced by
'element' of type string is incompatible with the type boolean expected by parameter $position of Nip_Form_Renderer_Elemen...act::renderDecorators(). ( Ignorable by Annotation )

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

53
        $return = $this->renderDecorators($this->generateElement(), /** @scrutinizer ignore-type */ 'element');
Loading history...
Bug introduced by
Are you sure the usage of $this->generateElement() targeting Nip_Form_Renderer_Elemen...ract::generateElement() 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...
54 5
        $this->getElement()->setRendered(true);
55
56 5
        return $return;
57
    }
58
59
    /**
60
     * @param $return
61
     * @param bool $position
62
     * @return mixed
63
     */
64 5
    public function renderDecorators($return, $position = false)
65
    {
66 5
        if ($position) {
67 5
            $decorators = $this->getElement()->getDecoratorsByPosition($position);
68 5
            if (is_array($decorators)) {
69
                foreach ($decorators as $decorator) {
70
                    $return = $decorator->render($return);
71
                }
72
            }
73
        }
74
75 5
        return $return;
76
    }
77
78
    /**
79
     * @return Nip_Form_Element_Abstract
80
     */
81 5
    public function getElement()
82
    {
83 5
        return $this->_element;
84
    }
85
86 5
    public function setElement(AbstractElement $element)
87
    {
88 5
        $this->_element = $element;
89
90 5
        return $this;
91
    }
92
93
    public function generateElement()
94
    {
95
        return;
96
    }
97
98 5
    public function renderErrors()
99
    {
100 5
        $return = '';
101 5
        if ($this->getElement()->isError() && $this->getElement()->getForm()->getOption('renderElementErrors') !== false) {
102
            $errors = $this->getElement()->getErrors();
103
            $errors_string = implode('<br />', $errors);
104
            $return .= '<span class="help-inline">'.$errors_string.'</span>';
105
        }
106
107 5
        return $return;
108
    }
109
110
    /**
111
     * @param array $overrides
112
     * @return string
113
     */
114 3
    public function renderAttributes($overrides = [])
115
    {
116 3
        $attribs = $this->getElement()->getAttribs();
117 3
        if (!isset($attribs['title'])) {
118 3
            $attribs['title'] = $this->getElement()->getLabel();
119
        }
120 3
        $elementAttribs = $this->getElementAttribs();
121 3
        $return = '';
122 3
        foreach ($attribs as $name => $value) {
123 3
            if (in_array($name, $elementAttribs)) {
124 3
                if (in_array($name, array_keys($overrides))) {
125
                    $value = $overrides[$name];
126
                }
127 3
                if ($name == "name" && $this->getElement()->isGroup()) {
128
                    $value = $value."[]";
129
                }
130 3
                $return .= ' '.$name.'="'.$value.'"';
131
            }
132
        }
133
134 3
        return $return;
135
    }
136
137
    /**
138
     * @return array
139
     */
140 3
    public function getElementAttribs()
141
    {
142 3
        return ['id', 'name', 'style', 'class', 'title', 'readonly', 'disabled'];
143
    }
144
}
145