Complex classes like DomainValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DomainValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class DomainValidator extends Validator |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var bool whether to allow underscores in domain name; |
||
| 17 | * defaults to false |
||
| 18 | */ |
||
| 19 | public $allowUnderscore = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool whether to allow URL address along with domain name; |
||
| 23 | * defaults to true, meaning that validator should try to parse URL address and then validate domain name |
||
| 24 | */ |
||
| 25 | public $allowURL = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool|callable whether to check whether domain name exists; |
||
| 29 | * be aware that this check can fail due to temporary DNS problems even if domain name exists; |
||
| 30 | * do not use it to check domain name availability; |
||
| 31 | * defaults to false; |
||
| 32 | * this field can be specified as a PHP callback, for example: |
||
| 33 | * ```php |
||
| 34 | * function (string $domain) { |
||
| 35 | * $records = @dns_get_record("$domain.", DNS_MX); // @ is just for simplicity of example, avoid to use it |
||
| 36 | * if (empty($records)) { |
||
| 37 | * return ['Cannot find Mail Exchanger record for "{value}".', ['value' => $domain]]; |
||
| 38 | * } |
||
| 39 | * |
||
| 40 | * return null; // the data is valid |
||
| 41 | * } |
||
| 42 | * ``` |
||
| 43 | * note that alternatively you can override method `checkDNS` |
||
| 44 | * @see checkDNS |
||
| 45 | */ |
||
| 46 | public $checkDNS = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool whether validation process should take into account IDN (internationalized domain names); |
||
| 50 | * defaults to false, meaning that validation of domain names containing IDN will always fail; |
||
| 51 | * note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
||
| 52 | * otherwise an exception would be thrown |
||
| 53 | */ |
||
| 54 | public $enableIDN = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string the encoding of the string value to be validated (e.g. 'UTF-8'); |
||
| 58 | * if this property is not set, [[\yii\base\Application::charset]] will be used |
||
| 59 | */ |
||
| 60 | public $encoding; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string the base path for all translated messages; specify it if you want to use custom translated messages |
||
| 64 | */ |
||
| 65 | public $i18nBasePath; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var int minimum number of domain name labels; |
||
| 69 | * defaults to 2, meaning that domain name should contain at least 2 labels |
||
| 70 | * @see messageLabelNumberMin for the customized message for domain name with too small number of labels |
||
| 71 | */ |
||
| 72 | public $labelNumberMin = 2; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string user-defined error message used when domain name is invalid but |
||
| 76 | * reason is too complicated for explanation to end-user or details are not needed at all; |
||
| 77 | * you may use the following placeholders in the message: |
||
| 78 | * - `{attribute}`: the label of the attribute being validated |
||
| 79 | * - `{value}`: the value of the attribute being validated |
||
| 80 | * @see simpleErrorMessage to use this message for all possible errors |
||
| 81 | */ |
||
| 82 | public $message; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string user-defined error message used when DNS record corresponding to domain name not found; |
||
| 86 | * you may use the following placeholders in the message: |
||
| 87 | * - `{attribute}`: the label of the attribute being validated |
||
| 88 | * - `{value}`: the value of the attribute being validated |
||
| 89 | */ |
||
| 90 | public $messageDNS; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string user-defined error message used when domain name contains an invalid character; |
||
| 94 | * you may use the following placeholders in the message: |
||
| 95 | * - `{attribute}`: the label of the attribute being validated |
||
| 96 | * - `{value}`: the value of the attribute being validated |
||
| 97 | */ |
||
| 98 | public $messageInvalidCharacter; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string user-defined error message used when number of domain name labels is smaller than [[labelNumberMin]]; |
||
| 102 | * you may use the following placeholders in the message: |
||
| 103 | * - `{attribute}`: the label of the attribute being validated |
||
| 104 | * - `{labelNumberMin}`: the value of [[labelNumberMin]] |
||
| 105 | * - `{value}`: the value of the attribute being validated |
||
| 106 | */ |
||
| 107 | public $messageLabelNumberMin; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string user-defined error message used when domain name label starts or ends with an invalid character; |
||
| 111 | * you may use the following placeholders in the message: |
||
| 112 | * - `{attribute}`: the label of the attribute being validated |
||
| 113 | * - `{value}`: the value of the attribute being validated |
||
| 114 | */ |
||
| 115 | public $messageLabelStartEnd; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string user-defined error message used when domain name label is too long; |
||
| 119 | * you may use the following placeholders in the message: |
||
| 120 | * - `{attribute}`: the label of the attribute being validated |
||
| 121 | * - `{value}`: the value of the attribute being validated |
||
| 122 | */ |
||
| 123 | public $messageLabelTooLong; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string user-defined error message used when domain name label is too short; |
||
| 127 | * you may use the following placeholders in the message: |
||
| 128 | * - `{attribute}`: the label of the attribute being validated |
||
| 129 | * - `{value}`: the value of the attribute being validated |
||
| 130 | */ |
||
| 131 | public $messageLabelTooShort; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string user-defined error message used when domain name is not a string; |
||
| 135 | * you may use the following placeholders in the message: |
||
| 136 | * - `{attribute}`: the label of the attribute being validated |
||
| 137 | * - `{value}`: the value of the attribute being validated |
||
| 138 | */ |
||
| 139 | public $messageNotString; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string user-defined error message used when domain name is too long; |
||
| 143 | * you may use the following placeholders in the message: |
||
| 144 | * - `{attribute}`: the label of the attribute being validated |
||
| 145 | * - `{value}`: the value of the attribute being validated |
||
| 146 | */ |
||
| 147 | public $messageTooLong; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var string user-defined error message used when domain name is too short; |
||
| 151 | * you may use the following placeholders in the message: |
||
| 152 | * - `{attribute}`: the label of the attribute being validated |
||
| 153 | * - `{value}`: the value of the attribute being validated |
||
| 154 | */ |
||
| 155 | public $messageTooShort; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var bool whether to always use simple error message; |
||
| 159 | * defaults to false, meaning that validator should use specialized error messages for different errors, |
||
| 160 | * it should help end-user to understand reason of error; set it to true if detailed error messages don't fit |
||
| 161 | * for your application then [[message]] will be used in all cases |
||
| 162 | */ |
||
| 163 | public $simpleErrorMessage = false; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 198 | public function init() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 197 | protected function validateValue($value) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Check whether domain name exists. |
||
| 302 | * @param string $value domain name |
||
| 303 | * @return bool whether domain name exists. |
||
| 304 | * @see https://github.com/yiisoft/yii2/issues/17083 |
||
| 305 | */ |
||
| 306 | 2 | protected function checkDNS($value) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get error message by name. |
||
| 319 | * @param string $name error message name |
||
| 320 | * @param array $params parameters to be inserted into the error message |
||
| 321 | * @return array error message. |
||
| 322 | */ |
||
| 323 | 100 | protected function getErrorMessage($name, $params = []) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get default error messages. |
||
| 337 | * @return array default error messages. |
||
| 338 | */ |
||
| 339 | 99 | protected function getDefaultErrorMessages() |
|
| 380 | } |
||
| 381 |