Complex classes like Host 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 Host, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Host extends AbstractHierarchicalComponent implements HostInterface |
||
| 25 | { |
||
| 26 | use HostIpTrait; |
||
| 27 | |||
| 28 | use HostnameInfoTrait; |
||
| 29 | |||
| 30 | use HostnameTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * HierarchicalComponent delimiter |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected static $separator = '.'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Host literal representation |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $host; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
| 48 | * |
||
| 49 | * @deprecated deprecated since version 4.2 |
||
| 50 | * |
||
| 51 | * return a new instance from an array or a traversable object |
||
| 52 | * |
||
| 53 | * @param \Traversable|string[] $data The segments list |
||
| 54 | * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE |
||
| 55 | * |
||
| 56 | * @throws InvalidArgumentException If $data is invalid |
||
| 57 | * @throws InvalidArgumentException If $type is not a recognized constant |
||
| 58 | * |
||
| 59 | * @return static |
||
| 60 | */ |
||
| 61 | public static function createFromArray($data, $type = self::IS_RELATIVE) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * return a new instance from an array or a traversable object |
||
| 68 | * |
||
| 69 | * @param \Traversable|string[] $data The segments list |
||
| 70 | * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE |
||
| 71 | * |
||
| 72 | * @throws InvalidArgumentException If $data is invalid |
||
| 73 | * @throws InvalidArgumentException If $type is not a recognized constant |
||
| 74 | * |
||
| 75 | * @return static |
||
| 76 | */ |
||
| 77 | 165 | public static function createFromLabels($data, $type = self::IS_RELATIVE) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Return a formatted host string |
||
| 99 | * |
||
| 100 | * @param string[] $data The segments list |
||
| 101 | * @param int $type |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 889 | protected static function format(array $data, $type) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * New instance |
||
| 117 | * |
||
| 118 | * @param null|string $host |
||
| 119 | */ |
||
| 120 | 1153 | public function __construct($host = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * validate the submitted data |
||
| 128 | * |
||
| 129 | * @param string $str |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 1153 | protected function validate($str) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Return a new instance when needed |
||
| 154 | * |
||
| 155 | * @param array $data |
||
| 156 | * |
||
| 157 | * @return static |
||
| 158 | */ |
||
| 159 | 45 | protected function newCollectionInstance(array $data) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Returns whether or not the host is an IDN |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | 9 | public function isIdn() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns whether or not the host is an IP address |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | 1087 | public function isIp() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Returns whether or not the host is an IPv4 address |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | 39 | public function isIpv4() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Returns whether or not the host is an IPv6 address |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 39 | public function isIpv6() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Returns whether or not the host has a ZoneIdentifier |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | * |
||
| 209 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
| 210 | */ |
||
| 211 | 12 | public function hasZoneIdentifier() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
| 218 | * |
||
| 219 | * @deprecated deprecated since version 4.2 |
||
| 220 | * |
||
| 221 | * Returns the instance literal representation |
||
| 222 | * without encoding |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getLiteral() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns an array representation of the Host |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 880 | public function getLabels() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieves a single host label. |
||
| 243 | * |
||
| 244 | * Retrieves a single host label. If the label offset has not been set, |
||
| 245 | * returns the default value provided. |
||
| 246 | * |
||
| 247 | * @param string $offset the label offset |
||
| 248 | * @param mixed $default Default value to return if the offset does not exist. |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | 3 | public function getLabel($offset, $default = null) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
| 263 | * |
||
| 264 | * @deprecated deprecated since version 4.2 |
||
| 265 | * |
||
| 266 | * Returns an array representation of the host |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function toArray() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | 1069 | public function getContent() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritdoc |
||
| 293 | */ |
||
| 294 | 2 | public function __debugInfo() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @inheritdoc |
||
| 301 | */ |
||
| 302 | 15 | public static function __set_state(array $properties) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Returns a host in his punycode encoded form |
||
| 313 | * |
||
| 314 | * This method MUST retain the state of the current instance, and return |
||
| 315 | * an instance with the host transcoded using to ascii the RFC 3492 rules |
||
| 316 | * |
||
| 317 | * @see http://tools.ietf.org/html/rfc3492 |
||
| 318 | * |
||
| 319 | * @return static |
||
| 320 | */ |
||
| 321 | 216 | public function toAscii() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns a host in his IDN form |
||
| 335 | * |
||
| 336 | * This method MUST retain the state of the current instance, and return |
||
| 337 | * an instance with the host in its IDN form using RFC 3492 rules |
||
| 338 | * |
||
| 339 | * @see http://tools.ietf.org/html/rfc3492 |
||
| 340 | * |
||
| 341 | * @return static |
||
| 342 | */ |
||
| 343 | 81 | public function toUnicode() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Return an host without its zone identifier according to RFC6874 |
||
| 354 | * |
||
| 355 | * This method MUST retain the state of the current instance, and return |
||
| 356 | * an instance without the host zone identifier according to RFC6874 |
||
| 357 | * |
||
| 358 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
| 359 | * |
||
| 360 | * @return static |
||
| 361 | */ |
||
| 362 | 18 | public function withoutZoneIdentifier() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Validated the Host Label Count |
||
| 373 | * |
||
| 374 | * @param array $labels Host labels |
||
| 375 | * |
||
| 376 | * @throws InvalidArgumentException If the validation fails |
||
| 377 | */ |
||
| 378 | 898 | protected function assertLabelsCount(array $labels) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * set the FQDN property |
||
| 387 | * |
||
| 388 | * @param string $str |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | 913 | protected function setIsAbsolute($str) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @inheritdoc |
||
| 405 | */ |
||
| 406 | 30 | public function prepend($component) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @inheritdoc |
||
| 416 | */ |
||
| 417 | 60 | public function append($component) |
|
| 424 | } |
||
| 425 |