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 | * @param UriInterface|null $previous |
||
| 93 | * |
||
| 94 | * @return UriInterface |
||
| 95 | * |
||
| 96 | * @throws \InvalidArgumentException |
||
| 97 | */ |
||
| 98 | public static function create(string $uri, UriInterface $previous = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function getScheme(): string |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function getAuthority(): string |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function getUserInfo(): string |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function getHost(): string |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function getPort() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | public function getPath(): string |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function getQuery(): string |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function getFragment(): string |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return UriInterface|null |
||
| 216 | */ |
||
| 217 | public function getPrevious() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function withScheme($scheme): self |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function withUserInfo($user, $password = null): self |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function withHost($host): self |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function withPort($port): self |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function withPath($path): self |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function withQuery($query): self |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritdoc} |
||
| 272 | */ |
||
| 273 | public function withFragment($fragment): self |
||
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | public function __toString(): string |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $scheme |
||
| 310 | * |
||
| 311 | * @return int|null |
||
| 312 | */ |
||
| 313 | private function getPortForScheme(string $scheme) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $authority |
||
| 326 | * @param string $path |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function getPathForUri(string $authority, string $path): string |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $path |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | private function getPathForUriWithAuthority(string $path) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $path |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function getPathForUriWithoutAuthority(string $path) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param array $arguments |
||
| 368 | * |
||
| 369 | * @return Uri |
||
| 370 | */ |
||
| 371 | public function with(array $arguments): self |
||
| 389 | } |
||
| 390 |
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: