Complex classes like Url 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 Url, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Url extends AbstractPart |
||
37 | { |
||
38 | /** |
||
39 | * @var string The original url string. |
||
40 | */ |
||
41 | private $url; |
||
42 | |||
43 | /** |
||
44 | * @var ParserInterface |
||
45 | */ |
||
46 | private $parser; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $data = array( |
||
52 | 'scheme' => null, |
||
53 | 'host' => null, |
||
54 | 'port' => null, |
||
55 | 'user' => null, |
||
56 | 'pass' => null, |
||
57 | 'path' => null, |
||
58 | 'query' => null, |
||
59 | 'fragment' => null, |
||
60 | 'publicSuffix' => null, |
||
61 | 'registerableDomain' => null, |
||
62 | 'subdomain' => null, |
||
63 | 'canonical' => null, |
||
64 | 'resource' => null |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $partClassMap = array( |
||
71 | 'path' => 'Purl\Path', |
||
72 | 'query' => 'Purl\Query', |
||
73 | 'fragment' => 'Purl\Fragment' |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Construct a new Url instance. |
||
78 | * |
||
79 | * @param string $url |
||
80 | * @param ParserInterface $parser |
||
81 | */ |
||
82 | public function __construct($url = null, ParserInterface $parser = null) |
||
87 | |||
88 | /** |
||
89 | * Static convenience method for creating a new Url instance. |
||
90 | * |
||
91 | * @param string $url |
||
92 | * @return Url |
||
93 | */ |
||
94 | public static function parse($url) |
||
98 | |||
99 | /** |
||
100 | * Extracts urls from a string of text. |
||
101 | * |
||
102 | * @param string $string |
||
103 | * @return array $urls |
||
104 | */ |
||
105 | public static function extract($string) |
||
117 | |||
118 | /** |
||
119 | * Creates an Url instance based on data available on $_SERVER variable. |
||
120 | * |
||
121 | * @return Url |
||
122 | */ |
||
123 | public static function fromCurrent() |
||
124 | { |
||
125 | $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http'; |
||
126 | |||
127 | $host = $_SERVER['HTTP_HOST']; |
||
128 | $baseUrl = "$scheme://$host"; |
||
129 | |||
130 | $url = new self($baseUrl); |
||
131 | |||
132 | if (!empty($_SERVER['REQUEST_URI'])) { |
||
133 | if (strpos($_SERVER['REQUEST_URI'], '?') !== false) { |
||
134 | list($path, $query) = explode('?', $_SERVER['REQUEST_URI'], 2); |
||
135 | } else { |
||
136 | $path = $_SERVER['REQUEST_URI']; |
||
137 | $query = ''; |
||
138 | } |
||
139 | |||
140 | $url->set('path', $path); |
||
141 | $url->set('query', $query); |
||
142 | } |
||
143 | |||
144 | // Only set port if different from default (80 or 443) |
||
145 | if (!empty($_SERVER['SERVER_PORT'])) { |
||
146 | $port = $_SERVER['SERVER_PORT']; |
||
147 | if (($scheme == 'http' && $port != 80) || |
||
148 | ($scheme == 'https' && $port != 443)) { |
||
149 | $url->set('port', $port); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // Authentication |
||
154 | if (!empty($_SERVER['PHP_AUTH_USER'])) { |
||
155 | $url->set('user', $_SERVER['PHP_AUTH_USER']); |
||
156 | if (!empty($_SERVER['PHP_AUTH_PW'])) { |
||
157 | $url->set('pass', $_SERVER['PHP_AUTH_PW']); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $url; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Gets the ParserInterface instance used to parse this Url instance. |
||
166 | * |
||
167 | * @return ParserInterface |
||
168 | */ |
||
169 | public function getParser() |
||
177 | |||
178 | /** |
||
179 | * Sets the ParserInterface instance to use to parse this Url instance. |
||
180 | * |
||
181 | * @param ParserInterface $parser |
||
182 | */ |
||
183 | public function setParser(ParserInterface $parser) |
||
187 | |||
188 | /** |
||
189 | * Join this Url instance together with another Url instance or a string url. |
||
190 | * |
||
191 | * @param Url|string $url |
||
192 | * @return Url |
||
193 | */ |
||
194 | public function join($url) |
||
211 | |||
212 | /** |
||
213 | * @inheritDoc |
||
214 | * @override |
||
215 | */ |
||
216 | public function set($key, $value) |
||
223 | |||
224 | /** |
||
225 | * Set the Path instance. |
||
226 | * |
||
227 | * @param Path |
||
228 | */ |
||
229 | public function setPath(Path $path) |
||
235 | |||
236 | /** |
||
237 | * Get the Path instance. |
||
238 | * |
||
239 | * @return Path |
||
240 | */ |
||
241 | public function getPath() |
||
246 | |||
247 | /** |
||
248 | * Set the Query instance. |
||
249 | * |
||
250 | * @param Query |
||
251 | */ |
||
252 | public function setQuery(Query $query) |
||
258 | |||
259 | /** |
||
260 | * Get the Query instance. |
||
261 | * |
||
262 | * @return Query |
||
263 | */ |
||
264 | public function getQuery() |
||
269 | |||
270 | /** |
||
271 | * Set the Fragment instance. |
||
272 | * |
||
273 | * @param Fragment |
||
274 | */ |
||
275 | public function setFragment(Fragment $fragment) |
||
281 | |||
282 | /** |
||
283 | * Get the Fragment instance. |
||
284 | * |
||
285 | * @return Fragment |
||
286 | */ |
||
287 | public function getFragment() |
||
292 | |||
293 | /** |
||
294 | * Gets the netloc part of the Url. It is the user, pass, host and port returned as a string. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getNetloc() |
||
303 | |||
304 | /** |
||
305 | * Builds a string url from this Url instance internal data and returns it. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getUrl() |
||
316 | |||
317 | /** |
||
318 | * Set the string url for this Url instance and sets initialized to false. |
||
319 | * |
||
320 | * @param string |
||
321 | */ |
||
322 | public function setUrl($url) |
||
328 | |||
329 | /** |
||
330 | * Checks if the Url instance is absolute or not. |
||
331 | * |
||
332 | * @return boolean |
||
333 | */ |
||
334 | public function isAbsolute() |
||
339 | |||
340 | /** |
||
341 | * @inheritDoc |
||
342 | */ |
||
343 | public function __toString() |
||
347 | |||
348 | /** |
||
349 | * @inheritDoc |
||
350 | */ |
||
351 | protected function doInitialize() |
||
365 | |||
366 | /** |
||
367 | * Reconstructs a string URL from an array of parts. |
||
368 | * |
||
369 | * @param array $parts |
||
370 | * @return string $url |
||
371 | */ |
||
372 | private static function httpBuildUrl(array $parts) |
||
386 | |||
387 | /** |
||
388 | * Creates the default Parser instance to parse urls. |
||
389 | * |
||
390 | * @return Parser |
||
391 | */ |
||
392 | private static function createDefaultParser() |
||
399 | } |
||
400 |