Passed
Branch master (41990d)
by Gabor
03:31
created

SelectElement   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 14
loc 105
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 13 4
A setValue() 0 16 4
A getValue() 14 14 4
A setOption() 0 17 2
A isGroupedSelect() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form\Element\Web;
13
14
/**
15
 * Class SelectElement.
16
 */
17
class SelectElement extends RadioElement
18
{
19
    /** @var string */
20
    protected $type = 'select';
21
22
    /**
23
     * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well.
24
     *
25
     * @param boolean $getFulNodeName
26
     * @return string
27
     */
28 9
    public function getName($getFulNodeName = true)
29
    {
30 9
        $name = parent::getName($getFulNodeName);
31
32
        if ($getFulNodeName
33 9
            && count($this->options) > 1
34 9
            && !empty($this->attributes['multiple'])
35 9
        ) {
36 1
            $name .= '[]';
37 1
        }
38
39 9
        return $name;
40
    }
41
42
    /**
43
     * Sets element value.
44
     *
45
     * @param mixed $value
46
     * @return SelectElement
47
     */
48 2
    public function setValue($value)
49
    {
50 2
        if (!is_array($value)) {
51 1
            $value = [$value];
52 1
        }
53
54 2
        $valuesToSelect = $this->getValuesToSelect($value);
55
56 2
        foreach ($this->options as $group => $options) {
57 2
            foreach ($options as $index => $option) {
58 2
                $this->options[$group][$index]['checked'] = in_array($option['value'], $valuesToSelect);
59 2
            }
60 2
        }
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * Returns element value.
67
     *
68
     * @return mixed
69
     */
70 1 View Code Duplication
    public function getValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 1
        $selectedValues = [];
73
74 1
        foreach ($this->options as $options) {
75 1
            foreach ($options as $option) {
76 1
                if ($option['checked']) {
77 1
                    $selectedValues[] = $option['value'];
78 1
                }
79 1
            }
80 1
        }
81
82 1
        return $selectedValues;
83
    }
84
85
    /**
86
     * Sets label-value option for the element.
87
     *
88
     * @param string  $label
89
     * @param string  $value
90
     * @param boolean $checked
91
     * @param string  $group
92
     * @return SelectElement
93
     */
94 9
    protected function setOption($label, $value, $checked, $group)
95
    {
96
        // For <select> tag, the option grouping is allowed.
97 9
        if (!isset($this->options[$group])) {
98 9
            $this->options[$group] = [];
99 9
        }
100
101 9
        $this->optionGroups[$group] = $group;
102
103 9
        $this->options[$group][$label] = [
104 9
            'label' => $label,
105 9
            'value' => $value,
106
            'checked' => $checked
107 9
        ];
108
109 9
        return $this;
110
    }
111
112
    /**
113
     * Checks if the Select box has groupped options.
114
     *
115
     * @return bool
116
     */
117 1
    public function isGroupedSelect()
118
    {
119 1
        return count($this->optionGroups) > 1;
120
    }
121
}
122