Completed
Push — master ( 73de08...39d3cf )
by Gabor
03:53
created

SelectElement   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 136
Duplicated Lines 15.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 21
loc 136
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 4 1
A getName() 0 13 4
C setValue() 7 26 8
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
    /**
20
     * SelectElement constructor.
21
     *
22
     * @param string $name
23
     * @param string $label
24
     * @param mixed  $value
25
     */
26
    public function __construct($name = '', $label = '', $value = null)
27
    {
28
        parent::__construct($name, $label, $value);
29
30
        $this->setTabIndex();
31
    }
32
33
    /**
34
     * Returns the element type.
35
     *
36
     * @return string
37
     */
38
    public function getType()
39
    {
40
        return 'select';
41
    }
42
43
    /**
44
     * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well.
45
     *
46
     * @param boolean $getFulNodeName
47
     * @return string
48
     */
49
    public function getName($getFulNodeName = true)
50
    {
51
        $name = parent::getName($getFulNodeName);
52
53
        if ($getFulNodeName
54
            && count($this->options) > 1
55
            && !empty($this->attributes['multiple'])
56
        ) {
57
            $name .= '[]';
58
        }
59
60
        return $name;
61
    }
62
63
    /**
64
     * Sets element value.
65
     *
66
     * @param mixed $value
67
     * @return SelectElement
68
     */
69
    public function setValue($value)
70
    {
71
        if (!is_array($value)) {
72
            $value = [$value];
73
        }
74
75
        $isAssociativeArray = array_keys($value) !== range(0, count($value) - 1);
76
        $valuesToSelect = [];
77
78
        // Go through the given data and collect the selected ones.
79 View Code Duplication
        foreach ($value as $key => $data) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
            if ($isAssociativeArray && $data == 1) {
81
                $valuesToSelect[] = $key;
82
            } elseif (!$isAssociativeArray) {
83
                $valuesToSelect[] = $data;
84
            }
85
        }
86
87
        foreach ($this->options as $group => $options) {
88
            foreach ($options as $index => $option) {
89
                $this->options[$group][$index]['checked'] = in_array($option['value'], $valuesToSelect);
90
            }
91
        }
92
93
        return $this;
94
    }
95
96
    /**
97
     * Returns element value.
98
     *
99
     * @return mixed
100
     */
101 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...
102
    {
103
        $selectedValues = [];
104
105
        foreach ($this->options as $options) {
106
            foreach ($options as $option) {
107
                if ($option['checked']) {
108
                    $selectedValues[] = $option['value'];
109
                }
110
            }
111
        }
112
113
        return $selectedValues;
114
    }
115
116
    /**
117
     * Sets label-value option for the element.
118
     *
119
     * @param string  $label
120
     * @param string  $value
121
     * @param boolean $checked
122
     * @param string  $group
123
     * @return SelectElement
124
     */
125
    protected function setOption($label, $value, $checked, $group)
126
    {
127
        // For <select> tag, the option grouping is allowed.
128
        if (!isset($this->options[$group])) {
129
            $this->options[$group] = [];
130
        }
131
132
        $this->optionGroups[$group] = $group;
133
134
        $this->options[$group][$label] = [
135
            'label' => $label,
136
            'value' => $value,
137
            'checked' => $checked
138
        ];
139
140
        return $this;
141
    }
142
143
    /**
144
     * Checks if the Select box has groupped options.
145
     *
146
     * @return bool
147
     */
148
    public function isGroupedSelect()
149
    {
150
        return count($this->optionGroups) > 1;
151
    }
152
}
153