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 | * Indicates whether throws Guzzle exceptions. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $withExceptions = false; |
||
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 | * Trun on/off Guzzle exceptions. |
||
103 | * |
||
104 | * @param bool $throws |
||
105 | * @return $this |
||
106 | */ |
||
107 | 1 | public function withExceptions($throws) |
|
113 | |||
114 | /** |
||
115 | * Get the request options. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | 9 | public function getOptions() |
|
123 | |||
124 | /** |
||
125 | * Set the request options. |
||
126 | * |
||
127 | * @param array $options |
||
128 | * @return $this |
||
129 | */ |
||
130 | 2 | public function setOptions(array $options) |
|
136 | |||
137 | /** |
||
138 | * Merge the request options. |
||
139 | * |
||
140 | * @param array $options |
||
141 | * @return $this |
||
142 | */ |
||
143 | 2 | public function options(array $options) |
|
147 | |||
148 | /** |
||
149 | * Remove one or many options using "dot" notation. |
||
150 | * |
||
151 | * @param string|array|null $key |
||
152 | * @return $this |
||
153 | */ |
||
154 | 16 | public function removeOptions($key = null) |
|
164 | |||
165 | /** |
||
166 | * Set a request option using "dot" notation. |
||
167 | * |
||
168 | * @param string $key |
||
169 | * @param mixed $value |
||
170 | * @return $this |
||
171 | */ |
||
172 | 7 | public function option($key, $value) |
|
180 | |||
181 | /** |
||
182 | * Get a request option using "dot" notation. |
||
183 | * |
||
184 | * @param string $key |
||
185 | * @return mixed |
||
186 | */ |
||
187 | 3 | public function getOption($key) |
|
191 | |||
192 | /** |
||
193 | * Set the request header. |
||
194 | * |
||
195 | * @param string $name |
||
196 | * @param mixed $value |
||
197 | * @return $this |
||
198 | */ |
||
199 | 4 | public function header($name, $value) |
|
203 | |||
204 | /** |
||
205 | * Set the request content type. |
||
206 | * |
||
207 | * @param string $type |
||
208 | * @return $this |
||
209 | */ |
||
210 | 1 | public function contentType($type) |
|
214 | |||
215 | /** |
||
216 | * Set the request accept type. |
||
217 | * |
||
218 | * @param string $type |
||
219 | * @return $this |
||
220 | */ |
||
221 | 2 | public function accept($type) |
|
225 | |||
226 | /** |
||
227 | * Set the request accept type to JSON. |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | 1 | public function acceptJson() |
|
235 | |||
236 | /** |
||
237 | * Specify where the body of a response will be saved. |
||
238 | * Set the "sink" option. |
||
239 | * |
||
240 | * @param mixed $value |
||
241 | * @return $this |
||
242 | */ |
||
243 | 1 | public function saveTo($value) |
|
247 | |||
248 | /** |
||
249 | * Get the Guzzle response instance. |
||
250 | * |
||
251 | * @return \GuzzleHttp\Psr7\Response|null |
||
252 | */ |
||
253 | 8 | public function getResponse() |
|
257 | |||
258 | /** |
||
259 | * Get the status code of response. |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | public function getStatusCode() |
||
269 | |||
270 | /** |
||
271 | * Get the response header value. |
||
272 | * |
||
273 | * @param string $name |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function getHeader($name) |
||
282 | |||
283 | /** |
||
284 | * Get all response headers values. |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | public function getHeaders() |
||
292 | |||
293 | /** |
||
294 | * Get response body. |
||
295 | * |
||
296 | * @return \GuzzleHttp\Psr7\Stream|null |
||
297 | */ |
||
298 | 2 | public function getBody() |
|
304 | |||
305 | /** |
||
306 | * Get response content. |
||
307 | * |
||
308 | * @return string|null |
||
309 | */ |
||
310 | 2 | public function getContent() |
|
316 | |||
317 | /** |
||
318 | * Get JSON decoded response content. |
||
319 | * |
||
320 | * @param bool $assoc |
||
321 | * @return mixed |
||
322 | */ |
||
323 | 1 | public function getJson($assoc = true) |
|
329 | |||
330 | /** |
||
331 | * Make request to a URL. |
||
332 | * |
||
333 | * @param string $url |
||
334 | * @param string $method |
||
335 | * @param array $options |
||
336 | * @return $this |
||
337 | */ |
||
338 | 9 | public function request($url, $method = 'GET', $options = []) |
|
352 | |||
353 | /** |
||
354 | * Make request to a URL, expecting JSON content. |
||
355 | * |
||
356 | * @param string $url |
||
357 | * @param string $method |
||
358 | * @param array $options |
||
359 | * @return $this |
||
360 | */ |
||
361 | 3 | public function requestJson($url, $method = 'GET', $options = []) |
|
367 | |||
368 | /** |
||
369 | * Request the URL and return the response content. |
||
370 | * |
||
371 | * @param string $url |
||
372 | * @param string $method |
||
373 | * @param array $options |
||
374 | * @return string|null |
||
375 | */ |
||
376 | 1 | public function fetchContent($url, $method = 'GET', $options = []) |
|
380 | |||
381 | /** |
||
382 | * Request the URL and return the JSON decoded response content. |
||
383 | * |
||
384 | * @param string $url |
||
385 | * @param string $method |
||
386 | * @param array $options |
||
387 | * @param bool $assoc |
||
388 | * @return mixed |
||
389 | */ |
||
390 | 1 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
|
394 | |||
395 | 3 | public function __call($method, $args) |
|
412 | } |
||
413 |