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 |
||
68 | class HttpClient |
||
69 | { |
||
70 | /** |
||
71 | * The default request options. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected static $defaultOptions = [ |
||
76 | 'connect_timeout' => 5, |
||
77 | 'timeout' => 30, |
||
78 | 'http_errors' => false, |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * The Guzzle client. |
||
83 | * |
||
84 | * @var \GuzzleHttp\Client |
||
85 | */ |
||
86 | protected $client; |
||
87 | |||
88 | /** |
||
89 | * The request options. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $options = []; |
||
94 | |||
95 | /** |
||
96 | * The Guzzle response. |
||
97 | * |
||
98 | * @var \GuzzleHttp\Psr7\Response |
||
99 | */ |
||
100 | protected $response; |
||
101 | |||
102 | /** |
||
103 | * Indicate whether to catch Guzzle exceptions. |
||
104 | * |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $catchExceptions = true; |
||
108 | |||
109 | /** |
||
110 | * Get the default request options. |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | 26 | public static function defaultOptions() |
|
118 | |||
119 | /** |
||
120 | * Set the default request options. |
||
121 | * |
||
122 | * @param array $options |
||
123 | * @return void |
||
124 | */ |
||
125 | 27 | public static function setDefaultOptions(array $options) |
|
129 | |||
130 | /** |
||
131 | * Create a http client instance. |
||
132 | * |
||
133 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
134 | * |
||
135 | * @throws \InvalidArgumentException |
||
136 | */ |
||
137 | 26 | public function __construct($options = []) |
|
151 | |||
152 | /** |
||
153 | * Get the Guzzle client instance. |
||
154 | * |
||
155 | * @return \GuzzleHttp\Client |
||
156 | */ |
||
157 | 4 | public function getClient() |
|
161 | |||
162 | /** |
||
163 | * Get whether to catch Guzzle exceptions or not. |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 1 | public function areExceptionsCaught() |
|
171 | |||
172 | /** |
||
173 | * Set whether to catch Guzzle exceptions or not. |
||
174 | * |
||
175 | * @param bool $catch |
||
176 | * @return $this |
||
177 | */ |
||
178 | 2 | public function catchExceptions($catch) |
|
184 | |||
185 | /** |
||
186 | * Get the request options using "dot" notation. |
||
187 | * |
||
188 | * @param string|null $key |
||
189 | * @return mixed |
||
190 | */ |
||
191 | 11 | public function getOption($key = null) |
|
195 | |||
196 | /** |
||
197 | * Set the request options using "dot" notation. |
||
198 | * |
||
199 | * @param string|array $key |
||
200 | * @param mixed $value |
||
201 | * @return $this |
||
202 | */ |
||
203 | 8 | public function option($key, $value = null) |
|
213 | |||
214 | /** |
||
215 | * Merge the given options to the request options. |
||
216 | * |
||
217 | * @param array ...$options |
||
218 | * @return $this |
||
219 | */ |
||
220 | 1 | public function mergeOptions(array ...$options) |
|
226 | |||
227 | /** |
||
228 | * Remove one or many request options using "dot" notation. |
||
229 | * |
||
230 | * @param array|string $keys |
||
231 | * @return $this |
||
232 | */ |
||
233 | 2 | public function removeOption($keys) |
|
239 | |||
240 | /** |
||
241 | * Set a request header. |
||
242 | * |
||
243 | * @param string $name |
||
244 | * @param mixed $value |
||
245 | * @return $this |
||
246 | */ |
||
247 | 5 | public function header($name, $value) |
|
251 | |||
252 | /** |
||
253 | * Set the request content type. |
||
254 | * |
||
255 | * @param string $type |
||
256 | * @return $this |
||
257 | */ |
||
258 | 1 | public function contentType($type) |
|
262 | |||
263 | /** |
||
264 | * Set the request accept type. |
||
265 | * |
||
266 | * @param string $type |
||
267 | * @return $this |
||
268 | */ |
||
269 | 3 | public function accept($type) |
|
273 | |||
274 | /** |
||
275 | * Set the request accept type to "application/json". |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | 1 | public function acceptJson() |
|
283 | |||
284 | /** |
||
285 | * Specify where the body of the response will be saved. |
||
286 | * Set the "sink" option. |
||
287 | * |
||
288 | * @param string|resource|\Psr\Http\Message\StreamInterface $dest |
||
289 | * @return $this |
||
290 | */ |
||
291 | 1 | public function saveTo($dest) |
|
295 | |||
296 | /** |
||
297 | * Get the Guzzle response instance. |
||
298 | * |
||
299 | * @return \GuzzleHttp\Psr7\Response|null |
||
300 | */ |
||
301 | 3 | public function getResponse() |
|
305 | |||
306 | /** |
||
307 | * Get data from the response. |
||
308 | * |
||
309 | * @param string|\Closure $callback |
||
310 | * @param array $parameters |
||
311 | * @param mixed $default |
||
312 | * @return mixed |
||
313 | */ |
||
314 | 5 | protected function getResponseData($callback, array $parameters = [], $default = null) |
|
324 | |||
325 | /** |
||
326 | * Get the response content. |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | 4 | public function getContent() |
|
334 | |||
335 | /** |
||
336 | * Get the JSON-decoded response content. |
||
337 | * |
||
338 | * @param bool $assoc |
||
339 | * @return mixed |
||
340 | */ |
||
341 | 2 | public function getJsonContent($assoc = true) |
|
345 | |||
346 | /** |
||
347 | * Make request to a URI. |
||
348 | * |
||
349 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
350 | * @param string $method |
||
351 | * @param array $options |
||
352 | * @return $this |
||
353 | */ |
||
354 | 10 | public function request($uri = '', $method = 'GET', array $options = []) |
|
371 | |||
372 | /** |
||
373 | * Make request to a URI, expecting JSON content. |
||
374 | * |
||
375 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
376 | * @param string $method |
||
377 | * @param array $options |
||
378 | * @return $this |
||
379 | */ |
||
380 | 4 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
|
388 | |||
389 | /** |
||
390 | * Add JSON type to the "Accept" header for the request options. |
||
391 | * |
||
392 | * @param array $options |
||
393 | * @return array |
||
394 | */ |
||
395 | 4 | protected function addAcceptableJsonType(array $options) |
|
406 | |||
407 | /** |
||
408 | * Request the URI and return the response content. |
||
409 | * |
||
410 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
411 | * @param string $method |
||
412 | * @param array $options |
||
413 | * @return string |
||
414 | */ |
||
415 | 1 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
|
419 | |||
420 | /** |
||
421 | * Request the URI and return the JSON-decoded response content. |
||
422 | * |
||
423 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
424 | * @param string $method |
||
425 | * @param array $options |
||
426 | * @return mixed |
||
427 | */ |
||
428 | 1 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
|
432 | |||
433 | /** |
||
434 | * Get all allowed magic request methods. |
||
435 | * |
||
436 | * @return array |
||
437 | */ |
||
438 | 9 | protected function getMagicRequestMethods() |
|
444 | |||
445 | /** |
||
446 | * Determine if the given method is a magic request method. |
||
447 | * |
||
448 | * @param string $method |
||
449 | * @param string &$requestMethod |
||
450 | * @param string &$httpMethod |
||
451 | * @return bool |
||
452 | */ |
||
453 | 9 | protected function isMagicRequestMethod($method, &$requestMethod, &$httpMethod) |
|
471 | |||
472 | /** |
||
473 | * Get parameters for $this->request() from the magic request methods. |
||
474 | * |
||
475 | * @param string $httpMethod |
||
476 | * @param array $parameters |
||
477 | * @return array |
||
478 | */ |
||
479 | 2 | protected function getRequestParameters($httpMethod, array $parameters) |
|
489 | |||
490 | /** |
||
491 | * Get all allowed magic response methods. |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | 7 | protected function getMagicResponseMethods() |
|
502 | |||
503 | /** |
||
504 | * Get all allowed magic option methods. |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | 2 | protected function getMagicOptionMethods() |
|
522 | |||
523 | /** |
||
524 | * Get the option key for the given magic option method. |
||
525 | * |
||
526 | * @param string $method |
||
527 | * @return string|null |
||
528 | */ |
||
529 | 2 | protected function getOptionKeyForMethod($method) |
|
535 | |||
536 | /** |
||
537 | * Handle magic method to send request, get response data, or set |
||
538 | * request options. |
||
539 | * |
||
540 | * @param string $method |
||
541 | * @param array $parameters |
||
542 | * @return mixed |
||
543 | * |
||
544 | * @throws \InvalidArgumentException |
||
545 | * @throws \BadMethodCallException |
||
546 | */ |
||
547 | 9 | public function __call($method, $parameters) |
|
569 | } |
||
570 |
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..