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 | 34 | public function __construct($url) |
|
| 83 | { |
||
| 84 | |||
| 85 | // Parse the URL |
||
| 86 | 34 | $this->urlParts = @parse_url($url); |
|
| 87 | 34 | if ($this->urlParts === false) { |
|
| 88 | 1 | throw new InvalidArgumentException( |
|
| 89 | 1 | sprintf('Invalid URL "%s"', $url), |
|
| 90 | InvalidArgumentException::INVALID_URL |
||
| 91 | 1 | ); |
|
| 92 | } |
||
| 93 | 33 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Return the serialized URL |
||
| 97 | * |
||
| 98 | * @return string Serialized URL |
||
| 99 | */ |
||
| 100 | 7 | public function __toString() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Return the full serialized URL |
||
| 107 | * |
||
| 108 | * @return string Full URL |
||
| 109 | */ |
||
| 110 | 7 | 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 getUrlInternal(array &$override = []) |
|
| 122 | { |
||
| 123 | // Prepare the URL scheme |
||
| 124 | 9 | $scheme = !empty($this->urlParts['scheme']) ? $this->getScheme().'://' : ''; |
|
| 125 | 9 | if (isset($override['scheme'])) { |
|
| 126 | $scheme = trim($override['scheme']); |
||
| 127 | if (strlen($scheme)) { |
||
| 128 | $scheme .= '://'; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | 9 | $override['scheme'] = $scheme; |
|
| 132 | |||
| 133 | // Prepare the URL user |
||
| 134 | 9 | $user = !empty($this->urlParts['user']) ? rawurlencode($this->getUser()) : ''; |
|
| 135 | 9 | if (isset($override['user'])) { |
|
| 136 | $user = $override['user']; |
||
| 137 | } |
||
| 138 | 9 | $override['user'] = $user; |
|
| 139 | |||
| 140 | // Prepare the URL password |
||
| 141 | 9 | $pass = !empty($this->urlParts['pass']) ? ':'.rawurlencode($this->getPassword()) : ''; |
|
| 142 | 9 | if (isset($override['pass'])) { |
|
| 143 | $pass = ':'.$override['pass']; |
||
| 144 | } |
||
| 145 | 9 | if ($user || $pass) { |
|
| 146 | 4 | $pass .= '@'; |
|
| 147 | 4 | } |
|
| 148 | 9 | $override['pass'] = $pass; |
|
| 149 | |||
| 150 | // Prepare the URL host |
||
| 151 | 9 | $host = !empty($this->urlParts['host']) ? $this->getHost() : ''; |
|
| 152 | 9 | if (isset($override['host'])) { |
|
| 153 | $host = $override['host']; |
||
| 154 | } |
||
| 155 | 9 | $override['host'] = $host; |
|
| 156 | |||
| 157 | // Prepare the URL port |
||
| 158 | 9 | $port = !empty($this->urlParts['port']) ? ':'.$this->getPort() : ''; |
|
| 159 | 9 | if (isset($override['port'])) { |
|
| 160 | $port = ':'.$override['port']; |
||
| 161 | } |
||
| 162 | 9 | $override['port'] = $port; |
|
| 163 | |||
| 164 | // Prepare the URL path |
||
| 165 | 9 | $path = empty($this->urlParts['path']) ? '' : $this->urlParts['path']; |
|
| 166 | 9 | if (isset($override['path'])) { |
|
| 167 | $path = $override['path']; |
||
| 168 | } |
||
| 169 | 9 | $override['path'] = $path; |
|
| 170 | |||
| 171 | // Prepare the URL query |
||
| 172 | 9 | $query = !empty($this->urlParts['query']) ? '?'.$this->urlParts['query'] : ''; |
|
| 173 | 9 | if (isset($override['query'])) { |
|
| 174 | 2 | $query = (is_array($override['query']) ? http_build_query($override['query']) : strval($override['query'])); |
|
| 175 | 2 | if (strlen($query)) { |
|
| 176 | $query = '?'.$query; |
||
| 177 | } |
||
| 178 | 2 | } |
|
| 179 | 9 | $override['query'] = $query; |
|
| 180 | |||
| 181 | // Prepare the URL fragment |
||
| 182 | 9 | $fragment = !empty($this->urlParts['fragment']) ? '#'.$this->getFragment() : ''; |
|
| 183 | 9 | if (isset($override['fragment'])) { |
|
| 184 | 2 | $fragment = $override['fragment']; |
|
| 185 | 2 | if (strlen($fragment)) { |
|
| 186 | $fragment = '#'.$fragment; |
||
| 187 | } |
||
| 188 | 2 | } |
|
| 189 | 9 | $override['fragment'] = $fragment; |
|
| 190 | |||
| 191 | 9 | return "$scheme$user$pass$host$port$path$query$fragment"; |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return the URL scheme |
||
| 196 | * |
||
| 197 | * @return string URL scheme |
||
| 198 | */ |
||
| 199 | 12 | public function getScheme() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Return the URL user |
||
| 206 | * |
||
| 207 | * @return string|NULL URL user |
||
| 208 | */ |
||
| 209 | 9 | public function getUser() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return the URL password |
||
| 216 | * |
||
| 217 | * @return string|NULL URL password |
||
| 218 | */ |
||
| 219 | 9 | public function getPassword() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Return the URL host |
||
| 226 | * |
||
| 227 | * @return string URL host |
||
| 228 | */ |
||
| 229 | 12 | public function getHost() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Return the URL port |
||
| 236 | * |
||
| 237 | * @return int URL port |
||
| 238 | */ |
||
| 239 | 9 | public function getPort() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Return the URL fragment |
||
| 246 | * |
||
| 247 | * @return string URL fragment |
||
| 248 | */ |
||
| 249 | 8 | public function getFragment() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Set the URL host |
||
| 256 | * |
||
| 257 | * @param string $host URL host |
||
| 258 | * @return Url New URL |
||
| 259 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 260 | */ |
||
| 261 | public function setHost($host) |
||
| 262 | { |
||
| 263 | // If the hostname is invalid |
||
| 264 | if (preg_match("%[/\?#]%", $host) || |
||
| 265 | (!filter_var('http://'.$host, FILTER_VALIDATE_URL) && !filter_var($host, FILTER_VALIDATE_IP)) |
||
| 266 | ) { |
||
| 267 | throw new InvalidArgumentException( |
||
| 268 | sprintf('Invalid URL host "%s"', $host), |
||
| 269 | InvalidArgumentException::INVALID_URL_HOST |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | |||
| 273 | $url = clone $this; |
||
| 274 | $url->urlParts['host'] = $host; |
||
| 275 | return $url; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set the URL port |
||
| 280 | * |
||
| 281 | * @param int $port URL port |
||
| 282 | * @return Url New URL |
||
| 283 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 284 | */ |
||
| 285 | public function setPort($port) |
||
| 286 | { |
||
| 287 | // If the URL port is invalid |
||
| 288 | if (!is_int($port) || ($port < 0) || ($port > 65535)) { |
||
| 289 | throw new InvalidArgumentException( |
||
| 290 | sprintf('Invalid URL port "%s"', $port), |
||
| 291 | InvalidArgumentException::INVALID_URL_PORT |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | |||
| 295 | $url = clone $this; |
||
| 296 | $url->urlParts['port'] = $port; |
||
| 297 | return $url; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Set the URL user |
||
| 302 | * |
||
| 303 | * @param string|NULL $user URL user |
||
| 304 | * @return Url New URL |
||
| 305 | */ |
||
| 306 | public function setUser($user) |
||
| 307 | { |
||
| 308 | $url = clone $this; |
||
| 309 | $url->urlParts['user'] = $user ?: null; |
||
| 310 | return $url; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set the URL password |
||
| 315 | * |
||
| 316 | * @param string $pass URL password |
||
| 317 | * @return Url New URL |
||
| 318 | */ |
||
| 319 | public function setPassword($pass) |
||
| 320 | { |
||
| 321 | $url = clone $this; |
||
| 322 | $url->urlParts['pass'] = $pass ?: null; |
||
| 323 | return $url; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the URL query |
||
| 328 | * |
||
| 329 | * @param array $query URL query |
||
| 330 | * @return Url New URL |
||
| 331 | */ |
||
| 332 | public function setQuery(array $query) |
||
| 333 | { |
||
| 334 | $url = clone $this; |
||
| 335 | $url->urlParts['query'] = http_build_query($query); |
||
| 336 | return $url; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set the URL fragment |
||
| 341 | * |
||
| 342 | * @param string $fragment URL fragment |
||
| 343 | * @return Url New URL |
||
| 344 | */ |
||
| 345 | public function setFragment($fragment) |
||
| 346 | { |
||
| 347 | $url = clone $this; |
||
| 348 | $url->urlParts['fragment'] = $fragment; |
||
| 349 | return $url; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Test whether this URL is remote |
||
| 354 | * |
||
| 355 | * @return bool Remote URL |
||
| 356 | */ |
||
| 357 | 1 | public function isRemote() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Return whether this URL is absolute |
||
| 364 | * |
||
| 365 | * @return bool Absolute URL |
||
| 366 | */ |
||
| 367 | 31 | public function isAbsolute() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Test whether this URL belongs to the local Apparat instance |
||
| 374 | * |
||
| 375 | * @return bool URL belongs to the local Apparat instance |
||
| 376 | */ |
||
| 377 | 10 | public function isAbsoluteLocal() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Return the URL path |
||
| 394 | * |
||
| 395 | * @return string URL path |
||
| 396 | */ |
||
| 397 | 14 | public function getPath() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Set the URL path |
||
| 404 | * |
||
| 405 | * @param string $path URL path |
||
| 406 | * @return Url New URL |
||
| 407 | */ |
||
| 408 | 10 | public function setPath($path) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Set the URL scheme |
||
| 419 | * |
||
| 420 | * @param string $scheme URL scheme |
||
| 421 | * @return Url New URL |
||
| 422 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 423 | */ |
||
| 424 | 10 | public function setScheme($scheme) |
|
| 425 | { |
||
| 426 | // If the URL scheme is not valid |
||
| 427 | 10 | if (strlen($scheme) && !array_key_exists($scheme, static::$schemes)) { |
|
| 428 | throw new InvalidArgumentException( |
||
| 429 | sprintf('Invalid URL scheme "%s"', $scheme), |
||
| 430 | InvalidArgumentException::INVALID_URL_SCHEME |
||
| 431 | ); |
||
| 432 | } |
||
| 433 | |||
| 434 | 10 | $url = clone $this; |
|
| 435 | 10 | $url->urlParts['scheme'] = $scheme; |
|
| 436 | 10 | return $url; |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Test if this URL matches all available parts of a given URL |
||
| 441 | * |
||
| 442 | * @param Url $url Comparison URL |
||
| 443 | * @return bool This URL matches all available parts of the given URL |
||
| 444 | */ |
||
| 445 | 7 | public function matches(Url $url) |
|
| 498 | |||
| 499 | /******************************************************************************* |
||
| 500 | * PRIVATE METHODS |
||
| 501 | *******************************************************************************/ |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Return the URL query |
||
| 505 | * |
||
| 506 | * @return array URL query |
||
| 507 | */ |
||
| 508 | 5 | public function getQuery() |
|
| 516 | } |
||
| 517 |