Complex classes like HttpDispatcher 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 HttpDispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class HttpDispatcher implements Dispatcher |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var HttpAsyncClient |
||
| 41 | */ |
||
| 42 | private $httpClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var UriFactory |
||
| 46 | */ |
||
| 47 | private $uriFactory; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Queued requests. |
||
| 51 | * |
||
| 52 | * @var RequestInterface[] |
||
| 53 | */ |
||
| 54 | private $queue = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Caching proxy server host names or IP addresses. |
||
| 58 | * |
||
| 59 | * @var UriInterface[] |
||
| 60 | */ |
||
| 61 | private $servers; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Application host name and optional base URL. |
||
| 65 | * |
||
| 66 | * @var UriInterface[] |
||
| 67 | */ |
||
| 68 | private $baseUris; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * If you specify a custom HTTP client, make sure that it converts HTTP |
||
| 72 | * errors to exceptions. |
||
| 73 | * |
||
| 74 | * If your proxy server IPs can not be statically configured, extend this |
||
| 75 | * class and overwrite getServers. Be sure to have some caching in |
||
| 76 | * getServers. |
||
| 77 | * |
||
| 78 | * @param string[] $servers Caching proxy server hostnames or IP |
||
| 79 | * addresses, including port if not port 80. |
||
| 80 | * E.g. ['127.0.0.1:6081'] |
||
| 81 | * @param string|string[] $baseUris Default application hostnames, optionally |
||
| 82 | * including base URL, for purge and refresh |
||
| 83 | * requests (optional). At least one is required if |
||
| 84 | * you purge and refresh paths instead of |
||
| 85 | * absolute URLs. A request will be sent for each |
||
| 86 | * base URL. |
||
| 87 | * @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
||
| 88 | * client is supplied, a default one is created |
||
| 89 | * @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a |
||
| 90 | * default one is created |
||
| 91 | 38 | */ |
|
| 92 | public function __construct( |
||
| 120 | 33 | ||
| 121 | 1 | /** |
|
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 32 | public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) |
|
| 138 | |||
| 139 | /** |
||
| 140 | 33 | * {@inheritdoc} |
|
| 141 | */ |
||
| 142 | 33 | public function flush() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get the list of servers to send invalidation requests to. |
||
| 184 | * |
||
| 185 | 32 | * @return UriInterface[] |
|
| 186 | */ |
||
| 187 | 32 | protected function getServers() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Duplicate a request for each caching server. |
||
| 194 | * |
||
| 195 | * @param RequestInterface $request The request to duplicate for each configured server |
||
| 196 | * |
||
| 197 | 32 | * @return RequestInterface[] |
|
| 198 | */ |
||
| 199 | 32 | private function fanOut(RequestInterface $request) |
|
| 234 | 32 | ||
| 235 | 32 | /** |
|
| 236 | * Looks at a given request and returns an array of requests incorporating |
||
| 237 | 32 | * every configured base URI. |
|
| 238 | 32 | * |
|
| 239 | 32 | * @param RequestInterface $request The request to modify for every configured base URI |
|
| 240 | 32 | * |
|
| 241 | * @return RequestInterface[] |
||
| 242 | */ |
||
| 243 | private function requestToBaseUris(RequestInterface $request) |
||
| 277 | |||
| 278 | 3 | /** |
|
| 279 | * Set caching proxy server URI objects, validating them. |
||
| 280 | * |
||
| 281 | 32 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
|
| 282 | 31 | * addresses, including port if not port 80. |
|
| 283 | * E.g. ['127.0.0.1:6081'] |
||
| 284 | * |
||
| 285 | * @throws InvalidUrlException If server is invalid or contains URL |
||
| 286 | * parts other than scheme, host, port |
||
| 287 | */ |
||
| 288 | private function setServers(array $servers) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set application base URI that will be prefixed to relative purge and |
||
| 298 | 38 | * refresh requests, and validate it. |
|
| 299 | * |
||
| 300 | 38 | * @param array $baseUris Your application’s base URIs |
|
| 301 | 1 | * |
|
| 302 | 1 | * @throws InvalidUrlException If the base URI is not a valid URI |
|
| 303 | 1 | */ |
|
| 304 | private function setBaseUris(array $baseUris = []) |
||
| 316 | 1 | ||
| 317 | 1 | /** |
|
| 318 | * Filter a URL. |
||
| 319 | * |
||
| 320 | 37 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
|
| 321 | 1 | * for validity. You can specify what parts of the URL are allowed. |
|
| 322 | * |
||
| 323 | * @param string $uriString |
||
| 324 | 36 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
|
| 325 | 36 | * |
|
| 326 | 36 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
|
| 327 | 36 | * |
|
| 328 | 1 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
|
| 329 | * contains parts that are not expected |
||
| 330 | */ |
||
| 331 | private function filterUri($uriString, array $allowedParts = []) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Build a request signature based on the request data. Unique for every different request, identical |
||
| 370 | * for the same requests. |
||
| 371 | * |
||
| 372 | * This signature is used to avoid sending the same invalidation request twice. |
||
| 373 | * |
||
| 374 | * @param RequestInterface $request An invalidation request |
||
| 375 | * |
||
| 376 | * @return string A signature for this request |
||
| 377 | */ |
||
| 378 | private function getRequestSignature(RequestInterface $request) |
||
| 385 | } |
||
| 386 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: