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 |
||
| 45 | class Url |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * HTTP-Schema |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | const SCHEME_HTTP = 'http'; |
||
| 53 | /** |
||
| 54 | * HTTPS-Schema |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const SCHEME_HTTPS = 'https'; |
||
| 59 | /** |
||
| 60 | * Valid schemes |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected static $_schemes = [self::SCHEME_HTTP => true, self::SCHEME_HTTPS => true]; |
||
| 65 | /** |
||
| 66 | * URL parts |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $_urlParts = null; |
||
| 71 | |||
| 72 | /******************************************************************************* |
||
| 73 | * PUBLIC METHODS |
||
| 74 | *******************************************************************************/ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * URL constructor |
||
| 78 | * |
||
| 79 | * @param string $url URL |
||
| 80 | * @throws InvalidArgumentException If the URL is invalid |
||
| 81 | */ |
||
| 82 | 30 | public function __construct($url) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Return the serialized URL |
||
| 97 | * |
||
| 98 | * @return string Serialized URL |
||
| 99 | */ |
||
| 100 | 6 | public function __toString() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Return the full serialized URL |
||
| 107 | * |
||
| 108 | * @return string Full URL |
||
| 109 | */ |
||
| 110 | 6 | public function getUrl() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Return the a complete serialized URL |
||
| 117 | * |
||
| 118 | * @param array $override Override components |
||
| 119 | * @return string Serialized URL |
||
| 120 | */ |
||
| 121 | 9 | protected function _getUrl(array &$override = []) |
|
| 122 | { |
||
| 123 | // Prepare the URL scheme |
||
| 124 | 9 | if (isset($override['scheme'])) { |
|
| 125 | 1 | $scheme = trim($override['scheme']); |
|
| 126 | 1 | if (strlen($scheme)) { |
|
| 127 | 1 | $scheme .= '://'; |
|
| 128 | 1 | } |
|
| 129 | 1 | } else { |
|
| 130 | 8 | $scheme = !empty($this->_urlParts['scheme']) ? $this->getScheme().'://' : ''; |
|
| 131 | } |
||
| 132 | 9 | $override['scheme'] = $scheme; |
|
| 133 | |||
| 134 | // Prepare the URL user |
||
| 135 | 9 | if (isset($override['user'])) { |
|
| 136 | 1 | $user = $override['user']; |
|
| 137 | 1 | } else { |
|
| 138 | 8 | $user = !empty($this->_urlParts['user']) ? rawurlencode($this->getUser()) : ''; |
|
| 139 | } |
||
| 140 | 9 | $override['user'] = $user; |
|
| 141 | |||
| 142 | // Prepare the URL password |
||
| 143 | 9 | if (isset($override['pass'])) { |
|
| 144 | 1 | $pass = ':'.$override['pass']; |
|
| 145 | 1 | } else { |
|
| 146 | 8 | $pass = !empty($this->_urlParts['pass']) ? ':'.rawurlencode($this->getPassword()) : ''; |
|
| 147 | } |
||
| 148 | 9 | if ($user || $pass) { |
|
| 149 | 5 | $pass .= '@'; |
|
| 150 | 5 | } |
|
| 151 | 9 | $override['pass'] = $pass; |
|
| 152 | |||
| 153 | // Prepare the URL host |
||
| 154 | 9 | if (isset($override['host'])) { |
|
| 155 | 1 | $host = $override['host']; |
|
| 156 | 1 | } else { |
|
| 157 | 8 | $host = !empty($this->_urlParts['host']) ? $this->getHost() : ''; |
|
| 158 | } |
||
| 159 | 9 | $override['host'] = $host; |
|
| 160 | |||
| 161 | // Prepare the URL port |
||
| 162 | 9 | if (isset($override['port'])) { |
|
| 163 | 1 | $port = ':'.$override['port']; |
|
| 164 | 1 | } else { |
|
| 165 | 8 | $port = !empty($this->_urlParts['port']) ? ':'.$this->getPort() : ''; |
|
| 166 | } |
||
| 167 | 9 | $override['port'] = $port; |
|
| 168 | |||
| 169 | // Prepare the URL path |
||
| 170 | 9 | if (isset($override['path'])) { |
|
| 171 | 1 | $path = $override['path']; |
|
| 172 | 1 | } else { |
|
| 173 | 8 | $path = empty($this->_urlParts['path']) ? '' : $this->_urlParts['path']; |
|
| 174 | } |
||
| 175 | 9 | $override['path'] = $path; |
|
| 176 | |||
| 177 | // Prepare the URL query |
||
| 178 | 9 | if (isset($override['query'])) { |
|
| 179 | 4 | $query = (is_array($override['query']) ? http_build_query($override['query']) : strval($override['query'])); |
|
| 180 | 4 | if (strlen($query)) { |
|
| 181 | 1 | $query = '?'.$query; |
|
| 182 | 1 | } |
|
| 183 | 4 | } else { |
|
| 184 | 6 | $query = !empty($this->_urlParts['query']) ? '?'.$this->_urlParts['query'] : ''; |
|
| 185 | } |
||
| 186 | 9 | $override['query'] = $query; |
|
| 187 | |||
| 188 | // Prepare the URL fragment |
||
| 189 | 9 | if (isset($override['fragment'])) { |
|
| 190 | 4 | $fragment = $override['fragment']; |
|
| 191 | 4 | if (strlen($fragment)) { |
|
| 192 | 1 | $fragment = '#'.$fragment; |
|
| 193 | 1 | } |
|
| 194 | 4 | } else { |
|
| 195 | 6 | $fragment = !empty($this->_urlParts['fragment']) ? '#'.$this->getFragment() : ''; |
|
| 196 | } |
||
| 197 | 9 | $override['fragment'] = $fragment; |
|
| 198 | |||
| 199 | 9 | return "$scheme$user$pass$host$port$path$query$fragment"; |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return the URL scheme |
||
| 204 | * |
||
| 205 | * @return string URL scheme |
||
| 206 | */ |
||
| 207 | 13 | public function getScheme() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Return the URL user |
||
| 214 | * |
||
| 215 | * @return string|NULL URL user |
||
| 216 | */ |
||
| 217 | 9 | public function getUser() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Return the URL password |
||
| 224 | * |
||
| 225 | * @return string|NULL URL password |
||
| 226 | */ |
||
| 227 | 9 | public function getPassword() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Return the URL host |
||
| 234 | * |
||
| 235 | * @return string URL host |
||
| 236 | */ |
||
| 237 | 13 | public function getHost() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Return the URL port |
||
| 244 | * |
||
| 245 | * @return int URL port |
||
| 246 | */ |
||
| 247 | 9 | public function getPort() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Return the URL fragment |
||
| 254 | * |
||
| 255 | * @return string URL fragment |
||
| 256 | */ |
||
| 257 | 6 | public function getFragment() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Set the URL host |
||
| 264 | * |
||
| 265 | * @param string $host URL host |
||
| 266 | * @return Url New URL |
||
| 267 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 268 | */ |
||
| 269 | 1 | public function setHost($host) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Set the URL port |
||
| 290 | * |
||
| 291 | * @param int $port URL port |
||
| 292 | * @return Url New URL |
||
| 293 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 294 | */ |
||
| 295 | 1 | public function setPort($port) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Set the URL user |
||
| 312 | * |
||
| 313 | * @param string|NULL $user URL user |
||
| 314 | * @return Url New URL |
||
| 315 | */ |
||
| 316 | 1 | public function setUser($user) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set the URL password |
||
| 325 | * |
||
| 326 | * @param string $pass URL password |
||
| 327 | * @return Url New URL |
||
| 328 | */ |
||
| 329 | 1 | public function setPassword($pass) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set the URL query |
||
| 338 | * |
||
| 339 | * @param array $query URL query |
||
| 340 | * @return Url New URL |
||
| 341 | */ |
||
| 342 | 1 | public function setQuery(array $query) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Set the URL fragment |
||
| 351 | * |
||
| 352 | * @param string $fragment URL fragment |
||
| 353 | * @return Url New URL |
||
| 354 | */ |
||
| 355 | 1 | public function setFragment($fragment) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Test whether this URL is remote |
||
| 364 | * |
||
| 365 | * @return bool Remote URL |
||
| 366 | */ |
||
| 367 | public function isRemote() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return whether this URL is absolute |
||
| 374 | * |
||
| 375 | * @return bool Absolute URL |
||
| 376 | */ |
||
| 377 | 27 | public function isAbsolute() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Test whether this URL belongs to the local Apparat instance |
||
| 384 | * |
||
| 385 | * @return bool URL belongs to the local Apparat instance |
||
| 386 | */ |
||
| 387 | 9 | public function isAbsoluteLocal() |
|
| 388 | { |
||
| 389 | // Instantiate the apparat base URL |
||
| 390 | 9 | $apparatBaseUrl = new self(getenv('APPARAT_BASE_URL')); |
|
| 391 | 9 | $apparatBaseUrlPath = $apparatBaseUrl->getPath(); |
|
| 392 | 9 | $apparatBaseUrl = $apparatBaseUrl->setScheme(null)->setPath(null); |
|
| 393 | |||
| 394 | // If the URL matches the Apparat base URL (the scheme is disregarded) |
||
| 395 | 9 | return $this->isAbsolute() && $this->matches($apparatBaseUrl) && !strncmp( |
|
| 396 | 2 | $apparatBaseUrlPath, |
|
| 397 | 2 | $this->getPath(), strlen($apparatBaseUrlPath) |
|
| 398 | 9 | ); |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return the URL path |
||
| 403 | * |
||
| 404 | * @return string URL path |
||
| 405 | */ |
||
| 406 | 17 | public function getPath() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Set the URL path |
||
| 413 | * |
||
| 414 | * @param string $path URL path |
||
| 415 | * @return Url New URL |
||
| 416 | */ |
||
| 417 | 10 | public function setPath($path) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Set the URL scheme |
||
| 428 | * |
||
| 429 | * @param string $scheme URL scheme |
||
| 430 | * @return Url New URL |
||
| 431 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 432 | */ |
||
| 433 | 10 | public function setScheme($scheme) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Test if this URL matches all available parts of a given URL |
||
| 450 | * |
||
| 451 | * @param Url $url Comparison URL |
||
| 452 | * @return bool This URL matches all available parts of the given URL |
||
| 453 | */ |
||
| 454 | 7 | public function matches(Url $url) |
|
| 513 | |||
| 514 | /******************************************************************************* |
||
| 515 | * PRIVATE METHODS |
||
| 516 | *******************************************************************************/ |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Return the URL query |
||
| 520 | * |
||
| 521 | * @return array URL query |
||
| 522 | */ |
||
| 523 | 6 | public function getQuery() |
|
| 531 | } |
||
| 532 |
If you suppress an error, we recommend checking for the error condition explicitly: