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 | * Merge request options. |
||
126 | * |
||
127 | * @param array $options |
||
128 | * @return $this |
||
129 | */ |
||
130 | 2 | public function options(array ...$options) |
|
136 | |||
137 | /** |
||
138 | * Remove one or many options using "dot" notation. |
||
139 | * |
||
140 | * @param string|array|null $key |
||
141 | * @return $this |
||
142 | */ |
||
143 | 16 | public function removeOptions($key = null) |
|
153 | |||
154 | /** |
||
155 | * Set a request option using "dot" notation. |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @param mixed $value |
||
159 | * @return $this |
||
160 | */ |
||
161 | 7 | public function option($key, $value) |
|
169 | |||
170 | /** |
||
171 | * Get a request option using "dot" notation. |
||
172 | * |
||
173 | * @param string $key |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 3 | public function getOption($key) |
|
180 | |||
181 | /** |
||
182 | * Set the request header. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param mixed $value |
||
186 | * @return $this |
||
187 | */ |
||
188 | 4 | public function header($name, $value) |
|
192 | |||
193 | /** |
||
194 | * Set the request content type. |
||
195 | * |
||
196 | * @param string $type |
||
197 | * @return $this |
||
198 | */ |
||
199 | 1 | public function contentType($type) |
|
203 | |||
204 | /** |
||
205 | * Set the request accept type. |
||
206 | * |
||
207 | * @param string $type |
||
208 | * @return $this |
||
209 | */ |
||
210 | 2 | public function accept($type) |
|
214 | |||
215 | /** |
||
216 | * Set the request accept type to JSON. |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | 1 | public function acceptJson() |
|
224 | |||
225 | /** |
||
226 | * Specify where the body of a response will be saved. |
||
227 | * Set the "sink" option. |
||
228 | * |
||
229 | * @param mixed $value |
||
230 | * @return $this |
||
231 | */ |
||
232 | 1 | public function saveTo($value) |
|
236 | |||
237 | /** |
||
238 | * Get the Guzzle response instance. |
||
239 | * |
||
240 | * @return \GuzzleHttp\Psr7\Response|null |
||
241 | */ |
||
242 | 8 | public function getResponse() |
|
246 | |||
247 | /** |
||
248 | * Get the status code of response. |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | public function getStatusCode() |
||
258 | |||
259 | /** |
||
260 | * Get the response header value. |
||
261 | * |
||
262 | * @param string $name |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function getHeader($name) |
||
271 | |||
272 | /** |
||
273 | * Get all response headers values. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getHeaders() |
||
281 | |||
282 | /** |
||
283 | * Get response body. |
||
284 | * |
||
285 | * @return \GuzzleHttp\Psr7\Stream|null |
||
286 | */ |
||
287 | 2 | public function getBody() |
|
293 | |||
294 | /** |
||
295 | * Get response content. |
||
296 | * |
||
297 | * @return string|null |
||
298 | */ |
||
299 | 2 | public function getContent() |
|
305 | |||
306 | /** |
||
307 | * Get JSON decoded response content. |
||
308 | * |
||
309 | * @param bool $assoc |
||
310 | * @return mixed |
||
311 | */ |
||
312 | 1 | public function getJson($assoc = true) |
|
318 | |||
319 | /** |
||
320 | * Make request to a URL. |
||
321 | * |
||
322 | * @param string $url |
||
323 | * @param string $method |
||
324 | * @param array $options |
||
325 | * @return $this |
||
326 | */ |
||
327 | 9 | public function request($url, $method = 'GET', $options = []) |
|
341 | |||
342 | /** |
||
343 | * Make request to a URL, expecting JSON content. |
||
344 | * |
||
345 | * @param string $url |
||
346 | * @param string $method |
||
347 | * @param array $options |
||
348 | * @return $this |
||
349 | */ |
||
350 | 3 | public function requestJson($url, $method = 'GET', $options = []) |
|
356 | |||
357 | /** |
||
358 | * Request the URL and return the response content. |
||
359 | * |
||
360 | * @param string $url |
||
361 | * @param string $method |
||
362 | * @param array $options |
||
363 | * @return string|null |
||
364 | */ |
||
365 | 1 | public function fetchContent($url, $method = 'GET', $options = []) |
|
369 | |||
370 | /** |
||
371 | * Request the URL and return the JSON decoded response content. |
||
372 | * |
||
373 | * @param string $url |
||
374 | * @param string $method |
||
375 | * @param array $options |
||
376 | * @param bool $assoc |
||
377 | * @return mixed |
||
378 | */ |
||
379 | 1 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
|
383 | |||
384 | 3 | public function __call($method, $args) |
|
401 | } |
||
402 |