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 integer minimum number of domain name labels; |
||
| 51 | * defaults to 1, meaning that domain name should contain at least 1 label |
||
| 52 | * @see messageLabelNumberMin for the customized message for domain name with too small number of labels |
||
| 53 | */ |
||
| 54 | public $labelNumberMin = 1; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string user-defined error message used when DNS record corresponding to domain name not found |
||
| 58 | */ |
||
| 59 | public $messageDNS; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string user-defined error message used when domain name contains a character which 'intl' extension |
||
| 63 | * failed to convert to ASCII |
||
| 64 | */ |
||
| 65 | public $messageIdnToAscii; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string user-defined error message used when domain name contains an invalid character |
||
| 69 | */ |
||
| 70 | public $messageInvalidCharacter; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string user-defined error message used when number of domain name labels is smaller than [[labelNumberMin]] |
||
| 74 | */ |
||
| 75 | public $messageLabelNumberMin; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string user-defined error message used when domain name label starts or ends with an invalid character |
||
| 79 | */ |
||
| 80 | public $messageLabelStartEnd; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string user-defined error message used when domain name label is too long |
||
| 84 | */ |
||
| 85 | public $messageLabelTooLong; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string user-defined error message used when domain name label is too short |
||
| 89 | */ |
||
| 90 | public $messageLabelTooShort; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string user-defined error message used when domain name is not a string |
||
| 94 | */ |
||
| 95 | public $messageNotString; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string user-defined error message used when domain name is too long |
||
| 99 | */ |
||
| 100 | public $messageTooLong; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string user-defined error message used when domain name is too short |
||
| 104 | */ |
||
| 105 | public $messageTooShort; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | */ |
||
| 110 | 148 | public function init() |
|
| 111 | { |
||
| 112 | 148 | parent::init(); |
|
| 113 | 148 | if ($this->enableIDN && !function_exists('idn_to_ascii')) { |
|
| 114 | 1 | throw new InvalidConfigException( |
|
| 115 | 'In order to use IDN validation intl extension must be installed and enabled.' |
||
| 116 | 1 | ); |
|
| 117 | } |
||
| 118 | 148 | if (!isset($this->encoding)) { |
|
| 119 | 148 | $this->encoding = Yii::$app->charset; |
|
| 120 | 148 | } |
|
| 121 | 148 | } |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | 147 | protected function validateValue($value) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get error message by name. |
||
| 244 | * @param string $name error message name |
||
| 245 | * @param array $params parameters to be inserted into the error message |
||
| 246 | * @return string error message. |
||
| 247 | */ |
||
| 248 | 67 | protected function getErrorMessage($name, $params = []) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get default error messages. |
||
| 259 | * @return array default error messages. |
||
| 260 | */ |
||
| 261 | 66 | protected function getDefaultErrorMessages() |
|
| 302 | } |
||
| 303 |