Conditions | 3 |
Paths | 2 |
Total Lines | 51 |
Code Lines | 3 |
Lines | 0 |
Ratio | 0 % |
Tests | 3 |
CRAP Score | 3.1406 |
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 |
||
27 | 2 | public function __construct( |
|
28 | /** |
||
29 | * @var string the regular expression used to validate value. |
||
30 | * |
||
31 | * @link http://www.regular-expressions.info/email.html |
||
32 | */ |
||
33 | 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])?$/', |
||
34 | /** |
||
35 | * @var string the regular expression used to validate email addresses with the name part. This property is used |
||
36 | * only when {@see $allowName} is `true`. |
||
37 | * |
||
38 | * @see $allowName |
||
39 | */ |
||
40 | 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])?>$/', |
||
41 | /** |
||
42 | * @var string the regular expression used to validate complex emails when {@see $enableIDN} is `true`. |
||
43 | */ |
||
44 | 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})(\]?)$/', |
||
45 | /** |
||
46 | * @var bool whether to allow name in the email address (e.g. "John Smith <[email protected]>"). Defaults |
||
47 | * to `false`. |
||
48 | * |
||
49 | * @see $fullPattern |
||
50 | */ |
||
51 | private bool $allowName = false, |
||
52 | /** |
||
53 | * @var bool whether to check whether the email's domain exists and has either an A or MX record. |
||
54 | * Be aware that this check can fail due to temporary DNS problems even if the email address is |
||
55 | * valid and an email would be deliverable. Defaults to `false`. |
||
56 | */ |
||
57 | private bool $checkDNS = false, |
||
58 | /** |
||
59 | * @var bool whether validation process should take into account IDN (internationalized domain |
||
60 | * names). Defaults to false meaning that validation of emails containing IDN will always fail. |
||
61 | * Note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
||
62 | * otherwise an exception would be thrown. |
||
63 | */ |
||
64 | private bool $enableIDN = false, |
||
65 | private string $message = 'This value is not a valid email address.', |
||
66 | private bool $skipOnEmpty = false, |
||
67 | private $skipOnEmptyCallback = null, |
||
68 | private bool $skipOnError = false, |
||
69 | /** |
||
70 | * @var Closure(mixed, ValidationContext):bool|null |
||
71 | */ |
||
72 | private ?Closure $when = null, |
||
73 | ) { |
||
74 | 2 | $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback); |
|
75 | |||
76 | 2 | if ($enableIDN && !function_exists('idn_to_ascii')) { |
|
77 | throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
||
78 | } |
||
159 |