Completed
Branch FET-10766-extract-activation-d... (a650cc)
by
unknown
87:01 queued 68:06
created

Basic::validate()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 36
nc 10
nop 1
dl 0
loc 65
rs 6.2553
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 EventEspresso\core\domain\services\validation\email\strategies;
4
5
use EventEspresso\core\domain\services\validation\email\EmailValidationException;
6
use EventEspresso\core\domain\services\validation\email\EmailValidatorInterface;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
9
10
11
12
/**
13
 * Class Basic
14
 * Performs super basic email validation
15
 *
16
 * @package        Event Espresso
17
 * @author         Mike Nelson
18
 * @since          $VID:$
19
 */
20
class Basic implements EmailValidatorInterface
21
{
22
23
    /**
24
     * @param string $email_address
25
     * @return bool
26
     * @throws EmailValidationException
27
     */
28
    public function validate($email_address)
29
    {
30
        if (! preg_match('/^.+\@\S+$/', $email_address)) {
31
            // email not in correct {string}@{string} format
32
            throw new EmailValidationException(
33
                esc_html__('Email does not have the required @ sign.', 'event_espresso')
34
            );
35
        }
36
        $atIndex = $this->getAtIndex($email_address);
37
        $local = $this->getLocalPartOfEmail($email_address, $atIndex);
38
        $localLen = strlen($local);
39
        if ($localLen < 1) {
40
            //no local part
41
            throw new EmailValidationException(
42
                esc_html__('Email local-part (before the @) is required.', 'event_espresso')
43
            );
44
        }
45
        if ($localLen > 64) {
46
            // local part length exceeded
47
            throw new EmailValidationException(
48
                esc_html__('Email local-part (before the @) is too long.', 'event_espresso')
49
            );
50
        }
51
        if ($local[0] === '.') {
52
            // local part starts with '.'
53
            throw new EmailValidationException(
54
                esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso')
55
            );
56
        }
57
        if ($local[$localLen - 1] === '.') {
58
            // local part starts or ends with '.'
59
            throw new EmailValidationException(
60
                esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso')
61
            );
62
        }
63
        if (preg_match('/\\.\\./', $local)) {
64
            // local part has two consecutive dots
65
            throw new EmailValidationException(
66
                esc_html__(
67
                    'Email local-part (before the @) must not have two consecutive periods.',
68
                    'event_espresso'
69
                )
70
            );
71
        }
72
        $domain = $this->getDomainPartOfEmail($email_address, $atIndex);
73
        $domainLen = strlen($domain);
74
        if ($domainLen < 1) {
75
            throw new EmailValidationException(
76
                esc_html__('Email domain (after the @) is required.', 'event_espresso')
77
            );
78
        }
79
        if ($domainLen > 255) {
80
            // domain part length exceeded
81
            throw new EmailValidationException(
82
                esc_html__('Email domain (after the @) is too long.', 'event_espresso')
83
            );
84
        }
85
        if (preg_match('/\\.\\./', $domain)) {
86
            // domain part has two consecutive dots
87
            throw new EmailValidationException(
88
                esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso')
89
            );
90
        }
91
        return true;
92
    }
93
94
95
96
    /**
97
     * returns the location of the @ symbol
98
     *
99
     * @param string $email_address
100
     * @return bool|string
101
     */
102
    protected function getAtIndex($email_address)
103
    {
104
        return strrpos($email_address, '@');
105
    }
106
107
108
109
    /**
110
     * Gets the local part of the email
111
     *
112
     * @param string $email_address
113
     * @param bool|int $atIndex
114
     * @return bool|string
115
     */
116
    protected function getLocalPartOfEmail($email_address, $atIndex)
117
    {
118
        return substr($email_address, 0, $atIndex);
119
    }
120
121
122
123
    /**
124
     * Gets the domain part of the email
125
     *
126
     * @param string   $email_address
127
     * @param bool|int $atIndex
128
     * @return bool|string
129
     */
130
    protected function getDomainPartOfEmail($email_address, $atIndex)
131
    {
132
        return substr($email_address, $atIndex + 1);
133
    }
134
}
135
// End of file Basic.php
136
// Location: core\services\validation/Basic.php
137