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

RadioElement   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 13.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 19
loc 143
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

8 Methods

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