Complex classes like HttpClient 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 HttpClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HttpClient extends Client |
||
21 | { |
||
22 | public const OPTION_HANDLER_RESPONSE = 'handler_response'; |
||
23 | public const OPTION_CACHE_TTL = 'cache_ttl'; |
||
24 | public const OPTION_NO_CACHE = 'no_cache'; |
||
25 | public const OPTION_CACHE_KEY = 'cache_key'; |
||
26 | |||
27 | /** |
||
28 | * @internal |
||
29 | */ |
||
30 | private const CACHE_KEY = 'gplay.v1.%s.%s'; |
||
31 | |||
32 | /** |
||
33 | * Number of attempts with HTTP error (except 404) |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | private $retryLimit; |
||
38 | /** |
||
39 | * @var CacheInterface|null |
||
40 | */ |
||
41 | private $cache; |
||
42 | |||
43 | /** |
||
44 | * HttpClient constructor. |
||
45 | * |
||
46 | * @param array $config |
||
47 | * @param int $retryLimit |
||
48 | * @param CacheInterface|null $cache |
||
49 | */ |
||
50 | public function __construct(array $config = [], int $retryLimit = 4, ?CacheInterface $cache = null) |
||
184 | |||
185 | /** |
||
186 | * @param CacheInterface|null $cache |
||
187 | * @return self |
||
188 | */ |
||
189 | public function setCache(?CacheInterface $cache): self |
||
194 | |||
195 | /** |
||
196 | * @param string|null $proxy |
||
197 | * @return self |
||
198 | */ |
||
199 | public function setProxy(?string $proxy): self |
||
206 | |||
207 | /** |
||
208 | * @param int $retryLimit |
||
209 | * @return self |
||
210 | */ |
||
211 | public function setRetryLimit(int $retryLimit): self |
||
216 | |||
217 | /** |
||
218 | * @param string $key |
||
219 | * @param string $value |
||
220 | * @return HttpClient |
||
221 | */ |
||
222 | public function setHttpHeader(string $key, ?string $value): self |
||
236 | |||
237 | /** |
||
238 | * @param \DateInterval|int|null $ttl |
||
239 | * @return HttpClient |
||
240 | */ |
||
241 | public function setCacheTtl($ttl): self |
||
251 | |||
252 | /** |
||
253 | * @param float $connectTimeout |
||
254 | * @return HttpClient |
||
255 | */ |
||
256 | public function setConnectTimeout(float $connectTimeout): self |
||
266 | |||
267 | /** |
||
268 | * @param float $timeout |
||
269 | * @return HttpClient |
||
270 | */ |
||
271 | public function setTimeout(float $timeout): self |
||
281 | |||
282 | /** |
||
283 | * @param array $config |
||
284 | */ |
||
285 | protected function mergeConfig(array $config): void |
||
296 | |||
297 | /** |
||
298 | * @param array $config |
||
299 | */ |
||
300 | protected function setConfig(array $config): void |
||
313 | |||
314 | /** |
||
315 | * @param RequestInterface $request |
||
316 | * @return string |
||
317 | */ |
||
318 | private static function getRequestHash(RequestInterface $request): string |
||
331 | |||
332 | /** |
||
333 | * @param string $method |
||
334 | * @param iterable $urls |
||
335 | * @param array $options |
||
336 | * @param int $concurrency |
||
337 | * @return array |
||
338 | * @throws GuzzleException |
||
339 | */ |
||
340 | public function requestAsyncPool(string $method, iterable $urls, array $options = [], int $concurrency = 4): array |
||
351 | |||
352 | /** |
||
353 | * @param string $method |
||
354 | * @param iterable $urls |
||
355 | * @param array $options |
||
356 | * @return \Generator |
||
357 | */ |
||
358 | private function requestGenerator(string $method, iterable $urls, array $options): \Generator |
||
364 | } |
||
365 |