Failed Conditions
Push — psr2-config ( c6639e )
by Andreas
06:39 queued 03:33
created

SettingMulticheckbox::html()   F

Complexity

Conditions 16
Paths 792

Size

Total Lines 68
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 42
nc 792
nop 2
dl 0
loc 68
rs 2.9411
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * Class setting_multicheckbox
7
 */
8
class SettingMulticheckbox extends SettingString {
9
10
    protected $_choices = array();
11
    protected $_combine = array();
12
    protected $_other = 'always';
13
14
    /**
15
     * update changed setting with user provided value $input
16
     * - if changed value fails error check, save it to $this->_input (to allow echoing later)
17
     * - if changed value passes error check, set $this->_local to the new value
18
     *
19
     * @param  mixed $input the new value
20
     * @return boolean          true if changed, false otherwise (also on error)
21
     */
22
    public function update($input) {
23
        if($this->is_protected()) return false;
24
25
        // split any combined values + convert from array to comma separated string
26
        $input = ($input) ? $input : array();
27
        $input = $this->_array2str($input);
28
29
        $value = is_null($this->_local) ? $this->_default : $this->_local;
30
        if($value == $input) return false;
31
32
        if($this->_pattern && !preg_match($this->_pattern, $input)) {
33
            $this->_error = true;
34
            $this->_input = $input;
35
            return false;
36
        }
37
38
        $this->_local = $input;
39
        return true;
40
    }
41
42
    /**
43
     * Build html for label and input of setting
44
     *
45
     * @param \admin_plugin_config $plugin object of config plugin
46
     * @param bool $echo true: show input value, when error occurred, otherwise the stored setting
47
     * @return string[] with content array(string $label_html, string $input_html)
48
     */
49
    public function html(\admin_plugin_config $plugin, $echo = false) {
50
51
        $disable = '';
52
53
        if($this->is_protected()) {
54
            $value = $this->_protected;
55
            $disable = 'disabled="disabled"';
56
        } else {
57
            if($echo && $this->_error) {
58
                $value = $this->_input;
59
            } else {
60
                $value = is_null($this->_local) ? $this->_default : $this->_local;
61
            }
62
        }
63
64
        $key = htmlspecialchars($this->_key);
65
66
        // convert from comma separated list into array + combine complimentary actions
67
        $value = $this->_str2array($value);
68
        $default = $this->_str2array($this->_default);
69
70
        $input = '';
71
        foreach($this->_choices as $choice) {
72
            $idx = array_search($choice, $value);
73
            $idx_default = array_search($choice, $default);
74
75
            $checked = ($idx !== false) ? 'checked="checked"' : '';
76
77
            // @todo ideally this would be handled using a second class of "default"
78
            $class = (($idx !== false) == (false !== $idx_default)) ? " selectiondefault" : "";
79
80
            $prompt = ($plugin->getLang($this->_key . '_' . $choice) ?
81
                $plugin->getLang($this->_key . '_' . $choice) : htmlspecialchars($choice));
82
83
            $input .= '<div class="selection' . $class . '">' . "\n";
84
            $input .= '<label for="config___' . $key . '_' . $choice . '">' . $prompt . "</label>\n";
85
            $input .= '<input id="config___' . $key . '_' . $choice . '" name="config[' . $key .
86
                '][]" type="checkbox" class="checkbox" value="' . $choice . '" ' . $disable . ' ' . $checked . "/>\n";
87
            $input .= "</div>\n";
88
89
            // remove this action from the disabledactions array
90
            if($idx !== false) unset($value[$idx]);
91
            if($idx_default !== false) unset($default[$idx_default]);
92
        }
93
94
        // handle any remaining values
95
        if($this->_other != 'never') {
96
            $other = join(',', $value);
97
            // test equivalent to ($this->_other == 'always' || ($other && $this->_other == 'exists')
98
            // use != 'exists' rather than == 'always' to ensure invalid values default to 'always'
99
            if($this->_other != 'exists' || $other) {
100
101
                $class = (
102
                    (count($default) == count($value)) &&
103
                    (count($value) == count(array_intersect($value, $default)))
104
                ) ?
105
                    " selectiondefault" : "";
106
107
                $input .= '<div class="other' . $class . '">' . "\n";
108
                $input .= '<label for="config___' . $key . '_other">' . $plugin->getLang($key . '_other') . "</label>\n";
109
                $input .= '<input id="config___' . $key . '_other" name="config[' . $key .
110
                    '][other]" type="text" class="edit" value="' . htmlspecialchars($other) . '" ' . $disable . " />\n";
111
                $input .= "</div>\n";
112
            }
113
        }
114
        $label = '<label>' . $this->prompt($plugin) . '</label>';
115
        return array($label, $input);
116
    }
117
118
    /**
119
     * convert comma separated list to an array and combine any complimentary values
120
     *
121
     * @param string $str
122
     * @return array
123
     */
124
    protected function _str2array($str) {
125
        $array = explode(',', $str);
126
127
        if(!empty($this->_combine)) {
128
            foreach($this->_combine as $key => $combinators) {
129
                $idx = array();
130
                foreach($combinators as $val) {
131
                    if(($idx[] = array_search($val, $array)) === false) break;
132
                }
133
134
                if(count($idx) && $idx[count($idx) - 1] !== false) {
135
                    foreach($idx as $i) unset($array[$i]);
136
                    $array[] = $key;
137
                }
138
            }
139
        }
140
141
        return $array;
142
    }
143
144
    /**
145
     * convert array of values + other back to a comma separated list, incl. splitting any combined values
146
     *
147
     * @param array $input
148
     * @return string
149
     */
150
    protected function _array2str($input) {
151
152
        // handle other
153
        $other = trim($input['other']);
154
        $other = !empty($other) ? explode(',', str_replace(' ', '', $input['other'])) : array();
155
        unset($input['other']);
156
157
        $array = array_unique(array_merge($input, $other));
158
159
        // deconstruct any combinations
160
        if(!empty($this->_combine)) {
161
            foreach($this->_combine as $key => $combinators) {
162
163
                $idx = array_search($key, $array);
164
                if($idx !== false) {
165
                    unset($array[$idx]);
166
                    $array = array_merge($array, $combinators);
167
                }
168
            }
169
        }
170
171
        return join(',', array_unique($array));
172
    }
173
}
174