Failed Conditions
Pull Request — psr2 (#2382)
by Andreas
08:02 queued 12s
created

SettingMultichoice::html()   C

Complexity

Conditions 11
Paths 108

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
nc 108
nop 2
dl 0
loc 44
rs 5.1372
c 0
b 0
f 0

How to fix   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\Setting;
4
5
/**
6
 * Class setting_multichoice
7
 */
8
class SettingMultichoice extends SettingString {
9
    protected $choices = array();
10
    public $lang; //some custom language strings are stored in setting
11
12
    /** @inheritdoc */
13
    public function html(\admin_plugin_config $plugin, $echo = false) {
14
        $disable = '';
15
        $nochoice = '';
16
17
        if($this->isProtected()) {
18
            $value = $this->protected;
19
            $disable = ' disabled="disabled"';
20
        } else {
21
            $value = is_null($this->local) ? $this->default : $this->local;
22
        }
23
24
        // ensure current value is included
25
        if(!in_array($value, $this->choices)) {
26
            $this->choices[] = $value;
27
        }
28
        // disable if no other choices
29
        if(!$this->isProtected() && count($this->choices) <= 1) {
30
            $disable = ' disabled="disabled"';
31
            $nochoice = $plugin->getLang('nochoice');
32
        }
33
34
        $key = htmlspecialchars($this->key);
35
36
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
37
38
        $input = "<div class=\"input\">\n";
39
        $input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
40
        foreach($this->choices as $choice) {
41
            $selected = ($value == $choice) ? ' selected="selected"' : '';
42
            $option = $plugin->getLang($this->key . '_o_' . $choice);
43
            if(!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
44
                $option = $this->lang[$this->key . '_o_' . $choice];
45
            }
46
            if(!$option) $option = $choice;
47
48
            $choice = htmlspecialchars($choice);
49
            $option = htmlspecialchars($option);
50
            $input .= '  <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
51
        }
52
        $input .= "</select> $nochoice \n";
53
        $input .= "</div>\n";
54
55
        return array($label, $input);
56
    }
57
58
    /** @inheritdoc */
59
    public function update($input) {
60
        if(is_null($input)) return false;
61
        if($this->isProtected()) return false;
62
63
        $value = is_null($this->local) ? $this->default : $this->local;
64
        if($value == $input) return false;
65
66
        if(!in_array($input, $this->choices)) return false;
67
68
        $this->local = $input;
69
        return true;
70
    }
71
}
72