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' => 30, |
||
| 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 using "dot" notation. |
||
| 131 | * |
||
| 132 | * @param string|null $key |
||
| 133 | * @return mixed |
||
| 134 | 3 | */ |
|
| 135 | public function getOption($key = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the request options using "dot" notation. |
||
| 142 | * |
||
| 143 | * @param string|array $key |
||
| 144 | * @param mixed $value |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function option($key, $value = null) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Merge the given options to the request options. |
||
| 160 | * |
||
| 161 | * @param array $options,... |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function mergeOption(array ...$options) |
||
| 165 | { |
||
| 166 | $this->options = array_replace_recursive($this->options, ...$options); |
||
| 167 | |||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | 1 | /** |
|
| 172 | * Remove one or many request options using "dot" notation. |
||
| 173 | 1 | * |
|
| 174 | * @param array|string $keys |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function removeOption($keys) |
||
| 178 | { |
||
| 179 | Arr::forget($this->options, is_array($keys) ? $keys : func_get_args()); |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | 1 | ||
| 184 | /** |
||
| 185 | 1 | * Set a request header. |
|
| 186 | * |
||
| 187 | 1 | * @param string $name |
|
| 188 | 1 | * @param mixed $value |
|
| 189 | 1 | * @return $this |
|
| 190 | */ |
||
| 191 | 1 | public function header($name, $value) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Set the request content type. |
||
| 198 | * |
||
| 199 | * @param string $type |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function contentType($type) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the request accept type. |
||
| 209 | * |
||
| 210 | * @param string $type |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function accept($type) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set the request accept type to "application/json". |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function acceptJson() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Specify where the body of the response will be saved. |
||
| 230 | * Set the "sink" option. |
||
| 231 | * |
||
| 232 | * @param mixed $dest |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | public function saveTo($dest) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the Guzzle response instance. |
||
| 242 | * |
||
| 243 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 244 | */ |
||
| 245 | public function getResponse() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get data from the response. |
||
| 252 | * |
||
| 253 | * @param string|\Closure $callback |
||
| 254 | * @param array $parameters |
||
| 255 | * @param mixed $default |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | protected function getResponseData($callback, array $parameters = [], $default = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the response content. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getContent() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the JSON-decoded response content. |
||
| 281 | * |
||
| 282 | * @param bool $assoc |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | public function json($assoc = true) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Make request to a URI. |
||
| 292 | * |
||
| 293 | * @param string $uri |
||
| 294 | * @param string $method |
||
| 295 | * @param array $options |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function request($uri = '', $method = 'GET', array $options = []) |
||
| 314 | |||
| 315 | 2 | /** |
|
| 316 | 2 | * Make request to a URI, expecting JSON content. |
|
| 317 | 2 | * |
|
| 318 | * @param string $uri |
||
| 319 | * @param string $method |
||
| 320 | * @param array $options |
||
| 321 | * @return $this |
||
| 322 | 2 | */ |
|
| 323 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Add JSON type to the "Accept" header for the request options. |
||
| 334 | * |
||
| 335 | * @param array $options |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | protected function addAcceptableJsonType(array $options) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Request the URI and return the response content. |
||
| 352 | * |
||
| 353 | * @param string $uri |
||
| 354 | * @param string $method |
||
| 355 | * @param array $options |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Request the URI and return the JSON-decoded response content. |
||
| 365 | * |
||
| 366 | * @param string $uri |
||
| 367 | * @param string $method |
||
| 368 | * @param array $options |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the dynamic request methods. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function getDynamicRequestMethods() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the dynamic requestJson methods. |
||
| 390 | * |
||
| 391 | 2 | * @return array |
|
| 392 | */ |
||
| 393 | protected function getDynamicRequestJsonMethods() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the dynamic response methods. |
||
| 402 | * |
||
| 403 | 1 | * @return array |
|
| 404 | */ |
||
| 405 | protected function getDynamicResponseMethods() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Insert HTTP method to the parameters. |
||
| 415 | 1 | * |
|
| 416 | * @param array $parameters |
||
| 417 | * @return array |
||
| 418 | 1 | */ |
|
| 419 | 1 | protected function insertHttpMethodToParameters($method, array $parameters) |
|
| 431 | 1 | ||
| 432 | /** |
||
| 433 | 1 | * Dynamically send request, get response data, or set request option. |
|
| 434 | * |
||
| 435 | * @param string $method |
||
| 436 | 1 | * @param array $parameters |
|
| 437 | * @return mixed |
||
| 438 | */ |
||
| 439 | 1 | public function __call($method, $parameters) |
|
| 459 | } |
||
| 460 |
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..