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 |
||
| 63 | class HttpClient |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * The default request options. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected static $defaultOptions = [ |
||
| 71 | 'connect_timeout' => 5, |
||
| 72 | 'timeout' => 20, |
||
| 73 | 'http_errors' => false, |
||
| 74 | ]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Guzzle client. |
||
| 78 | * |
||
| 79 | * @var \GuzzleHttp\Client |
||
| 80 | */ |
||
| 81 | protected $client; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The request options. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $options = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The Guzzle response. |
||
| 92 | * |
||
| 93 | * @var \GuzzleHttp\Psr7\Response |
||
| 94 | */ |
||
| 95 | protected $response; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Indicate whether to catch Guzzle exceptions. |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $catchExceptions = true; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the default request options. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | 27 | public static function defaultOptions() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Set the default request options. |
||
| 116 | * |
||
| 117 | * @param array $options |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | 28 | public static function setDefaultOptions(array $options) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Create a new http client instance. |
||
| 127 | * |
||
| 128 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
| 129 | * @return static |
||
| 130 | */ |
||
| 131 | 1 | public static function new($options = []) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Create a new http client instance. |
||
| 138 | * |
||
| 139 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
| 140 | * |
||
| 141 | * @throws \InvalidArgumentException |
||
| 142 | */ |
||
| 143 | 27 | public function __construct($options = []) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get the Guzzle client instance. |
||
| 160 | * |
||
| 161 | * @return \GuzzleHttp\Client |
||
| 162 | */ |
||
| 163 | 4 | public function getClient() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get whether to catch Guzzle exceptions or not. |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | 1 | public function areExceptionsCaught() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Set whether to catch Guzzle exceptions or not. |
||
| 180 | * |
||
| 181 | * @param bool $catch |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | 2 | public function catchExceptions($catch) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Get the request options using "dot" notation. |
||
| 193 | * |
||
| 194 | * @param string|null $key |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | 12 | public function getOption($key = null) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Set the request options using "dot" notation. |
||
| 204 | * |
||
| 205 | * @param string|array $key |
||
| 206 | * @param mixed $value |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | 9 | public function option($key, $value = null) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Merge the given options to the request options. |
||
| 222 | * |
||
| 223 | * @param array ...$options |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 1 | public function mergeOptions(array ...$options) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Remove one or many request options using "dot" notation. |
||
| 235 | * |
||
| 236 | * @param array|string $keys |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | 2 | public function removeOption($keys) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Set a request header. |
||
| 248 | * |
||
| 249 | * @param string $name |
||
| 250 | * @param mixed $value |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 6 | public function header($name, $value) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set the request accept type. |
||
| 260 | * |
||
| 261 | * @param string $type |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | 3 | public function accept($type) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set the request accept type to "application/json". |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | 1 | public function acceptJson() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Set user agent for the request. |
||
| 281 | * |
||
| 282 | * @param string $value |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 1 | public function userAgent($value) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Set the request content type. |
||
| 292 | * |
||
| 293 | * @param string $type |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | 1 | public function contentType($type) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Specify where the body of the response will be saved. |
||
| 303 | * Set the "sink" option. |
||
| 304 | * |
||
| 305 | * @param string|resource|\Psr\Http\Message\StreamInterface $dest |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | 1 | public function saveTo($dest) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Get the Guzzle response instance. |
||
| 315 | * |
||
| 316 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 317 | */ |
||
| 318 | 3 | public function getResponse() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Get data from the response. |
||
| 325 | * |
||
| 326 | * @param string|\Closure $callback |
||
| 327 | * @param mixed $parameters |
||
| 328 | * @param mixed $default |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | 6 | public function getResponseData($callback, $parameters = [], $default = null) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the response content. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 4 | public function getContent() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get the JSON-decoded response content. |
||
| 354 | * |
||
| 355 | * @param bool $assoc |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | 2 | public function getJson($assoc = true) |
|
| 359 | { |
||
| 360 | 2 | return json_decode($this->getContent(), $assoc); |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Make request to a URI. |
||
| 365 | * |
||
| 366 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 367 | * @param string $method |
||
| 368 | * @param array $options |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | 10 | public function request($uri = '', $method = 'GET', array $options = []) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Make request to a URI, expecting JSON content. |
||
| 391 | * |
||
| 392 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 393 | * @param string $method |
||
| 394 | * @param array $options |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | 2 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Add JSON type to the "Accept" header for the request options. |
||
| 408 | * |
||
| 409 | * @param array $options |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 2 | protected function addAcceptableJsonType(array $options) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Request the URI and return the response content. |
||
| 426 | * |
||
| 427 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 428 | * @param string $method |
||
| 429 | * @param array $options |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 1 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Request the URI and return the JSON-decoded response content. |
||
| 439 | * |
||
| 440 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 441 | * @param string $method |
||
| 442 | * @param array $options |
||
| 443 | * @return mixed |
||
| 444 | */ |
||
| 445 | 1 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Get all allowed magic request methods. |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | 8 | protected function getMagicRequestMethods() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get parameters for $this->request() from the magic request call. |
||
| 464 | * |
||
| 465 | * @param string $method |
||
| 466 | * @param array $parameters |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | 1 | protected function getRequestParameters($method, array $parameters) |
|
| 470 | { |
||
| 471 | 1 | if (empty($parameters)) { |
|
| 472 | $parameters = ['', $method]; |
||
| 473 | } else { |
||
| 474 | 1 | array_splice($parameters, 1, 0, $method); |
|
| 475 | } |
||
| 476 | |||
| 477 | 1 | return $parameters; |
|
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get all allowed magic response methods. |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | 7 | protected function getMagicResponseMethods() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Get all allowed magic option methods. |
||
| 495 | * |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | 2 | protected function getMagicOptionMethods() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Get the option name for the given magic option method. |
||
| 515 | * |
||
| 516 | * @param string $method |
||
| 517 | * @return string|null |
||
| 518 | */ |
||
| 519 | 2 | protected function getOptionNameForMethod($method) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Handle magic method to send request, get response data, or set |
||
| 528 | * request options. |
||
| 529 | * |
||
| 530 | * @param string $method |
||
| 531 | * @param array $parameters |
||
| 532 | * @return mixed |
||
| 533 | * |
||
| 534 | * @throws \InvalidArgumentException |
||
| 535 | * @throws \BadMethodCallException |
||
| 536 | */ |
||
| 537 | 8 | public function __call($method, $parameters) |
|
| 557 | } |
||
| 558 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: