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 | ]; |
||
79 | |||
80 | /** |
||
81 | * The Guzzle client. |
||
82 | * |
||
83 | * @var \GuzzleHttp\Client |
||
84 | */ |
||
85 | protected $client; |
||
86 | |||
87 | /** |
||
88 | * The request options. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $options = []; |
||
93 | |||
94 | /** |
||
95 | * The Guzzle response. |
||
96 | * |
||
97 | * @var \GuzzleHttp\Psr7\Response |
||
98 | */ |
||
99 | protected $response; |
||
100 | |||
101 | /** |
||
102 | * Indicate whether to catch Guzzle exceptions. |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | protected $catchExceptions = true; |
||
107 | |||
108 | /** |
||
109 | * Get the default request options. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 26 | public static function defaultOptions() |
|
114 | { |
||
115 | 26 | return static::$defaultOptions; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Set the default request options. |
||
120 | * |
||
121 | * @param array $options |
||
122 | * @return void |
||
123 | */ |
||
124 | 27 | public static function setDefaultOptions(array $options) |
|
125 | { |
||
126 | 27 | static::$defaultOptions = $options; |
|
127 | 27 | } |
|
128 | |||
129 | /** |
||
130 | * Create a http client instance. |
||
131 | * |
||
132 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
133 | * |
||
134 | * @throws \InvalidArgumentException |
||
135 | */ |
||
136 | 26 | public function __construct($options = []) |
|
137 | { |
||
138 | 26 | if (is_string($options) || $options instanceof UriInterface) { |
|
139 | 1 | $options = ['base_uri' => $options]; |
|
140 | 25 | } elseif (! is_array($options)) { |
|
141 | 1 | throw new InvalidArgumentException('Options must be a string, UriInterface, or an array'); |
|
142 | } |
||
143 | |||
144 | 25 | $this->client = new Client( |
|
145 | 25 | array_replace_recursive(static::defaultOptions(), $options) |
|
146 | ); |
||
147 | |||
148 | 25 | $this->options = $this->client->getConfig(); |
|
|
|||
149 | 25 | } |
|
150 | |||
151 | /** |
||
152 | * Get the Guzzle client instance. |
||
153 | * |
||
154 | * @return \GuzzleHttp\Client |
||
155 | */ |
||
156 | 4 | public function getClient() |
|
157 | { |
||
158 | 4 | return $this->client; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get whether to catch Guzzle exceptions or not. |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | 1 | public function areExceptionsCaught() |
|
167 | { |
||
168 | 1 | return $this->catchExceptions; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Set whether to catch Guzzle exceptions or not. |
||
173 | * |
||
174 | * @param bool $catch |
||
175 | * @return $this |
||
176 | */ |
||
177 | 2 | public function catchExceptions($catch) |
|
178 | { |
||
179 | 2 | $this->catchExceptions = (bool) $catch; |
|
180 | |||
181 | 2 | return $this; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get the request options using "dot" notation. |
||
186 | * |
||
187 | * @param string|null $key |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 11 | public function getOption($key = null) |
|
194 | |||
195 | /** |
||
196 | * Set the request options using "dot" notation. |
||
197 | * |
||
198 | * @param string|array $key |
||
199 | * @param mixed $value |
||
200 | * @return $this |
||
201 | */ |
||
202 | 8 | public function option($key, $value = null) |
|
212 | |||
213 | /** |
||
214 | * Merge the given options to the request options. |
||
215 | * |
||
216 | * @param array ...$options |
||
217 | * @return $this |
||
218 | */ |
||
219 | 1 | public function mergeOptions(array ...$options) |
|
225 | |||
226 | /** |
||
227 | * Remove one or many request options using "dot" notation. |
||
228 | * |
||
229 | * @param array|string $keys |
||
230 | * @return $this |
||
231 | */ |
||
232 | 2 | public function removeOption($keys) |
|
238 | |||
239 | /** |
||
240 | * Set a request header. |
||
241 | * |
||
242 | * @param string $name |
||
243 | * @param mixed $value |
||
244 | * @return $this |
||
245 | */ |
||
246 | 5 | public function header($name, $value) |
|
250 | |||
251 | /** |
||
252 | * Set the request content type. |
||
253 | * |
||
254 | * @param string $type |
||
255 | * @return $this |
||
256 | */ |
||
257 | 1 | public function contentType($type) |
|
261 | |||
262 | /** |
||
263 | * Set the request accept type. |
||
264 | * |
||
265 | * @param string $type |
||
266 | * @return $this |
||
267 | */ |
||
268 | 3 | public function accept($type) |
|
272 | |||
273 | /** |
||
274 | * Set the request accept type to "application/json". |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | 1 | public function acceptJson() |
|
282 | |||
283 | /** |
||
284 | * Specify where the body of the response will be saved. |
||
285 | * Set the "sink" option. |
||
286 | * |
||
287 | * @param string|resource|\Psr\Http\Message\StreamInterface $dest |
||
288 | * @return $this |
||
289 | */ |
||
290 | 1 | public function saveTo($dest) |
|
294 | |||
295 | /** |
||
296 | * Get the Guzzle response instance. |
||
297 | * |
||
298 | * @return \GuzzleHttp\Psr7\Response|null |
||
299 | */ |
||
300 | 3 | public function getResponse() |
|
304 | |||
305 | /** |
||
306 | * Get data from the response. |
||
307 | * |
||
308 | * @param string|\Closure $callback |
||
309 | * @param array $parameters |
||
310 | * @param mixed $default |
||
311 | * @return mixed |
||
312 | */ |
||
313 | 5 | protected function getResponseData($callback, array $parameters = [], $default = null) |
|
323 | |||
324 | /** |
||
325 | * Get the response content. |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | 4 | public function getContent() |
|
333 | |||
334 | /** |
||
335 | * Get the JSON-decoded response content. |
||
336 | * |
||
337 | * @param bool $assoc |
||
338 | * @return mixed |
||
339 | */ |
||
340 | 2 | public function getJsonContent($assoc = true) |
|
344 | |||
345 | /** |
||
346 | * Make request to a URI. |
||
347 | * |
||
348 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
349 | * @param string $method |
||
350 | * @param array $options |
||
351 | * @return $this |
||
352 | */ |
||
353 | 10 | public function request($uri = '', $method = 'GET', array $options = []) |
|
370 | |||
371 | /** |
||
372 | * Make request to a URI, expecting JSON content. |
||
373 | * |
||
374 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
375 | * @param string $method |
||
376 | * @param array $options |
||
377 | * @return $this |
||
378 | */ |
||
379 | 4 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
|
387 | |||
388 | /** |
||
389 | * Add JSON type to the "Accept" header for the request options. |
||
390 | * |
||
391 | * @param array $options |
||
392 | * @return array |
||
393 | */ |
||
394 | 4 | protected function addAcceptableJsonType(array $options) |
|
405 | |||
406 | /** |
||
407 | * Request the URI and return the response content. |
||
408 | * |
||
409 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
410 | * @param string $method |
||
411 | * @param array $options |
||
412 | * @return string |
||
413 | */ |
||
414 | 1 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
|
418 | |||
419 | /** |
||
420 | * Request the URI and return the JSON-decoded response content. |
||
421 | * |
||
422 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
423 | * @param string $method |
||
424 | * @param array $options |
||
425 | * @return mixed |
||
426 | */ |
||
427 | 1 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
|
431 | |||
432 | /** |
||
433 | * Get all allowed magic request methods. |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | 9 | protected function getMagicRequestMethods() |
|
443 | |||
444 | /** |
||
445 | * Determine if the given method is a magic request method. |
||
446 | * |
||
447 | * @param string $method |
||
448 | * @param string &$requestMethod |
||
449 | * @param string &$httpMethod |
||
450 | * @return bool |
||
451 | */ |
||
452 | 9 | protected function isMagicRequestMethod($method, &$requestMethod, &$httpMethod) |
|
470 | |||
471 | /** |
||
472 | * Get parameters for $this->request() from the magic request methods. |
||
473 | * |
||
474 | * @param string $httpMethod |
||
475 | * @param array $parameters |
||
476 | * @return array |
||
477 | */ |
||
478 | 2 | protected function getRequestParameters($httpMethod, array $parameters) |
|
488 | |||
489 | /** |
||
490 | * Get all allowed magic response methods. |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | 7 | protected function getMagicResponseMethods() |
|
501 | |||
502 | /** |
||
503 | * Get all allowed magic option methods. |
||
504 | * |
||
505 | * @return array |
||
506 | */ |
||
507 | 2 | protected function getMagicOptionMethods() |
|
521 | |||
522 | /** |
||
523 | * Get the option key for the given magic option method. |
||
524 | * |
||
525 | * @param string $method |
||
526 | * @return string|null |
||
527 | */ |
||
528 | 2 | protected function getOptionKeyForMethod($method) |
|
534 | |||
535 | /** |
||
536 | * Handle magic method to send request, get response data, or set |
||
537 | * request options. |
||
538 | * |
||
539 | * @param string $method |
||
540 | * @param array $parameters |
||
541 | * @return mixed |
||
542 | * |
||
543 | * @throws \InvalidArgumentException |
||
544 | * @throws \BadMethodCallException |
||
545 | */ |
||
546 | 9 | public function __call($method, $parameters) |
|
568 | } |
||
569 |
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..