Passed
Push — master ( d26101...1eb4af )
by Gabor
03:11
created

RadioElement   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 148
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 12
loc 148
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1
ccs 52
cts 52
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setOptions() 0 14 3
A setValue() 0 15 3
B getValuesToSelect() 0 16 5
A getValue() 12 12 3
A setOption() 0 11 1
A hasOptions() 0 4 1
A getOptions() 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
use WebHemi\Form\Element\MultiOptionElementInterface;
15
16
/**
17
 * Class RadioElement.
18
 */
19
class RadioElement extends AbstractElement implements MultiOptionElementInterface
20
{
21
    /** @var string */
22
    protected $type = 'radio';
23
    /** @var array */
24
    protected $options = [];
25
    /** @var array */
26
    protected $optionGroups = [];
27
28
    /**
29
     * RadioElement constructor.
30
     *
31
     * @param string $name
32
     * @param string $label
33
     * @param mixed  $value
34
     */
35 12
    public function __construct($name = '', $label = '', $value = null)
36
    {
37 12
        parent::__construct($name, $label, $value);
38
39 12
        $this->setTabIndex();
40 12
    }
41
42
    /**
43
     * Sets element value.
44
     *
45
     * @param mixed $value
46
     * @return RadioElement
47
     */
48 2
    public function setValue($value)
49
    {
50 2
        if (!is_array($value)) {
51 2
            $value = [$value];
52 2
        }
53
54 2
        $valuesToSelect = $this->getValuesToSelect($value);
55
56
        // Go through the options and change the defaults.
57 2
        foreach ($this->options as &$option) {
58 2
            $option['checked'] = in_array($option['value'], $valuesToSelect);
59 2
        }
60
61 2
        return $this;
62
    }
63
64
    /**
65
     * Collects the selected values for multi option element.
66
     *
67
     * @param $value
68
     * @return array
69
     */
70 4
    protected function getValuesToSelect($value)
71
    {
72 4
        $isAssociativeArray = array_keys($value) !== range(0, count($value) - 1);
73 4
        $valuesToSelect = [];
74
75
        // Go through the given data and collect the selected ones.
76 4
        foreach ($value as $key => $data) {
77 4
            if ($isAssociativeArray && $data == 1) {
78 1
                $valuesToSelect[] = $key;
79 4
            } elseif (!$isAssociativeArray) {
80 4
                $valuesToSelect[] = $data;
81 4
            }
82 4
        }
83
84 4
        return $valuesToSelect;
85
    }
86
87
    /**
88
     * Returns element value.
89
     *
90
     * @return mixed
91
     */
92 2 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...
93
    {
94 2
        $selectedValues = [];
95
96 2
        foreach ($this->options as $option) {
97 2
            if ($option['checked']) {
98 2
                $selectedValues[] = $option['value'];
99 2
            }
100 2
        }
101
102 2
        return $selectedValues;
103
    }
104
105
    /**
106
     * Set label-value options for the element.
107
     *
108
     * @param array $options
109
     * @return RadioElement
110
     */
111 11
    public function setOptions(array $options)
112
    {
113
        /** @var MultiOptionElementInterface $this */
114 11
        $this->options = [];
115 11
        $this->optionGroups = [];
116
117 11
        foreach ($options as $option) {
118 11
            $checked = !empty($option['checked']);
119 11
            $group = !empty($option['group']) ? $option['group'] : 'Default';
120 11
            $this->setOption($option['label'], $option['value'], $checked, $group);
121 11
        }
122
123 11
        return $this;
124
    }
125
126
    /**
127
     * Sets label-value option for the element.
128
     *
129
     * @param string  $label
130
     * @param string  $value
131
     * @param boolean $checked
132
     * @param string  $group
133
     * @return RadioElement
134
     */
135 2
    protected function setOption($label, $value, $checked, $group)
136
    {
137 2
        $this->options[$label] = [
138 2
            'label' => $label,
139 2
            'value' => $value,
140 2
            'checked' => $checked,
141
            'group' => $group
142 2
        ];
143
144 2
        return $this;
145
    }
146
147
    /**
148
     * Checks if the element has value options.
149
     *
150
     * @return bool
151
     */
152 1
    public function hasOptions()
153
    {
154 1
        return !empty($this->options);
155
    }
156
157
    /**
158
     * Gets element value options.
159
     *
160
     * @return array
161
     */
162 1
    public function getOptions()
163
    {
164 1
        return $this->options;
165
    }
166
}
167