Passed
Push — master ( 89f940...819311 )
by Alexander
02:27
created

Email::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 48
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 48
ccs 2
cts 3
cp 0.6667
rs 10
cc 3
nc 2
nop 10
crap 3.3332

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\SerializableRuleInterface;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
16
use Yiisoft\Validator\ValidationContext;
17
18
use function function_exists;
19
20
/**
21
 * Validates that the value is a valid email address.
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Email implements SerializableRuleInterface, BeforeValidationInterface
25
{
26
    use BeforeValidationTrait;
27
    use RuleNameTrait;
28
29 2
    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
        /**
71
         * @var Closure(mixed, ValidationContext):bool|null
72
         */
73
        private ?Closure $when = null,
74
    ) {
75 2
        if ($enableIDN && !function_exists('idn_to_ascii')) {
76
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
77
        }
78
    }
79
80
    /**
81
     * @return string
82
     */
83 73
    public function getPattern(): string
84
    {
85 73
        return $this->pattern;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 20
    public function getFullPattern(): string
92
    {
93 20
        return $this->fullPattern;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 10
    public function getIdnEmailPattern(): string
100
    {
101 10
        return $this->idnEmailPattern;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107 47
    public function isAllowName(): bool
108
    {
109 47
        return $this->allowName;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 39
    public function isCheckDNS(): bool
116
    {
117 39
        return $this->checkDNS;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123 83
    public function isEnableIDN(): bool
124
    {
125 83
        return $this->enableIDN;
126
    }
127
128
    /**
129
     * @return string
130
     */
131 45
    public function getMessage(): string
132
    {
133 45
        return $this->message;
134
    }
135
136 4
    #[ArrayShape([
137
        'pattern' => 'string',
138
        'fullPattern' => 'string',
139
        'idnEmailPattern' => 'string',
140
        'allowName' => 'bool',
141
        'checkDNS' => 'bool',
142
        'enableIDN' => 'bool',
143
        'message' => 'string[]',
144
        'skipOnEmpty' => 'bool',
145
        'skipOnError' => 'bool',
146
    ])]
147
    public function getOptions(): array
148
    {
149
        return [
150 4
            'pattern' => $this->pattern,
151 4
            'fullPattern' => $this->fullPattern,
152 4
            'idnEmailPattern' => $this->idnEmailPattern,
153 4
            'allowName' => $this->allowName,
154 4
            'checkDNS' => $this->checkDNS,
155 4
            'enableIDN' => $this->enableIDN,
156
            'message' => [
157 4
                'message' => $this->message,
158
            ],
159 4
            'skipOnEmpty' => $this->skipOnEmpty,
160 4
            'skipOnError' => $this->skipOnError,
161
        ];
162
    }
163
164 1
    public function getHandlerClassName(): string
165
    {
166 1
        return EmailHandler::class;
167
    }
168
}
169