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 |
||
12 | class HttpClient |
||
13 | { |
||
14 | /** |
||
15 | * The default request options. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $defaultOptions = [ |
||
20 | 'connect_timeout' => 5, |
||
21 | 'timeout' => 25, |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * The Guzzle client. |
||
26 | * |
||
27 | * @var \GuzzleHttp\Client |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * The Guzzle response. |
||
33 | * |
||
34 | * @var \GuzzleHttp\Psr7\Response |
||
35 | */ |
||
36 | protected $response; |
||
37 | |||
38 | /** |
||
39 | * The request options. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $options = []; |
||
44 | |||
45 | /** |
||
46 | * Indicate whether to catch Guzzle exceptions. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $catchExceptions = true; |
||
51 | |||
52 | /** |
||
53 | * Get the default request options. |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 24 | public static function defaultOptions() |
|
61 | |||
62 | /** |
||
63 | * Set the default request options. |
||
64 | * |
||
65 | * @param array $options |
||
66 | * @return void |
||
67 | */ |
||
68 | 1 | public static function setDefaultOptions(array $options) |
|
72 | |||
73 | /** |
||
74 | * Create a http client instance. |
||
75 | * |
||
76 | * @param array|string|\Psr\Http\Message\UriInterface $config base URI or any request options |
||
77 | */ |
||
78 | 23 | public function __construct($config = []) |
|
90 | |||
91 | /** |
||
92 | * Get the Guzzle client instance. |
||
93 | * |
||
94 | * @return \GuzzleHttp\Client |
||
95 | */ |
||
96 | 2 | public function getClient() |
|
100 | |||
101 | /** |
||
102 | * Get whether to catch Guzzle exceptions or not. |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function areExceptionsCaught() |
||
110 | |||
111 | /** |
||
112 | * Set whether to catch Guzzle exceptions or not. |
||
113 | * |
||
114 | * @param bool $catch |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function catchExceptions($catch) |
||
123 | |||
124 | /** |
||
125 | * Get the request options. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 9 | public function getOptions() |
|
133 | |||
134 | /** |
||
135 | * Set the request options. |
||
136 | * |
||
137 | * @param array $options |
||
138 | * @return $this |
||
139 | */ |
||
140 | 2 | public function setOptions(array $options) |
|
146 | |||
147 | /** |
||
148 | * Merge the request options. |
||
149 | * |
||
150 | * @param array $options |
||
151 | * @return $this |
||
152 | */ |
||
153 | 2 | public function options(array $options) |
|
157 | |||
158 | /** |
||
159 | * Remove one or many options using "dot" notation. |
||
160 | * |
||
161 | * @param string|array|null $key |
||
162 | * @return $this |
||
163 | */ |
||
164 | 16 | public function removeOptions($key = null) |
|
174 | |||
175 | /** |
||
176 | * Set a request option using "dot" notation. |
||
177 | * |
||
178 | * @param string|array $key |
||
179 | * @param mixed $value |
||
180 | * @return $this |
||
181 | */ |
||
182 | 8 | public function option($key, $value = null) |
|
192 | |||
193 | /** |
||
194 | * Get a request option using "dot" notation. |
||
195 | * |
||
196 | * @param string $key |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 3 | public function getOption($key) |
|
203 | |||
204 | /** |
||
205 | * Set the request header. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * @param mixed $value |
||
209 | * @return $this |
||
210 | */ |
||
211 | 4 | public function header($name, $value) |
|
215 | |||
216 | /** |
||
217 | * Set the request content type. |
||
218 | * |
||
219 | * @param string $type |
||
220 | * @return $this |
||
221 | */ |
||
222 | 1 | public function contentType($type) |
|
226 | |||
227 | /** |
||
228 | * Set the request accept type. |
||
229 | * |
||
230 | * @param string $type |
||
231 | * @return $this |
||
232 | */ |
||
233 | 2 | public function accept($type) |
|
237 | |||
238 | /** |
||
239 | * Set the request accept type to JSON. |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | 1 | public function acceptJson() |
|
247 | |||
248 | /** |
||
249 | * Specify where the body of a response will be saved. |
||
250 | * Set the "sink" option. |
||
251 | * |
||
252 | * @param mixed $value |
||
253 | * @return $this |
||
254 | */ |
||
255 | 1 | public function saveTo($value) |
|
259 | |||
260 | /** |
||
261 | * Get the Guzzle response instance. |
||
262 | * |
||
263 | * @return \GuzzleHttp\Psr7\Response|null |
||
264 | */ |
||
265 | 8 | public function getResponse() |
|
269 | |||
270 | /** |
||
271 | * Get the status code of response. |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | public function getStatusCode() |
||
281 | |||
282 | /** |
||
283 | * Get the response header value. |
||
284 | * |
||
285 | * @param string $name |
||
286 | * @return mixed |
||
287 | */ |
||
288 | public function getHeader($name) |
||
294 | |||
295 | /** |
||
296 | * Get all response headers values. |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | public function getHeaders() |
||
304 | |||
305 | /** |
||
306 | * Get response body. |
||
307 | * |
||
308 | * @return \GuzzleHttp\Psr7\Stream|null |
||
309 | */ |
||
310 | 2 | public function getBody() |
|
316 | |||
317 | /** |
||
318 | * Get response content. |
||
319 | * |
||
320 | * @return string|null |
||
321 | */ |
||
322 | 2 | public function getContent() |
|
328 | |||
329 | /** |
||
330 | * Get JSON decoded response content. |
||
331 | * |
||
332 | * @param bool $assoc |
||
333 | * @return mixed |
||
334 | */ |
||
335 | 1 | public function getJson($assoc = true) |
|
341 | |||
342 | /** |
||
343 | * Make request to a URI. |
||
344 | * |
||
345 | * @param string $uri |
||
346 | * @param string $method |
||
347 | * @param array $options |
||
348 | * @return $this |
||
349 | */ |
||
350 | 9 | public function request($uri, $method = 'GET', array $options = []) |
|
364 | |||
365 | /** |
||
366 | * Make request to a URI, expecting JSON content. |
||
367 | * |
||
368 | * @param string $uri |
||
369 | * @param string $method |
||
370 | * @param array $options |
||
371 | * @return $this |
||
372 | */ |
||
373 | 3 | public function requestJson($uri, $method = 'GET', array $options = []) |
|
379 | |||
380 | /** |
||
381 | * Request the URI and return the response content. |
||
382 | * |
||
383 | * @param string $uri |
||
384 | * @param string $method |
||
385 | * @param array $options |
||
386 | * @return string|null |
||
387 | */ |
||
388 | 1 | public function fetchContent($uri, $method = 'GET', array $options = []) |
|
392 | |||
393 | /** |
||
394 | * Request the URI and return the JSON decoded response content. |
||
395 | * |
||
396 | * @param string $uri |
||
397 | * @param string $method |
||
398 | * @param array $options |
||
399 | * @param bool $assoc |
||
400 | * @return mixed |
||
401 | */ |
||
402 | 1 | public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
|
406 | |||
407 | /** |
||
408 | * Dynamically methods to set request option, send request, or get |
||
409 | * response properties. |
||
410 | * |
||
411 | * @param string $method |
||
412 | * @param array $args |
||
413 | * @return mixed |
||
414 | */ |
||
415 | 4 | public function __call($method, $args) |
|
432 | } |
||
433 |