Email::validate()   C
last analyzed

Complexity

Conditions 12
Paths 7

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 39
rs 5.1612
cc 12
eloc 21
nc 7
nop 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
 * Copyright (c) 2014-2016 Ryan Parman.
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * http://opensource.org/licenses/MIT
24
 */
25
26
namespace Skyzyx\StrongTypes\StringType;
27
28
use Exception;
29
use UnexpectedValueException;
30
use Skyzyx\StrongTypes\StringType;
31
32
/**
33
 * Not RFC-valid, but that's on purpose. In general, this validates e-mail addresses against the syntax in
34
 * RFC 822 (obsolete), with the exceptions that comments and whitespace folding are not supported. Also, IP address
35
 * hosts are explicitly disallowed.
36
 *
37
 * @see http://tools.ietf.org/html/rfc822
38
 * @see http://tools.ietf.org/html/rfc2822
39
 * @see http://tools.ietf.org/html/rfc5322
40
 * @see http://markonphp.com/properly-validate-email-address-php/
41
 */
42
class Email extends StringType
43
{
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function validate()
48
    {
49
        // @codeCoverageIgnoreStart
50
        if (!function_exists('idn_to_ascii') && !class_exists('\idna_convert')) {
51
            throw new Exception('Unable to convert punycode. Either install the PHP Intl extension, or the mabrahamde/idna-converter Composer package.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
52
        }
53
54
        // Use PHP Intl first
55
        if (function_exists('idn_to_ascii')) {
56
            $email = idn_to_ascii($this->value);
57
58
        // Fall back to the idna_convert class
59
        } elseif (class_exists('\idna_convert')) {
60
            $idn = new \idna_convert([
61
                'idn_version' => 2008
62
            ]);
63
64
            $email = $idn->encode($this->value);
65
66
        // Fall back to no conversion
67
        } else {
68
            $email = $this->value;
69
        }
70
        // @codeCoverageIgnoreEnd
71
72
        if (!(
73
            filter_var($email, FILTER_VALIDATE_EMAIL) && // RFC 822 (obsolete). Exceptions: no comments, no whitespace
74
            preg_match('/@.+\./', $email) &&             // Standard email formatting
0 ignored issues
show
Coding Style introduced by
Expected 1 space after logical operator; 13 found
Loading history...
75
            !preg_match('/@\[/', $email) &&              // Disallow IPv6 domains
0 ignored issues
show
Coding Style introduced by
Expected 1 space after logical operator; 14 found
Loading history...
76
            !preg_match('/".+@/', $email) &&             // Disallow quotes
0 ignored issues
show
Coding Style introduced by
Expected 1 space after logical operator; 13 found
Loading history...
77
            !preg_match('/=.+@/', $email) &&             // Disallow equals
0 ignored issues
show
Coding Style introduced by
Expected 1 space after logical operator; 13 found
Loading history...
78
            !preg_match('/localhost/i', $email) &&       // Disallow localhost
0 ignored issues
show
Coding Style introduced by
Expected 1 space after logical operator; 7 found
Loading history...
79
            !preg_match('/localdomain/i', $email)        // Disallow localdomain
80
        )) {
81
            throw new UnexpectedValueException(
82
                sprintf('The value "%s" is not a valid email address.', $this->value)
83
            );
84
        }
85
    }
86
}
87