Complex classes like Uri 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 Uri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class Uri extends BaseObject implements UriInterface |
||
67 | { |
||
68 | /** |
||
69 | * @var string URI complete string. |
||
70 | */ |
||
71 | private $_string; |
||
72 | /** |
||
73 | * @var array URI components. |
||
74 | */ |
||
75 | private $_components; |
||
76 | /** |
||
77 | * @var array scheme default ports in format: `[scheme => port]` |
||
78 | */ |
||
79 | private static $defaultPorts = [ |
||
80 | 'http' => 80, |
||
81 | 'https' => 443, |
||
82 | 'ftp' => 21, |
||
83 | 'gopher' => 70, |
||
84 | 'nntp' => 119, |
||
85 | 'news' => 119, |
||
86 | 'telnet' => 23, |
||
87 | 'tn3270' => 23, |
||
88 | 'imap' => 143, |
||
89 | 'pop' => 110, |
||
90 | 'ldap' => 389, |
||
91 | ]; |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @return string URI string representation. |
||
96 | */ |
||
97 | public function getString() |
||
107 | |||
108 | /** |
||
109 | * @param string $string URI full string. |
||
110 | */ |
||
111 | public function setString($string) |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getScheme() |
||
124 | |||
125 | /** |
||
126 | * Sets up the scheme component of the URI. |
||
127 | * @param string $scheme the scheme. |
||
128 | */ |
||
129 | public function setScheme($scheme) |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function withScheme($scheme) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function getAuthority() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function getUserInfo() |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function getHost() |
||
171 | |||
172 | /** |
||
173 | * Specifies hostname. |
||
174 | * @param string $host the hostname to be used. |
||
175 | */ |
||
176 | public function setHost($host) |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function withHost($host) |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function getPort() |
||
202 | |||
203 | /** |
||
204 | * Specifies port. |
||
205 | * @param int|null $port The port to be used; a `null` value removes the port information. |
||
206 | */ |
||
207 | public function setPort($port) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function withPort($port) |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getPath() |
||
238 | |||
239 | /** |
||
240 | * Specifies path component of the URI |
||
241 | * @param string $path the path to be used. |
||
242 | */ |
||
243 | public function setPath($path) |
||
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | public function withPath($path) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function getQuery() |
||
269 | |||
270 | /** |
||
271 | * Specifies query string. |
||
272 | * @param string|array|object $query the query string or array of query parameters. |
||
273 | */ |
||
274 | public function setQuery($query) |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | public function withQuery($query) |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function getFragment() |
||
303 | |||
304 | /** |
||
305 | * Specifies URI fragment. |
||
306 | * @param string $fragment the fragment to be used. |
||
307 | */ |
||
308 | public function setFragment($fragment) |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function withFragment($fragment) |
||
326 | |||
327 | /** |
||
328 | * @return string the user name to use for authority. |
||
329 | */ |
||
330 | public function getUser() |
||
334 | |||
335 | /** |
||
336 | * @param string $user the user name to use for authority. |
||
337 | */ |
||
338 | public function setUser($user) |
||
342 | |||
343 | /** |
||
344 | * @return string password associated with [[user]]. |
||
345 | */ |
||
346 | public function getPassword() |
||
350 | |||
351 | /** |
||
352 | * @param string $password password associated with [[user]]. |
||
353 | */ |
||
354 | public function setPassword($password) |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function withUserInfo($user, $password = null) |
||
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | public function __toString() |
||
393 | |||
394 | /** |
||
395 | * Sets up particular URI component. |
||
396 | * @param string $name URI component name. |
||
397 | * @param mixed $value URI component value. |
||
398 | */ |
||
399 | protected function setComponent($name, $value) |
||
407 | |||
408 | /** |
||
409 | * @param string $name URI component name. |
||
410 | * @param mixed $default default value, which should be returned in case component is not exist. |
||
411 | * @return mixed URI component value. |
||
412 | */ |
||
413 | protected function getComponent($name, $default = null) |
||
421 | |||
422 | /** |
||
423 | * Returns URI components for this instance as an associative array. |
||
424 | * @return array URI components in format: `[name => value]` |
||
425 | */ |
||
426 | protected function getComponents() |
||
436 | |||
437 | /** |
||
438 | * Parses a URI and returns an associative array containing any of the various components of the URI |
||
439 | * that are present. |
||
440 | * @param string $uri the URI string to parse. |
||
441 | * @return array URI components. |
||
442 | */ |
||
443 | protected function parseUri($uri) |
||
451 | |||
452 | /** |
||
453 | * Composes URI string from given components. |
||
454 | * @param array $components URI components. |
||
455 | * @return string URI full string. |
||
456 | */ |
||
457 | protected function composeUri(array $components) |
||
489 | |||
490 | /** |
||
491 | * @param array $components URI components. |
||
492 | * @return string user info string. |
||
493 | */ |
||
494 | protected function composeUserInfo(array $components) |
||
505 | |||
506 | /** |
||
507 | * @param array $components URI components. |
||
508 | * @return string authority string. |
||
509 | */ |
||
510 | protected function composeAuthority(array $components) |
||
534 | |||
535 | /** |
||
536 | * Checks whether specified port is default one for the specified scheme. |
||
537 | * @param string $scheme scheme. |
||
538 | * @param int $port port number. |
||
539 | * @return bool whether specified port is default for specified scheme |
||
540 | */ |
||
541 | protected function isDefaultPort($scheme, $port) |
||
548 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..