Passed
Pull Request — master (#222)
by Alexander
04:26 queued 02:14
created

Email::validateValue()   C

Complexity

Conditions 16
Paths 96

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 16

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 35
nc 96
nop 2
dl 0
loc 60
ccs 29
cts 29
cp 1
crap 16
rs 5.5666
c 3
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
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\PreValidatableRuleInterface;
12
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 12 at column 27
Loading history...
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
15
use Yiisoft\Validator\ParametrizedRuleInterface;
16
use function function_exists;
17
18
/**
19
 * Validates that the value is a valid email address.
20
 */
21
#[Attribute(Attribute::TARGET_PROPERTY)]
22
final class Email implements ParametrizedRuleInterface, PreValidatableRuleInterface
23
{
24
    use HandlerClassNameTrait;
25
    use PreValidatableTrait;
26
    use RuleNameTrait;
27
28
    public function __construct(
29
        /**
30
         * @var string the regular expression used to validate value.
31
         *
32
         * @link http://www.regular-expressions.info/email.html
33
         */
34
        private string $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/',
35
        /**
36
         * @var string the regular expression used to validate email addresses with the name part. This property is used
37
         * only when {@see $allowName} is `true`.
38
         *
39
         * @see $allowName
40
         */
41
        private string $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/',
42
        /**
43
         * @var string the regular expression used to validate complex emails when {@see $enableIDN} is `true`.
44
         */
45
        private string $idnEmailPattern = '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
46
        /**
47
         * @var bool whether to allow name in the email address (e.g. "John Smith <[email protected]>"). Defaults
48
         * to `false`.
49
         *
50
         * @see $fullPattern
51
         */
52
        private bool $allowName = false,
53
        /**
54
         * @var bool whether to check whether the email's domain exists and has either an A or MX record.
55
         * Be aware that this check can fail due to temporary DNS problems even if the email address is
56
         * valid and an email would be deliverable. Defaults to `false`.
57
         */
58
        private bool $checkDNS = false,
59
        /**
60
         * @var bool whether validation process should take into account IDN (internationalized domain
61
         * names). Defaults to false meaning that validation of emails containing IDN will always fail.
62
         * Note that in order to use IDN validation you have to install and enable `intl` PHP extension,
63
         * otherwise an exception would be thrown.
64
         */
65
        private bool $enableIDN = false,
66
        private string $message = 'This value is not a valid email address.',
67
        private bool $skipOnEmpty = false,
68
        private bool $skipOnError = false,
69
        private ?Closure $when = null,
70
    ) {
71
        if ($enableIDN && !function_exists('idn_to_ascii')) {
72
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
73
        }
74
    }
75
76
    /**
77
     * @return string
78
     */
79 73
    public function getPattern(): string
80
    {
81 73
        return $this->pattern;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 20
    public function getFullPattern(): string
88
    {
89 20
        return $this->fullPattern;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 10
    public function getIdnEmailPattern(): string
96
    {
97 10
        return $this->idnEmailPattern;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 47
    public function isAllowName(): bool
104
    {
105 47
        return $this->allowName;
106
    }
107
108
    /**
109
     * @return bool
110
     */
111 39
    public function isCheckDNS(): bool
112
    {
113 39
        return $this->checkDNS;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 83
    public function isEnableIDN(): bool
120
    {
121 83
        return $this->enableIDN;
122
    }
123
124
    /**
125
     * @return string
126
     */
127 45
    public function getMessage(): string
128
    {
129 45
        return $this->message;
130
    }
131
132 4
    #[ArrayShape([
133
        'pattern' => 'string',
134
        'fullPattern' => 'string',
135
        'idnEmailPattern' => 'string',
136
        'allowName' => 'bool',
137
        'checkDNS' => 'bool',
138
        'enableIDN' => 'bool',
139
        'message' => 'string[]',
140
        'skipOnEmpty' => 'bool',
141
        'skipOnError' => 'bool',
142
    ])]
143
    public function getOptions(): array
144
    {
145
        return [
146 4
            'pattern' => $this->pattern,
147 4
            'fullPattern' => $this->fullPattern,
148 4
            'idnEmailPattern' => $this->idnEmailPattern,
149 4
            'allowName' => $this->allowName,
150 4
            'checkDNS' => $this->checkDNS,
151 4
            'enableIDN' => $this->enableIDN,
152
            'message' => [
153 4
                'message' => $this->message,
154
            ],
155 4
            'skipOnEmpty' => $this->skipOnEmpty,
156 4
            'skipOnError' => $this->skipOnError,
157
        ];
158
    }
159
}
160