Passed
Push — master ( ab2dc9...db3995 )
by Gabriel
14:15 queued 10s
created

generateElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
class Nip_Form_Renderer_Elements_Select extends \Nip\Form\Renderer\Elements\AbstractElementRenderer
4
{
5
    /**
6
     * @return string
7
     */
8
    public function generateElement()
9
    {
10
        $return = '<select ';
11
        $return .= $this->renderAttributes();
12
        $return .= ' >' . $this->renderOptions() . '</select>';
13
        return $return;
14
    }
15
16
    public function renderOptions($options = false)
17
    {
18
        $options = $options ? $options : $this->getElement()->getOptions();
19
        $return = '';
20
21
        $selectedValue = $this->getElement()->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $selectedValue is correct as $this->getElement()->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() 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...
22
23
        foreach ($options as $value=>$atribs) {
24
            if (is_string($value) && !isset($atribs['label'])) {
25
                $return .= '<optgroup label="' . $value . '">';
26
                $return .= $this->renderOptions($atribs);
27
                $return .= '</optgroup>';
28
            } else {
29
                $return .= '<option';
30
31
                $label = $atribs['label'];
32
                unset($atribs['label']);
33
34
                $atribs['value'] = $value;
35
                if ($this->isOptionSelected($value, $selectedValue)) {
36
                    $atribs['selected'] = 'selected';
37
                }
38
39
                foreach ($atribs as $name=>$value) {
0 ignored issues
show
Comprehensibility Bug introduced by
$value is overwriting a variable from outer foreach loop.
Loading history...
40
                    $return .= ' ' . $name . '="' . $value . '"';
41
                }
42
                $return .= '>' . $label . '</option>';
43
            }
44
        }
45
        return $return;
46
    }
47
48
    /**
49
     * @param $value
50
     * @param $selectedValue
51
     * @return bool
52
     */
53
    protected function isOptionSelected($value, $selectedValue): bool
54
    {
55
        if (is_null($selectedValue)) {
56
            return false;
57
        }
58
        if ($selectedValue === 0 or $value === 0) {
59
            if ($value === $selectedValue) {
60
                return true;
61
            }
62
        } elseif ($selectedValue == $value) {
63
            return true;
64
        }
65
        return false;
66
    }
67
}
68