Test Failed
Pull Request — master (#364)
by
unknown
03:02
created

Email::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 56
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 56
ccs 2
cts 3
cp 0.6667
rs 10
cc 3
nc 2
nop 11
crap 3.3332

How to fix   Long Method    Many Parameters   

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:

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