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 |
||
| 8 | class Uri implements UriInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Uri scheme (without "://" suffix) |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $scheme = ''; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Uri user |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $user = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Uri password |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $password = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Uri host |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $host = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Uri port number |
||
| 40 | * |
||
| 41 | * @var null|int |
||
| 42 | */ |
||
| 43 | protected $port; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Uri path |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $path = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Uri query string (without "?" prefix) |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $query = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Uri fragment string (without "#" prefix) |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $fragment = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Instance new Uri. |
||
| 68 | * |
||
| 69 | * @param string $scheme Uri scheme. |
||
| 70 | * @param string $host Uri host. |
||
| 71 | * @param int $port Uri port number. |
||
| 72 | * @param string $path Uri path. |
||
| 73 | * @param string $query Uri query string. |
||
| 74 | * @param string $fragment Uri fragment. |
||
| 75 | * @param string $user Uri user. |
||
| 76 | * @param string $password Uri password. |
||
| 77 | */ |
||
| 78 | public function __construct ( |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function getScheme () |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function getAuthority () |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function getUserInfo () |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function getHost () |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function getPort () |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getPath () |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function getQuery () |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function getFragment () |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function withScheme ($scheme) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function withUserInfo ($user, $password = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | public function withHost ($host) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function withPort ($port) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function withPath ($path) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function withQuery ($query) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function withFragment ($fragment) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * withString function. |
||
| 242 | * |
||
| 243 | * @access protected |
||
| 244 | * @param string $string |
||
| 245 | * @param string $name (default: 'query') |
||
| 246 | * @return Uri |
||
| 247 | */ |
||
| 248 | protected function withString ($string, $name = 'query') |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function __toString () |
||
| 277 | |||
| 278 | /* |
||
| 279 | END OF UriInterface Implementation |
||
| 280 | */ |
||
| 281 | |||
| 282 | /** |
||
| 283 | * filter scheme given to only allow certain scheme, no file:// or ftp:// or other scheme because its http message uri interface |
||
| 284 | * |
||
| 285 | * @access protected |
||
| 286 | * @param string $scheme |
||
| 287 | * @return string $scheme |
||
| 288 | * @throws InvalidArgumentException if not corret scheme is present |
||
| 289 | */ |
||
| 290 | protected function filterScheme (string $scheme) |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Filter allowable port to minimize risk |
||
| 309 | * |
||
| 310 | * @access protected |
||
| 311 | * @param integer|null $port |
||
| 312 | * @return null|integer $port |
||
| 313 | * @throws InvalidArgumentException for incorrect port assigned |
||
| 314 | */ |
||
| 315 | protected function filterPort ($port) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Path allowed chars filter, no weird path on uri yes?. |
||
| 326 | * |
||
| 327 | * @access protected |
||
| 328 | * @param string $path |
||
| 329 | * @return string of cleared path |
||
| 330 | */ |
||
| 331 | protected function filterPath ($path) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * replace query to clear not allowed chars |
||
| 344 | * |
||
| 345 | * @access protected |
||
| 346 | * @param string $query |
||
| 347 | * @return string of replaced query |
||
| 348 | */ |
||
| 349 | protected function filterQuery ($query) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * cek if current uri scheme use standard port |
||
| 362 | * |
||
| 363 | * @access protected |
||
| 364 | * @return boolean |
||
| 365 | */ |
||
| 366 | protected function hasStandardPort () |
||
| 370 | |||
| 371 | /** |
||
| 372 | * get Base Url |
||
| 373 | * |
||
| 374 | * @access public |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getBaseUrl () |
||
| 385 | } |
||
| 386 |