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 | * return a new instance from an array or a traversable object |
||
48 | * |
||
49 | * @param \Traversable|string[] $data The segments list |
||
50 | * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE |
||
51 | * |
||
52 | * @throws InvalidArgumentException If $type is not a recognized constant |
||
53 | * |
||
54 | * @return static |
||
55 | */ |
||
56 | 162 | public static function createFromArray($data, $type = self::IS_RELATIVE) |
|
74 | |||
75 | |||
76 | /** |
||
77 | * New instance |
||
78 | * |
||
79 | * @param null|string $host |
||
80 | */ |
||
81 | 1138 | public function __construct($host = null) |
|
86 | |||
87 | /** |
||
88 | * Returns whether or not the host is an IDN |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | 9 | public function isIdn() |
|
96 | |||
97 | /** |
||
98 | * Returns whether or not the host is an IP address |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 1072 | public function isIp() |
|
106 | |||
107 | /** |
||
108 | * Returns whether or not the host is an IPv4 address |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 39 | public function isIpv4() |
|
116 | |||
117 | /** |
||
118 | * Returns whether or not the host is an IPv6 address |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 39 | public function isIpv6() |
|
126 | |||
127 | /** |
||
128 | * Returns whether or not the host has a ZoneIdentifier |
||
129 | * |
||
130 | * @return bool |
||
131 | * |
||
132 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
133 | */ |
||
134 | 12 | public function hasZoneIdentifier() |
|
138 | |||
139 | /** |
||
140 | * Host literal setter |
||
141 | */ |
||
142 | 1072 | protected function setLiteral() |
|
146 | |||
147 | /** |
||
148 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
149 | * |
||
150 | * @deprecated deprecated since version 4.2 |
||
151 | * |
||
152 | * Returns the instance literal representation |
||
153 | * without encoding |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getLiteral() |
||
161 | |||
162 | /** |
||
163 | * validate the submitted data |
||
164 | * |
||
165 | * @param string $str |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | 1138 | protected function validate($str) |
|
187 | |||
188 | /** |
||
189 | * Retrieves a single host label. |
||
190 | * |
||
191 | * Retrieves a single host label. If the label offset has not been set, |
||
192 | * returns the default value provided. |
||
193 | * |
||
194 | * @param string $offset the label offset |
||
195 | * @param mixed $default Default value to return if the offset does not exist. |
||
196 | * |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 3 | public function getLabel($offset, $default = null) |
|
207 | |||
208 | /** |
||
209 | * Returns an array representation of the host |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 865 | public function toArray() |
|
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | 1048 | public function getContent() |
|
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | 2 | public function __debugInfo() |
|
241 | |||
242 | /** |
||
243 | * @inheritdoc |
||
244 | */ |
||
245 | 15 | public static function __set_state(array $properties) |
|
253 | |||
254 | /** |
||
255 | * Returns a host in his punycode encoded form |
||
256 | * |
||
257 | * This method MUST retain the state of the current instance, and return |
||
258 | * an instance with the host transcoded using to ascii the RFC 3492 rules |
||
259 | * |
||
260 | * @see http://tools.ietf.org/html/rfc3492 |
||
261 | * |
||
262 | * @return static |
||
263 | */ |
||
264 | 108 | public function toAscii() |
|
275 | |||
276 | /** |
||
277 | * Returns a host in his IDN form |
||
278 | * |
||
279 | * This method MUST retain the state of the current instance, and return |
||
280 | * an instance with the host in its IDN form using RFC 3492 rules |
||
281 | * |
||
282 | * @see http://tools.ietf.org/html/rfc3492 |
||
283 | * |
||
284 | * @return static |
||
285 | */ |
||
286 | 81 | public function toUnicode() |
|
294 | |||
295 | /** |
||
296 | * @inheritdoc |
||
297 | */ |
||
298 | 886 | protected static function formatComponentString($data, $type) |
|
307 | |||
308 | /** |
||
309 | * Return an host without its zone identifier according to RFC6874 |
||
310 | * |
||
311 | * This method MUST retain the state of the current instance, and return |
||
312 | * an instance without the host zone identifier according to RFC6874 |
||
313 | * |
||
314 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
315 | * |
||
316 | * @return static |
||
317 | */ |
||
318 | 18 | public function withoutZoneIdentifier() |
|
326 | |||
327 | /** |
||
328 | * Validated the Host Label Count |
||
329 | * |
||
330 | * @param array $labels Host labels |
||
331 | * |
||
332 | * @throws InvalidArgumentException If the validation fails |
||
333 | */ |
||
334 | 886 | protected function assertLabelsCount(array $labels) |
|
340 | |||
341 | /** |
||
342 | * set the FQDN property |
||
343 | * |
||
344 | * @param string $str |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | 901 | protected function setIsAbsolute($str) |
|
358 | |||
359 | /** |
||
360 | * @inheritdoc |
||
361 | */ |
||
362 | 60 | public function append($component) |
|
369 | } |
||
370 |