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 |
||
| 10 | class Uri implements UriInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $scheme = 'http'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $userInfo = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $rawUserInfo = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $host; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private $port = 80; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $path = '/'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $query = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $fragment = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private $explicitPortSpecified = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | private $explicitTrailingHostSlash = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $uri |
||
| 64 | */ |
||
| 65 | public function __construct($uri = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $uri |
||
| 74 | * |
||
| 75 | * @throws \InvalidArgumentException |
||
| 76 | */ |
||
| 77 | protected function parseUri($uri) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $rawUserInfo |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function protectUserInfo($rawUserInfo) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getScheme() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getUserInfo() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getRawUserInfo() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getHost() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | public function getPort() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getPath() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getQuery() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getFragment() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Uses protected user info by default as per rfc3986-3.2.1 |
||
| 210 | * Uri::getRawAuthority() is available if plain-text password information is desirable. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getAuthority() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getRawAuthority() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getAbsoluteUri() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getRelativeUri() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Uses protected user info by default as per rfc3986-3.2.1 |
||
| 283 | * Uri::getAbsoluteUri() is available if plain-text password information is desirable. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function __toString() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param $path |
||
| 310 | */ |
||
| 311 | public function setPath($path) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $query |
||
| 326 | */ |
||
| 327 | public function setQuery($query) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $var |
||
| 334 | * @param string $val |
||
| 335 | */ |
||
| 336 | public function addToQuery($var, $val) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $fragment |
||
| 346 | */ |
||
| 347 | public function setFragment($fragment) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $scheme |
||
| 354 | */ |
||
| 355 | public function setScheme($scheme) |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $userInfo |
||
| 363 | */ |
||
| 364 | public function setUserInfo($userInfo) |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * @param int $port |
||
| 373 | */ |
||
| 374 | public function setPort($port) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $host |
||
| 387 | */ |
||
| 388 | public function setHost($host) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function hasExplicitTrailingHostSlash() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | public function hasExplicitPortSpecified() |
||
| 408 | } |
||
| 409 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.