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 |
||
13 | class HttpClient |
||
14 | { |
||
15 | /** |
||
16 | * The default request options. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected static $defaultOptions = [ |
||
21 | 'connect_timeout' => 5, |
||
22 | 'timeout' => 25, |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * The Guzzle client. |
||
27 | * |
||
28 | * @var \GuzzleHttp\Client |
||
29 | */ |
||
30 | protected $client; |
||
31 | |||
32 | /** |
||
33 | * The request options. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $options = []; |
||
38 | |||
39 | /** |
||
40 | * The Guzzle response. |
||
41 | * |
||
42 | * @var \GuzzleHttp\Psr7\Response |
||
43 | */ |
||
44 | protected $response; |
||
45 | |||
46 | /** |
||
47 | * Indicate whether to catch Guzzle exceptions. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $catchExceptions = true; |
||
52 | |||
53 | /** |
||
54 | * Get the default request options. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 24 | public static function defaultOptions() |
|
62 | |||
63 | /** |
||
64 | * Set the default request options. |
||
65 | * |
||
66 | * @param array $options |
||
67 | * @return void |
||
68 | */ |
||
69 | 1 | public static function setDefaultOptions(array $options) |
|
73 | |||
74 | /** |
||
75 | * Create a http client instance. |
||
76 | * |
||
77 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
78 | * |
||
79 | * @throws \InvalidArgumentException |
||
80 | */ |
||
81 | 23 | public function __construct($options = []) |
|
95 | |||
96 | /** |
||
97 | * Get the Guzzle client instance. |
||
98 | * |
||
99 | * @return \GuzzleHttp\Client |
||
100 | */ |
||
101 | 2 | public function getClient() |
|
105 | |||
106 | /** |
||
107 | * Get whether to catch Guzzle exceptions or not. |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function areExceptionsCaught() |
||
115 | |||
116 | /** |
||
117 | * Set whether to catch Guzzle exceptions or not. |
||
118 | * |
||
119 | * @param bool $catch |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function catchExceptions($catch) |
||
128 | |||
129 | /** |
||
130 | * Get the request options. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 3 | public function getOptions() |
|
138 | |||
139 | /** |
||
140 | * Merge the given options to the request options. |
||
141 | * |
||
142 | * @param array $options,... |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function mergeOptions(array ...$options) |
||
151 | |||
152 | /** |
||
153 | * Remove one or many request options using "dot" notation. |
||
154 | * |
||
155 | * @param array|string $keys |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function removeOptions($keys) |
||
164 | |||
165 | /** |
||
166 | * Get a request option using "dot" notation. |
||
167 | * |
||
168 | * @param string $key |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 1 | public function getOption($key) |
|
175 | |||
176 | /** |
||
177 | * Set a request option using "dot" notation. |
||
178 | * |
||
179 | * @param string|array $key |
||
180 | * @param mixed $value |
||
181 | * @return $this |
||
182 | */ |
||
183 | 1 | public function option($key, $value = null) |
|
193 | |||
194 | /** |
||
195 | * Set a request header. |
||
196 | * |
||
197 | * @param string $name |
||
198 | * @param mixed $value |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function header($name, $value) |
||
205 | |||
206 | /** |
||
207 | * Set the request content type. |
||
208 | * |
||
209 | * @param string $type |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function contentType($type) |
||
216 | |||
217 | /** |
||
218 | * Set the request accept type. |
||
219 | * |
||
220 | * @param string $type |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function accept($type) |
||
227 | |||
228 | /** |
||
229 | * Set the request accept type to "application/json". |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function acceptJson() |
||
237 | |||
238 | /** |
||
239 | * Specify where the body of the response will be saved. |
||
240 | * Set the "sink" option. |
||
241 | * |
||
242 | * @param mixed $dest |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function saveTo($dest) |
||
249 | |||
250 | /** |
||
251 | * Get the Guzzle response instance. |
||
252 | * |
||
253 | * @return \GuzzleHttp\Psr7\Response|null |
||
254 | */ |
||
255 | public function getResponse() |
||
259 | |||
260 | /** |
||
261 | * Get data from the response. |
||
262 | * |
||
263 | * @param string|\Closure $callback |
||
264 | * @param array $parameters |
||
265 | * @param mixed $default |
||
266 | * @return mixed |
||
267 | */ |
||
268 | protected function getResponseData($callback, array $parameters = [], $default = null) |
||
278 | |||
279 | /** |
||
280 | * Get the response content. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | public function getContent() |
||
288 | |||
289 | /** |
||
290 | * Get the JSON-decoded response content. |
||
291 | * |
||
292 | * @param bool $assoc |
||
293 | * @return mixed |
||
294 | */ |
||
295 | public function json($assoc = true) |
||
299 | |||
300 | /** |
||
301 | * Make request to a URI. |
||
302 | * |
||
303 | * @param string $uri |
||
304 | * @param string $method |
||
305 | * @param array $options |
||
306 | * @return $this |
||
307 | */ |
||
308 | 2 | public function request($uri = '', $method = 'GET', array $options = []) |
|
324 | |||
325 | /** |
||
326 | * Make request to a URI, expecting JSON content. |
||
327 | * |
||
328 | * @param string $uri |
||
329 | * @param string $method |
||
330 | * @param array $options |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
||
341 | |||
342 | /** |
||
343 | * Add JSON type to the "Accept" header for the request options. |
||
344 | * |
||
345 | * @param array $options |
||
346 | * @return array |
||
347 | */ |
||
348 | protected function addAcceptableJsonType(array $options) |
||
359 | |||
360 | /** |
||
361 | * Request the URI and return the response content. |
||
362 | * |
||
363 | * @param string $uri |
||
364 | * @param string $method |
||
365 | * @param array $options |
||
366 | * @return string |
||
367 | */ |
||
368 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
||
372 | |||
373 | /** |
||
374 | * Request the URI and return the JSON-decoded response content. |
||
375 | * |
||
376 | * @param string $uri |
||
377 | * @param string $method |
||
378 | * @param array $options |
||
379 | * @return mixed |
||
380 | */ |
||
381 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
||
385 | |||
386 | /** |
||
387 | * Get the dynamic request methods. |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | 2 | protected function getDynamicRequestMethods() |
|
397 | |||
398 | /** |
||
399 | * Get the dynamic requestJson methods. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | 1 | protected function getDynamicRequestJsonMethods() |
|
409 | |||
410 | /** |
||
411 | * Get the dynamic response methods. |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | 1 | protected function getDynamicResponseMethods() |
|
422 | |||
423 | /** |
||
424 | * Insert HTTP method to the parameters. |
||
425 | * |
||
426 | * @param array $parameters |
||
427 | * @return array |
||
428 | */ |
||
429 | 1 | protected function insertHttpMethodToParameters($method, array $parameters) |
|
441 | |||
442 | /** |
||
443 | * Dynamically send request, get response data, or set request option. |
||
444 | * |
||
445 | * @param string $method |
||
446 | * @param array $parameters |
||
447 | * @return mixed |
||
448 | */ |
||
449 | 2 | public function __call($method, $parameters) |
|
469 | } |
||
470 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..