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 | * @var string |
||
| 12 | */ |
||
| 13 | protected $uri; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $scheme = ''; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $authority; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $user; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $pass; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $userInfo; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $host; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $port; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $path; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $query = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $fragment; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $schemes = [ |
||
| 69 | 'http' => 80, |
||
| 70 | 'https' => 443, |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param $uri |
||
| 75 | */ |
||
| 76 | 53 | public function __construct($uri = '') |
|
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | * |
||
| 88 | * @see https://tools.ietf.org/html/rfc3986#section-3.1 |
||
| 89 | * @return string The URI scheme. |
||
| 90 | */ |
||
| 91 | 12 | public function getScheme() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | * |
||
| 99 | * @see https://tools.ietf.org/html/rfc3986#section-3.2 |
||
| 100 | * @return string The URI authority, in "[user-info@]host[:port]" format. |
||
| 101 | */ |
||
| 102 | 9 | public function getAuthority() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | * |
||
| 115 | * @return string The URI user information, in "username[:password]" format. |
||
| 116 | */ |
||
| 117 | 12 | public function getUserInfo() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | * |
||
| 125 | * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
| 126 | * @return string The URI host. |
||
| 127 | */ |
||
| 128 | 6 | public function getHost() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | * |
||
| 136 | * @return null|int The URI port. |
||
| 137 | */ |
||
| 138 | 6 | public function getPort() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | * |
||
| 147 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 148 | * @see https://tools.ietf.org/html/rfc3986#section-3.3 |
||
| 149 | * @return string The URI path. |
||
| 150 | */ |
||
| 151 | 23 | public function getPath() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | * |
||
| 159 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 160 | * @see https://tools.ietf.org/html/rfc3986#section-3.4 |
||
| 161 | * @return string The URI query string. |
||
| 162 | */ |
||
| 163 | 14 | public function getQuery() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | * |
||
| 171 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 172 | * @see https://tools.ietf.org/html/rfc3986#section-3.5 |
||
| 173 | * @return string The URI fragment. |
||
| 174 | */ |
||
| 175 | 15 | public function getFragment() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | * |
||
| 183 | * @param string $scheme The scheme to use with the new instance. |
||
| 184 | * @return self A new instance with the specified scheme. |
||
| 185 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
| 186 | */ |
||
| 187 | 6 | public function withScheme($scheme) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | * |
||
| 201 | * @param string $user The user name to use for authority. |
||
| 202 | * @param null|string $password The password associated with $user. |
||
| 203 | * @return self A new instance with the specified user information. |
||
| 204 | */ |
||
| 205 | 4 | public function withUserInfo($user, $password = null) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | * |
||
| 215 | * @param string $host |
||
| 216 | * @return Uri |
||
| 217 | */ |
||
| 218 | 3 | public function withHost($host) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | * |
||
| 226 | * @param null|int $port The port to use with the new instance; a null value |
||
| 227 | * removes the port information. |
||
| 228 | * @return self A new instance with the specified port. |
||
| 229 | * @throws InvalidArgumentException for invalid ports. |
||
| 230 | */ |
||
| 231 | 5 | public function withPort($port) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | * |
||
| 247 | * @param string $path The path to use with the new instance. |
||
| 248 | * @return self A new instance with the specified path. |
||
| 249 | * @throws \InvalidArgumentException for invalid paths. |
||
| 250 | */ |
||
| 251 | 7 | public function withPath($path) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | * |
||
| 267 | * @param string $query The query string to use with the new instance. |
||
| 268 | * @return self A new instance with the specified query string. |
||
| 269 | * @throws \InvalidArgumentException for invalid query strings. |
||
| 270 | */ |
||
| 271 | 5 | public function withQuery($query) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | * |
||
| 279 | * @param string $fragment The fragment to use with the new instance. |
||
| 280 | * @return self A new instance with the specified fragment. |
||
| 281 | */ |
||
| 282 | 7 | public function withFragment($fragment) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | * |
||
| 294 | * @see http://tools.ietf.org/html/rfc3986#section-4.1 |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | 8 | public function __toString() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param $uri |
||
| 314 | */ |
||
| 315 | 36 | protected function format($uri) |
|
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * Filters the query string or fragment of a URI. |
||
| 333 | * |
||
| 334 | * @param string $query The raw uri query string. |
||
| 335 | * @return string The percent-encoded query string. |
||
| 336 | */ |
||
| 337 | 7 | protected function filter($query) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Filter Uri path. |
||
| 351 | * |
||
| 352 | * This method percent-encodes all reserved |
||
| 353 | * characters in the provided path string. This method |
||
| 354 | * will NOT double-encode characters that are already |
||
| 355 | * percent-encoded. |
||
| 356 | * |
||
| 357 | * @param string $path The raw uri path. |
||
| 358 | * @return string The RFC 3986 percent-encoded uri path. |
||
| 359 | * @link http://www.faqs.org/rfcs/rfc3986.html |
||
| 360 | */ |
||
| 361 | 5 | protected function filterPath($path) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param $scheme |
||
| 375 | * @param $port |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 14 | protected function isStandardPort($scheme, $port) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param $key |
||
| 395 | * @param $value |
||
| 396 | * @return static |
||
| 397 | */ |
||
| 398 | 14 | protected function _clone($key, $value) |
|
| 408 | } |
||
| 409 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.