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( |
||
| 127 | 1 | ||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | 32 | */ |
|
| 131 | 32 | public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) |
|
| 145 | 33 | ||
| 146 | /** |
||
| 147 | 33 | * {@inheritdoc} |
|
| 148 | */ |
||
| 149 | 33 | public function flush() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get the list of servers to send invalidation requests to. |
||
| 191 | * |
||
| 192 | * @return UriInterface[] |
||
| 193 | */ |
||
| 194 | protected function getServers() |
||
| 198 | |||
| 199 | 32 | /** |
|
| 200 | * Duplicate a request for each caching server. |
||
| 201 | 32 | * |
|
| 202 | * @param RequestInterface $request The request to duplicate for each configured server |
||
| 203 | * |
||
| 204 | * @return RequestInterface[] |
||
| 205 | 32 | */ |
|
| 206 | 30 | private function fanOut(RequestInterface $request) |
|
| 240 | 32 | ||
| 241 | /** |
||
| 242 | * Looks at a given request and returns an array of requests incorporating |
||
| 243 | * every configured base URI. |
||
| 244 | 32 | * |
|
| 245 | * @param RequestInterface $request The request to modify for every configured base URI |
||
| 246 | * |
||
| 247 | * @return RequestInterface[] |
||
| 248 | */ |
||
| 249 | private function requestToBaseUris(RequestInterface $request) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set caching proxy server URI objects, validating them. |
||
| 286 | * |
||
| 287 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
||
| 288 | * addresses, including port if not port 80. |
||
| 289 | * E.g. ['127.0.0.1:6081'] |
||
| 290 | * |
||
| 291 | * @throws InvalidUrlException If server is invalid or contains URL |
||
| 292 | * parts other than scheme, host, port |
||
| 293 | */ |
||
| 294 | private function setServers(array $servers) |
||
| 301 | 1 | ||
| 302 | 1 | /** |
|
| 303 | 1 | * Set application base URI that will be prefixed to relative purge and |
|
| 304 | * refresh requests, and validate it. |
||
| 305 | * |
||
| 306 | * @param array $baseUris Your application’s base URIs |
||
| 307 | * |
||
| 308 | * @throws InvalidUrlException If the base URI is not a valid URI |
||
| 309 | */ |
||
| 310 | 38 | private function setBaseUris(array $baseUris = []) |
|
| 322 | |||
| 323 | /** |
||
| 324 | 36 | * Filter a URL. |
|
| 325 | 36 | * |
|
| 326 | 36 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
|
| 327 | 36 | * for validity. You can specify what parts of the URL are allowed. |
|
| 328 | 1 | * |
|
| 329 | * @param string $uriString |
||
| 330 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
||
| 331 | * |
||
| 332 | 35 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
|
| 333 | * |
||
| 334 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
||
| 335 | * contains parts that are not expected |
||
| 336 | */ |
||
| 337 | private function filterUri($uriString, array $allowedParts = []) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Build a request signature based on the request data. Unique for every different request, identical |
||
| 369 | * for the same requests. |
||
| 370 | * |
||
| 371 | * This signature is used to avoid sending the same invalidation request twice. |
||
| 372 | * |
||
| 373 | * @param RequestInterface $request An invalidation request |
||
| 374 | * |
||
| 375 | * @return string A signature for this request |
||
| 376 | */ |
||
| 377 | private function getRequestSignature(RequestInterface $request) |
||
| 384 | } |
||
| 385 |
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: