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