Email   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 133
Duplicated Lines 9.02 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 12
loc 133
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
B doValidate() 0 28 8
A setDnsCheck() 0 6 1
A validateDomainPart() 0 20 5
B validateLocalPart() 12 32 8

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
namespace Fwlib\Validator\Constraint;
3
4
use Fwlib\Util\UtilContainerAwareTrait;
5
use Fwlib\Validator\AbstractConstraint;
6
7
/**
8
 * Constraint Email
9
 *
10
 * @copyright   Copyright 2013-2015 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
class Email extends AbstractConstraint
14
{
15
    use UtilContainerAwareTrait;
16
17
18
    /**
19
     * Check email domain through dns
20
     *
21
     * @var bool
22
     */
23
    protected $dnsCheck = false;
24
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $messageTemplates = [
30
        'default'   => 'The input should be valid email address'
31
    ];
32
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @link http://www.linuxjournal.com/article/9585
38
     */
39
    protected function doValidate($value)
40
    {
41
        $atIndex = strrpos($value, '@');
42
        if (false === $atIndex) {
43
            return false;
44
        }
45
46
        $domain = substr($value, $atIndex + 1);
47
        $local = substr($value, 0, $atIndex);
48
49
        $valid = $this->validateDomainPart($domain) &&
50
            $this->validateLocalPart($local);
51
52
        // Some network provider will return fake A record if a dns query
53
        // return fail, usually display some ads, so we only check MX record.
54
        if ($valid && $this->dnsCheck &&
55
            $this->getUtilContainer()->getEnv()->isNixOs() &&
56
            !checkdnsrr($domain, 'MX')
57
        ) {
58
            $valid = false;
59
        }
60
61
        if (!$valid) {
62
            $this->setMessage('default');
63
        }
64
65
        return $valid;
66
    }
67
68
69
    /**
70
     * Setter of $dnsCheck
71
     *
72
     * @param   boolean $dnsCheck
73
     * @return  static
74
     */
75
    public function setDnsCheck($dnsCheck)
76
    {
77
        $this->dnsCheck = $dnsCheck;
78
79
        return $this;
80
    }
81
82
83
    /**
84
     * @param   string  $domain
85
     * @return  bool
86
     */
87
    protected function validateDomainPart($domain)
88
    {
89
        $valid = true;
90
        $length = strlen($domain);
91
92
        if ($length < 1 || $length > 255) {
93
            // domain part length exceeded
94
            $valid = false;
95
96
        } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
97
            // character not valid in domain part
98
            $valid = false;
99
100
        } elseif (preg_match('/\\.\\./', $domain)) {
101
            // domain part has two consecutive dots
102
            $valid = false;
103
        }
104
105
        return $valid;
106
    }
107
108
109
    /**
110
     * @param   string  $local
111
     * @return  bool
112
     */
113
    protected function validateLocalPart($local)
114
    {
115
        $valid = true;
116
        $length = strlen($local);
117
118
        if ($length < 1 || $length > 64) {
119
            // local part length exceeded
120
            $valid = false;
121
122
        } elseif ($local[0] == '.' || $local[$length-1] == '.') {
123
            // local part starts or ends with '.'
124
            $valid = false;
125
126
        } elseif (preg_match('/\\.\\./', $local)) {
127
            // local part has two consecutive dots
128
            $valid = false;
129
130 View Code Duplication
        } elseif (!preg_match(
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...
131
            '/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
132
            str_replace("\\\\", "", $local)
133
        )) {
134
            // Character not valid in local part unless local part is quoted
135
            if (!preg_match(
136
                '/^"(\\\\"|[^"])+"$/',
137
                str_replace("\\\\", "", $local)
138
            )) {
139
                $valid = false;
140
            }
141
        }
142
143
        return $valid;
144
    }
145
}
146