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 Guzzle response. |
||
34 | * |
||
35 | * @var \GuzzleHttp\Psr7\Response |
||
36 | */ |
||
37 | protected $response; |
||
38 | |||
39 | /** |
||
40 | * The request options. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $options = []; |
||
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 = []) |
|
82 | { |
||
83 | 23 | if (is_string($options) || $options instanceof UriInterface) { |
|
84 | 1 | $options = ['base_uri' => $options]; |
|
85 | 23 | } elseif (! is_array($options)) { |
|
86 | throw new InvalidArgumentException('config must be a string, UriInterface, or an array'); |
||
87 | } |
||
88 | |||
89 | 23 | $this->client = new Client( |
|
90 | 23 | $this->options = $options + static::defaultOptions() |
|
91 | 23 | ); |
|
92 | 23 | } |
|
93 | |||
94 | /** |
||
95 | * Get the Guzzle client instance. |
||
96 | * |
||
97 | * @return \GuzzleHttp\Client |
||
98 | */ |
||
99 | 2 | public function getClient() |
|
103 | |||
104 | /** |
||
105 | * Get whether to catch Guzzle exceptions or not. |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function areExceptionsCaught() |
||
113 | |||
114 | /** |
||
115 | * Set whether to catch Guzzle exceptions or not. |
||
116 | * |
||
117 | * @param bool $catch |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function catchExceptions($catch) |
||
126 | |||
127 | /** |
||
128 | * Get the request options. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 9 | public function getOptions() |
|
136 | |||
137 | /** |
||
138 | * Set the request options. |
||
139 | * |
||
140 | * @param array $options |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setOptions(array $options) |
||
149 | |||
150 | /** |
||
151 | * Merge the given options to the request options. |
||
152 | * |
||
153 | * @param array $options |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function mergeOptions(array $options) |
||
160 | |||
161 | /** |
||
162 | * Remove request options using "dot" notation. |
||
163 | * |
||
164 | * @param string|array|null $key |
||
165 | * @return $this |
||
166 | */ |
||
167 | 16 | public function removeOptions($key = null) |
|
168 | { |
||
169 | 16 | if (is_null($key)) { |
|
170 | 16 | $this->options = []; |
|
171 | 16 | } else { |
|
172 | 1 | Arr::forget($this->options, is_array($key) ? $key : func_get_args()); |
|
173 | } |
||
174 | |||
175 | 16 | return $this; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get a request option using "dot" notation. |
||
180 | * |
||
181 | * @param string $key |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 3 | public function getOption($key) |
|
188 | |||
189 | /** |
||
190 | * Set a request option using "dot" notation. |
||
191 | * |
||
192 | * @param string|array $key |
||
193 | * @param mixed $value |
||
194 | * @return $this |
||
195 | */ |
||
196 | 11 | public function option($key, $value = null) |
|
197 | { |
||
198 | 11 | $keys = is_array($key) ? $key : [$key => $value]; |
|
199 | |||
200 | 11 | foreach ($keys as $key => $value) { |
|
201 | 11 | Arr::set($this->options, $key, $value); |
|
202 | 11 | } |
|
203 | |||
204 | 11 | return $this; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * Set the request header. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * @param mixed $value |
||
212 | * @return $this |
||
213 | */ |
||
214 | 4 | public function header($name, $value) |
|
218 | |||
219 | /** |
||
220 | * Set the request content type. |
||
221 | * |
||
222 | * @param string $type |
||
223 | * @return $this |
||
224 | */ |
||
225 | 1 | public function contentType($type) |
|
229 | |||
230 | /** |
||
231 | * Set the request accept type. |
||
232 | * |
||
233 | * @param string $type |
||
234 | * @return $this |
||
235 | */ |
||
236 | 2 | public function accept($type) |
|
240 | |||
241 | /** |
||
242 | * Set the request accept type to JSON. |
||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | 1 | public function acceptJson() |
|
250 | |||
251 | /** |
||
252 | * Specify where the body of a response will be saved. |
||
253 | * Set the "sink" option. |
||
254 | * |
||
255 | * @param mixed $dest |
||
256 | * @return $this |
||
257 | */ |
||
258 | 1 | public function saveTo($dest) |
|
262 | |||
263 | /** |
||
264 | * Get the Guzzle response instance. |
||
265 | * |
||
266 | * @return \GuzzleHttp\Psr7\Response|null |
||
267 | */ |
||
268 | 8 | public function getResponse() |
|
272 | |||
273 | /** |
||
274 | * Get data from the response. |
||
275 | * |
||
276 | * @param string|\Closure $callback |
||
277 | * @param array $parameters |
||
278 | * @param mixed $default |
||
279 | * @return mixed |
||
280 | */ |
||
281 | 1 | protected function getResponseData($callback, array $parameters = [], $default = null) |
|
282 | { |
||
283 | 1 | if ($this->response) { |
|
284 | return $callback instanceof Closure |
||
285 | 1 | ? $callback($this->response, ...$parameters) |
|
286 | 1 | : $this->response->$callback(...$parameters); |
|
287 | } |
||
288 | |||
289 | return $default; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Get the response content. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 1 | public function getContent() |
|
298 | { |
||
299 | 1 | return (string) $this->getBody(); |
|
|
|||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Get the decoded JSON response. |
||
304 | * |
||
305 | * @param bool $assoc |
||
306 | * @return mixed |
||
307 | */ |
||
308 | public function json($assoc = true) |
||
309 | { |
||
310 | if ($this->response) { |
||
311 | return json_decode($this->getContent(), $assoc); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Make request to a URI. |
||
317 | * |
||
318 | * @param string $uri |
||
319 | * @param string $method |
||
320 | * @param array $options |
||
321 | * @return $this |
||
322 | */ |
||
323 | 9 | public function request($uri, $method = 'GET', array $options = []) |
|
324 | { |
||
325 | 9 | $options += $this->options; |
|
326 | |||
327 | 9 | $this->response = null; |
|
328 | |||
329 | try { |
||
330 | 9 | $this->response = $this->client->request($method, $uri, $options); |
|
331 | 9 | } catch (Exception $e) { |
|
332 | 1 | if (! $this->catchExceptions) { |
|
333 | throw $e; |
||
334 | } |
||
335 | } |
||
336 | |||
337 | 9 | return $this; |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * Make request to a URI, expecting JSON content. |
||
342 | * |
||
343 | * @param string $uri |
||
344 | * @param string $method |
||
345 | * @param array $options |
||
346 | * @return $this |
||
347 | */ |
||
348 | 3 | public function requestJson($uri, $method = 'GET', array $options = []) |
|
354 | |||
355 | /** |
||
356 | * Add JSON type to the "Accept" header for the request options. |
||
357 | * |
||
358 | * @param array $options |
||
359 | * @return array |
||
360 | */ |
||
361 | 3 | protected function addAcceptableJsonType(array $options) |
|
372 | |||
373 | /** |
||
374 | * Request the URI and return the response content. |
||
375 | * |
||
376 | * @param string $uri |
||
377 | * @param string $method |
||
378 | * @param array $options |
||
379 | * @return string|null |
||
380 | */ |
||
381 | 1 | public function fetchContent($uri, $method = 'GET', array $options = []) |
|
385 | |||
386 | /** |
||
387 | * Request the URI and return the JSON decoded response content. |
||
388 | * |
||
389 | * @param string $uri |
||
390 | * @param string $method |
||
391 | * @param array $options |
||
392 | * @param bool $assoc |
||
393 | * @return mixed |
||
394 | */ |
||
395 | 1 | public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
|
399 | |||
400 | /** |
||
401 | * Get the dynamic response methods. |
||
402 | * |
||
403 | * @return array |
||
404 | */ |
||
405 | 6 | protected function getDynamicResponseMethods() |
|
412 | |||
413 | /** |
||
414 | * Dynamically methods to set request option, send request, or get |
||
415 | * response properties. |
||
416 | * |
||
417 | * @param string $method |
||
418 | * @param array $args |
||
419 | * @return mixed |
||
420 | */ |
||
421 | 8 | public function __call($method, $args) |
|
442 | } |
||
443 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: