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 boolean whether to allow underscores in domain name; |
||
| 17 | * defaults to false |
||
| 18 | */ |
||
| 19 | public $allowUnderscore = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var boolean 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 boolean 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 | */ |
||
| 33 | public $checkDNS = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean whether validation process should take into account IDN (internationalized domain names); |
||
| 37 | * defaults to false, meaning that validation of domain names containing IDN will always fail; |
||
| 38 | * note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
||
| 39 | * otherwise an exception would be thrown |
||
| 40 | */ |
||
| 41 | public $enableIDN = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string the encoding of the string value to be validated (e.g. 'UTF-8'); |
||
| 45 | * if this property is not set, [[\yii\base\Application::charset]] will be used |
||
| 46 | */ |
||
| 47 | public $encoding; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string the base path for all translated messages; specify it if you want to use custom translated messages |
||
| 51 | */ |
||
| 52 | public $i18nBasePath; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var integer minimum number of domain name labels; |
||
| 56 | * defaults to 2, meaning that domain name should contain at least 2 labels |
||
| 57 | * @see messageLabelNumberMin for the customized message for domain name with too small number of labels |
||
| 58 | */ |
||
| 59 | public $labelNumberMin = 2; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string user-defined error message used when DNS record corresponding to domain name not found; |
||
| 63 | * you may use the following placeholders in the message: |
||
| 64 | * - `{attribute}`: the label of the attribute being validated |
||
| 65 | * - `{value}`: the value of the attribute being validated |
||
| 66 | */ |
||
| 67 | public $messageDNS; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string user-defined error message used when domain name is invalid but |
||
| 71 | * reason is too complicated for explanation to end-user or details are not needed at all; |
||
| 72 | * you may use the following placeholders in the message: |
||
| 73 | * - `{attribute}`: the label of the attribute being validated |
||
| 74 | * - `{value}`: the value of the attribute being validated |
||
| 75 | * @see simpleErrorMessage to use this message for all possible errors |
||
| 76 | */ |
||
| 77 | public $message; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string user-defined error message used when domain name contains an invalid character; |
||
| 81 | * you may use the following placeholders in the message: |
||
| 82 | * - `{attribute}`: the label of the attribute being validated |
||
| 83 | * - `{value}`: the value of the attribute being validated |
||
| 84 | */ |
||
| 85 | public $messageInvalidCharacter; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string user-defined error message used when number of domain name labels is smaller than [[labelNumberMin]]; |
||
| 89 | * you may use the following placeholders in the message: |
||
| 90 | * - `{attribute}`: the label of the attribute being validated |
||
| 91 | * - `{labelNumberMin}`: the value of [[labelNumberMin]] |
||
| 92 | * - `{value}`: the value of the attribute being validated |
||
| 93 | */ |
||
| 94 | public $messageLabelNumberMin; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string user-defined error message used when domain name label starts or ends with an invalid character; |
||
| 98 | * you may use the following placeholders in the message: |
||
| 99 | * - `{attribute}`: the label of the attribute being validated |
||
| 100 | * - `{value}`: the value of the attribute being validated |
||
| 101 | */ |
||
| 102 | public $messageLabelStartEnd; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string user-defined error message used when domain name label is too long; |
||
| 106 | * you may use the following placeholders in the message: |
||
| 107 | * - `{attribute}`: the label of the attribute being validated |
||
| 108 | * - `{value}`: the value of the attribute being validated |
||
| 109 | */ |
||
| 110 | public $messageLabelTooLong; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string user-defined error message used when domain name label is too short; |
||
| 114 | * you may use the following placeholders in the message: |
||
| 115 | * - `{attribute}`: the label of the attribute being validated |
||
| 116 | * - `{value}`: the value of the attribute being validated |
||
| 117 | */ |
||
| 118 | public $messageLabelTooShort; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var string user-defined error message used when domain name is not a string; |
||
| 122 | * you may use the following placeholders in the message: |
||
| 123 | * - `{attribute}`: the label of the attribute being validated |
||
| 124 | * - `{value}`: the value of the attribute being validated |
||
| 125 | */ |
||
| 126 | public $messageNotString; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string user-defined error message used when domain name is too long; |
||
| 130 | * you may use the following placeholders in the message: |
||
| 131 | * - `{attribute}`: the label of the attribute being validated |
||
| 132 | * - `{value}`: the value of the attribute being validated |
||
| 133 | */ |
||
| 134 | public $messageTooLong; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string user-defined error message used when domain name is too short; |
||
| 138 | * you may use the following placeholders in the message: |
||
| 139 | * - `{attribute}`: the label of the attribute being validated |
||
| 140 | * - `{value}`: the value of the attribute being validated |
||
| 141 | */ |
||
| 142 | public $messageTooShort; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var boolean whether to always use simple error message; |
||
| 146 | * defaults to false, meaning that validator should use specialized error messages for different errors, |
||
| 147 | * it should help end-user to understand reason of error; set it to true if detailed error messages don't fit |
||
| 148 | * for your application then [[message]] will be used in all cases |
||
| 149 | */ |
||
| 150 | public $simpleErrorMessage = false; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | 197 | public function init() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | 196 | protected function validateValue($value) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get error message by name. |
||
| 282 | * @param string $name error message name |
||
| 283 | * @param array $params parameters to be inserted into the error message |
||
| 284 | * @return string error message. |
||
| 285 | */ |
||
| 286 | 100 | protected function getErrorMessage($name, $params = []) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Get default error messages. |
||
| 300 | * @return array default error messages. |
||
| 301 | */ |
||
| 302 | 99 | protected function getDefaultErrorMessages() |
|
| 343 | } |
||
| 344 |