Conditions | 3 |
Paths | 2 |
Total Lines | 81 |
Code Lines | 2 |
Lines | 0 |
Ratio | 0 % |
Tests | 9 |
CRAP Score | 3 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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 |
||
33 | public function __construct( |
||
34 | #[Language('RegExp')] |
||
35 | /** |
||
36 | * @var string The regular expression used to validate the value. |
||
37 | * |
||
38 | * @link https://www.regular-expressions.info/email.html |
||
39 | */ |
||
40 | 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])?$/', |
||
41 | #[Language('RegExp')] |
||
42 | /** |
||
43 | * @var string The regular expression used to validate email addresses with the name part. |
||
44 | * This property is used only when {@see $allowName} is `true`. |
||
45 | * |
||
46 | * @see $allowName |
||
47 | */ |
||
48 | 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])?>$/', |
||
49 | #[Language('RegExp')] |
||
50 | /** |
||
51 | * @var string The regular expression used to validate complex emails when {@see $enableIDN} is `true`. |
||
52 | */ |
||
53 | 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})(\]?)$/', |
||
54 | /** |
||
55 | * @var bool Whether to allow a name in the email address (e.g. "John Smith <[email protected]>"). |
||
56 | * Defaults to `false`. |
||
57 | * |
||
58 | * @see $fullPattern |
||
59 | */ |
||
60 | private bool $allowName = false, |
||
61 | /** |
||
62 | * @var bool Whether to check email's domain exists and has either an A or MX record. |
||
63 | * Be aware that this check can fail due to temporary DNS problems even if the email address is |
||
64 | * valid and an email would be deliverable. Defaults to `false`. |
||
65 | */ |
||
66 | private bool $checkDNS = false, |
||
67 | /** |
||
68 | * @var bool Whether validation process should take IDN (internationalized domain names) into account. |
||
69 | * Defaults to false meaning that validation of emails containing IDN will always fail. |
||
70 | * Note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
||
71 | * otherwise an exception would be thrown. |
||
72 | */ |
||
73 | private bool $enableIDN = false, |
||
74 | /** |
||
75 | * @var string A message used when the input is incorrect. |
||
76 | * |
||
77 | * You may use the following placeholders in the message: |
||
78 | * |
||
79 | * - `{attribute}`: the label of the attribute being validated. |
||
80 | * - `{value}`: the value of the attribute being validated. |
||
81 | */ |
||
82 | private string $incorrectInputMessage = 'The value must have a string type.', |
||
83 | /** |
||
84 | * @var string A message used when the value is not valid. |
||
85 | * |
||
86 | 3 | * You may use the following placeholders in the message: |
|
87 | * |
||
88 | * - `{attribute}`: the label of the attribute being validated. |
||
89 | * - `{value}`: the value of the attribute being validated. |
||
90 | */ |
||
91 | private string $message = 'This value is not a valid email address.', |
||
92 | /** |
||
93 | * @var bool|callable|null Whether to skip this rule if the value validated is empty. |
||
94 | 1 | * |
|
95 | * @see SkipOnEmptyInterface |
||
96 | 1 | */ |
|
97 | private $skipOnEmpty = null, |
||
98 | /** |
||
99 | 83 | * @var bool Whether to skip this rule if any of the previous rules gave an error. |
|
100 | */ |
||
101 | 83 | private bool $skipOnError = false, |
|
102 | /** |
||
103 | * @var Closure|null A callable to define a condition for applying the rule. |
||
104 | 20 | * @psalm-var WhenType |
|
105 | * |
||
106 | 20 | * @see WhenInterface |
|
107 | */ |
||
108 | private Closure|null $when = null, |
||
109 | 10 | ) { |
|
110 | if ($enableIDN && !function_exists('idn_to_ascii')) { |
||
111 | 10 | // Tested via separate CI configuration (see ".github/workflows/build.yml"). |
|
112 | // @codeCoverageIgnoreStart |
||
113 | throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
||
114 | 48 | // @codeCoverageIgnoreEnd |
|
230 |