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 | 28 | public static function defaultOptions() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Set the default request options. |
||
| 116 | * |
||
| 117 | * @param array $options |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | 29 | 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 | * |
||
| 130 | * @throws \InvalidArgumentException |
||
| 131 | */ |
||
| 132 | 28 | public function __construct($options = []) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get the Guzzle client instance. |
||
| 149 | * |
||
| 150 | * @return \GuzzleHttp\Client |
||
| 151 | */ |
||
| 152 | 4 | public function getClient() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Get whether to catch Guzzle exceptions or not. |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 1 | public function areExceptionsCaught() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set whether to catch Guzzle exceptions or not. |
||
| 169 | * |
||
| 170 | * @param bool $catch |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 2 | public function catchExceptions($catch) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Get the request options using "dot" notation. |
||
| 182 | * |
||
| 183 | * @param string|null $key |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | 12 | public function getOption($key = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Set the request options using "dot" notation. |
||
| 193 | * |
||
| 194 | * @param string|array $key |
||
| 195 | * @param mixed $value |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | 9 | public function option($key, $value = null) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Merge the given options to the request options. |
||
| 211 | * |
||
| 212 | * @param array ...$options |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | 1 | public function mergeOptions(array ...$options) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Remove one or many request options using "dot" notation. |
||
| 224 | * |
||
| 225 | * @param array|string $keys |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | 2 | public function removeOption($keys) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Set a request header. |
||
| 237 | * |
||
| 238 | * @param string $name |
||
| 239 | * @param mixed $value |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | 6 | public function header($name, $value) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set the request accept type. |
||
| 249 | * |
||
| 250 | * @param string $type |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 3 | public function accept($type) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set the request accept type to "application/json". |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | 1 | public function acceptJson() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Set user agent for the request. |
||
| 270 | * |
||
| 271 | * @param string $value |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | 1 | public function userAgent($value) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Set the request content type. |
||
| 281 | * |
||
| 282 | * @param string $type |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 1 | public function contentType($type) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Specify where the body of the response will be saved. |
||
| 292 | * Set the "sink" option. |
||
| 293 | * |
||
| 294 | * @param string|resource|\Psr\Http\Message\StreamInterface $dest |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | 1 | public function saveTo($dest) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get the Guzzle response instance. |
||
| 304 | * |
||
| 305 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 306 | */ |
||
| 307 | 3 | public function getResponse() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get data from the response. |
||
| 314 | * |
||
| 315 | * @param string|\Closure $callback |
||
| 316 | * @param mixed $parameters |
||
| 317 | * @param mixed $default |
||
| 318 | * @return mixed |
||
| 319 | */ |
||
| 320 | 6 | public function getResponseData($callback, $parameters = [], $default = null) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get the response content. |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | 4 | public function getContent() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Get the JSON-decoded response content. |
||
| 343 | * |
||
| 344 | * @param bool $assoc |
||
| 345 | * @return mixed |
||
| 346 | */ |
||
| 347 | 2 | public function getJson($assoc = true) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Make request to a URI. |
||
| 354 | * |
||
| 355 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 356 | * @param string $method |
||
| 357 | * @param array $options |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | 11 | public function request($uri = '', $method = 'GET', array $options = []) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Make request to a URI, expecting JSON content. |
||
| 380 | * |
||
| 381 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 382 | * @param string $method |
||
| 383 | * @param array $options |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | 2 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Add JSON type to the "Accept" header for the request options. |
||
| 397 | * |
||
| 398 | * @param array $options |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | 2 | protected function addAcceptableJsonType(array $options) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Request the URI and return the response content. |
||
| 415 | * |
||
| 416 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 417 | * @param string $method |
||
| 418 | * @param array $options |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | 1 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Request the URI and return the JSON-decoded response content. |
||
| 428 | * |
||
| 429 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 430 | * @param string $method |
||
| 431 | * @param array $options |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | 1 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Get all allowed magic request methods. |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | 9 | protected function getMagicRequestMethods() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Get parameters for $this->request() from the magic request call. |
||
| 453 | * |
||
| 454 | * @param string $method |
||
| 455 | * @param array $parameters |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | 2 | protected function getRequestParameters($method, array $parameters) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Get all allowed magic response methods. |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | 7 | protected function getMagicResponseMethods() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get all allowed magic option methods. |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | 2 | protected function getMagicOptionMethods() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Get the option name for the given magic option method. |
||
| 504 | * |
||
| 505 | * @param string $method |
||
| 506 | * @return string|null |
||
| 507 | */ |
||
| 508 | 2 | protected function getOptionNameForMethod($method) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Handle magic method to send request, get response data, or set |
||
| 517 | * request options. |
||
| 518 | * |
||
| 519 | * @param string $method |
||
| 520 | * @param array $parameters |
||
| 521 | * @return mixed |
||
| 522 | * |
||
| 523 | * @throws \InvalidArgumentException |
||
| 524 | * @throws \BadMethodCallException |
||
| 525 | */ |
||
| 526 | 9 | public function __call($method, $parameters) |
|
| 546 | } |
||
| 547 |
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: