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 |
||
| 7 | final class Uri implements UriInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string|null |
||
| 11 | */ |
||
| 12 | private $scheme; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string|null |
||
| 16 | */ |
||
| 17 | private $host; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int|null |
||
| 21 | */ |
||
| 22 | private $port; |
||
| 23 | |||
| 24 | const PORT_HTTP = 80; |
||
| 25 | const PORT_HTTPS = 443; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string|null |
||
| 29 | */ |
||
| 30 | private $user; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string|null |
||
| 34 | */ |
||
| 35 | private $password; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $path; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string|null |
||
| 44 | */ |
||
| 45 | private $query; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string|null |
||
| 49 | */ |
||
| 50 | private $fragment; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var UriInterface|null |
||
| 54 | */ |
||
| 55 | private $__previous; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string|null $scheme |
||
| 59 | * @param string|null $host |
||
| 60 | * @param int|null $port |
||
| 61 | * @param string|null $user |
||
| 62 | * @param string|null $password |
||
| 63 | * @param string|null $path |
||
| 64 | * @param string|null $query |
||
| 65 | * @param string|null $fragment |
||
| 66 | * @param UriInterface|null $previous |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $uri |
||
| 92 | * |
||
| 93 | * @return UriInterface |
||
| 94 | * |
||
| 95 | * @throws \InvalidArgumentException |
||
| 96 | */ |
||
| 97 | public static function create(string $uri) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function getScheme(): string |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function getAuthority(): string |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function getUserInfo(): string |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function getHost(): string |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function getPort() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function getPath(): string |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function getQuery(): string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function getFragment(): string |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function withScheme($scheme): self |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function withUserInfo($user, $password = null): self |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function withHost($host): self |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function withPort($port): self |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function withPath($path): self |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function withQuery($query): self |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function withFragment($fragment): self |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function __toString(): string |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $scheme |
||
| 300 | * |
||
| 301 | * @return int|null |
||
| 302 | */ |
||
| 303 | private function getPortForScheme(string $scheme) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $authority |
||
| 316 | * @param string $path |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | private function getPathForUri(string $authority, string $path): string |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $path |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | private function getPathForUriWithAuthority(string $path) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $path |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | private function getPathForUriWithoutAuthority(string $path) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $parameters |
||
| 358 | * |
||
| 359 | * @return Uri |
||
| 360 | */ |
||
| 361 | public function with(array $parameters): self |
||
| 378 | } |
||
| 379 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: