Failed Conditions
Push — psr2-config ( de33cd...0a5b05 )
by Andreas
04:27
created

SettingEmail::update()   C

Complexity

Conditions 11
Paths 46

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 46
nop 1
dl 0
loc 45
rs 5.2653
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_email
7
 */
8
class SettingEmail extends SettingString {
9
    protected $multiple = false;
10
    protected $placeholders = false;
11
12
    /** @inheritdoc */
13
    public function update($input) {
14
        if(is_null($input)) return false;
15
        if($this->isProtected()) return false;
16
17
        $value = is_null($this->local) ? $this->default : $this->local;
18
        if($value == $input) return false;
19
        if($input === '') {
20
            $this->local = $input;
21
            return true;
22
        }
23
        $mail = $input;
24
25
        if($this->placeholders) {
26
            // replace variables with pseudo values
27
            $mail = str_replace('@USER@', 'joe', $mail);
28
            $mail = str_replace('@NAME@', 'Joe Schmoe', $mail);
29
            $mail = str_replace('@MAIL@', '[email protected]', $mail);
30
        }
31
32
        // multiple mail addresses?
33
        if($this->multiple) {
34
            $mails = array_filter(array_map('trim', explode(',', $mail)));
35
        } else {
36
            $mails = array($mail);
37
        }
38
39
        // check them all
40
        foreach($mails as $mail) {
41
            // only check the address part
42
            if(preg_match('#(.*?)<(.*?)>#', $mail, $matches)) {
43
                $addr = $matches[2];
44
            } else {
45
                $addr = $mail;
46
            }
47
48
            if(!mail_isvalid($addr)) {
49
                $this->error = true;
50
                $this->input = $input;
51
                return false;
52
            }
53
        }
54
55
        $this->local = $input;
56
        return true;
57
    }
58
}
59