Complex classes like AbstractUri 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 AbstractUri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class AbstractUri implements UriInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string Original URI |
||
24 | * @since 1.0 |
||
25 | */ |
||
26 | protected $uri = null; |
||
27 | |||
28 | /** |
||
29 | * @var string Protocol |
||
30 | * @since 1.0 |
||
31 | */ |
||
32 | protected $scheme = null; |
||
33 | |||
34 | /** |
||
35 | * @var string Host |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | protected $host = null; |
||
39 | |||
40 | /** |
||
41 | * @var integer Port |
||
42 | * @since 1.0 |
||
43 | */ |
||
44 | protected $port = null; |
||
45 | |||
46 | /** |
||
47 | * @var string Username |
||
48 | * @since 1.0 |
||
49 | */ |
||
50 | protected $user = null; |
||
51 | |||
52 | /** |
||
53 | * @var string Password |
||
54 | * @since 1.0 |
||
55 | */ |
||
56 | protected $pass = null; |
||
57 | |||
58 | /** |
||
59 | * @var string Path |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | protected $path = null; |
||
63 | |||
64 | /** |
||
65 | * @var string Query |
||
66 | * @since 1.0 |
||
67 | */ |
||
68 | protected $query = null; |
||
69 | |||
70 | /** |
||
71 | * @var string Anchor |
||
72 | * @since 1.0 |
||
73 | */ |
||
74 | protected $fragment = null; |
||
75 | |||
76 | /** |
||
77 | * @var array Query variable hash |
||
78 | * @since 1.0 |
||
79 | */ |
||
80 | protected $vars = array(); |
||
81 | |||
82 | /** |
||
83 | * Constructor. |
||
84 | * You can pass a URI string to the constructor to initialise a specific URI. |
||
85 | * |
||
86 | * @param string $uri The optional URI string |
||
87 | * |
||
88 | * @since 1.0 |
||
89 | */ |
||
90 | public function __construct($uri = null) |
||
97 | |||
98 | /** |
||
99 | * Magic method to get the string representation of the URI object. |
||
100 | * |
||
101 | * @return string |
||
102 | * |
||
103 | * @since 1.0 |
||
104 | */ |
||
105 | public function __toString() |
||
109 | |||
110 | /** |
||
111 | * Returns full uri string. |
||
112 | * |
||
113 | * @param array $parts An array specifying the parts to render. |
||
114 | * |
||
115 | * @return string The rendered URI string. |
||
116 | * |
||
117 | * @since 1.0 |
||
118 | */ |
||
119 | public function toString(array $parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment')) |
||
136 | |||
137 | /** |
||
138 | * Checks if variable exists. |
||
139 | * |
||
140 | * @param string $name Name of the query variable to check. |
||
141 | * |
||
142 | * @return boolean True if the variable exists. |
||
143 | * |
||
144 | * @since 1.0 |
||
145 | */ |
||
146 | public function hasVar($name) |
||
150 | |||
151 | /** |
||
152 | * Returns a query variable by name. |
||
153 | * |
||
154 | * @param string $name Name of the query variable to get. |
||
155 | * @param string $default Default value to return if the variable is not set. |
||
156 | * |
||
157 | * @return array Query variables. |
||
158 | * |
||
159 | * @since 1.0 |
||
160 | */ |
||
161 | public function getVar($name, $default = null) |
||
170 | |||
171 | /** |
||
172 | * Returns flat query string. |
||
173 | * |
||
174 | * @param boolean $toArray True to return the query as a key => value pair array. |
||
175 | * |
||
176 | * @return string Query string. |
||
177 | * |
||
178 | * @since 1.0 |
||
179 | */ |
||
180 | public function getQuery($toArray = false) |
||
195 | |||
196 | /** |
||
197 | * Get URI scheme (protocol) |
||
198 | * ie. http, https, ftp, etc... |
||
199 | * |
||
200 | * @return string The URI scheme. |
||
201 | * |
||
202 | * @since 1.0 |
||
203 | */ |
||
204 | public function getScheme() |
||
208 | |||
209 | /** |
||
210 | * Get URI username |
||
211 | * Returns the username, or null if no username was specified. |
||
212 | * |
||
213 | * @return string The URI username. |
||
214 | * |
||
215 | * @since 1.0 |
||
216 | */ |
||
217 | public function getUser() |
||
221 | |||
222 | /** |
||
223 | * Get URI password |
||
224 | * Returns the password, or null if no password was specified. |
||
225 | * |
||
226 | * @return string The URI password. |
||
227 | * |
||
228 | * @since 1.0 |
||
229 | */ |
||
230 | public function getPass() |
||
234 | |||
235 | /** |
||
236 | * Get URI host |
||
237 | * Returns the hostname/ip or null if no hostname/ip was specified. |
||
238 | * |
||
239 | * @return string The URI host. |
||
240 | * |
||
241 | * @since 1.0 |
||
242 | */ |
||
243 | public function getHost() |
||
247 | |||
248 | /** |
||
249 | * Get URI port |
||
250 | * Returns the port number, or null if no port was specified. |
||
251 | * |
||
252 | * @return integer The URI port number. |
||
253 | * |
||
254 | * @since 1.0 |
||
255 | */ |
||
256 | public function getPort() |
||
260 | |||
261 | /** |
||
262 | * Gets the URI path string. |
||
263 | * |
||
264 | * @return string The URI path string. |
||
265 | * |
||
266 | * @since 1.0 |
||
267 | */ |
||
268 | public function getPath() |
||
272 | |||
273 | /** |
||
274 | * Get the URI archor string |
||
275 | * Everything after the "#". |
||
276 | * |
||
277 | * @return string The URI anchor string. |
||
278 | * |
||
279 | * @since 1.0 |
||
280 | */ |
||
281 | public function getFragment() |
||
285 | |||
286 | /** |
||
287 | * Checks whether the current URI is using HTTPS. |
||
288 | * |
||
289 | * @return boolean True if using SSL via HTTPS. |
||
290 | * |
||
291 | * @since 1.0 |
||
292 | */ |
||
293 | public function isSSL() |
||
297 | |||
298 | /** |
||
299 | * Build a query from a array (reverse of the PHP parse_str()). |
||
300 | * |
||
301 | * @param array $params The array of key => value pairs to return as a query string. |
||
302 | * |
||
303 | * @return string The resulting query string. |
||
304 | * |
||
305 | * @see parse_str() |
||
306 | * @since 1.0 |
||
307 | */ |
||
308 | protected static function buildQuery(array $params) |
||
312 | |||
313 | /** |
||
314 | * Parse a given URI and populate the class fields. |
||
315 | * |
||
316 | * @param string $uri The URI string to parse. |
||
317 | * |
||
318 | * @return boolean True on success. |
||
319 | * |
||
320 | * @since 1.0 |
||
321 | */ |
||
322 | protected function parse($uri) |
||
359 | |||
360 | /** |
||
361 | * Resolves //, ../ and ./ from a path and returns |
||
362 | * the result. Eg: |
||
363 | * |
||
364 | * /foo/bar/../boo.php => /foo/boo.php |
||
365 | * /foo/bar/../../boo.php => /boo.php |
||
366 | * /foo/bar/.././/boo.php => /foo/boo.php |
||
367 | * |
||
368 | * @param string $path The URI path to clean. |
||
369 | * |
||
370 | * @return string Cleaned and resolved URI path. |
||
371 | * |
||
372 | * @since 1.0 |
||
373 | */ |
||
374 | protected function cleanPath($path) |
||
402 | } |
||
403 |