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 |
||
| 12 | class HttpClient |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The default request options. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected static $defaultOptions = [ |
||
| 20 | 'connect_timeout' => 5, |
||
| 21 | 'timeout' => 25, |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The Guzzle client. |
||
| 26 | * |
||
| 27 | * @var \GuzzleHttp\Client |
||
| 28 | */ |
||
| 29 | protected $client; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The Guzzle response. |
||
| 33 | * |
||
| 34 | * @var \GuzzleHttp\Psr7\Response |
||
| 35 | */ |
||
| 36 | protected $response; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The request options. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $options = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Indicate whether to catch Guzzle exceptions. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $catchExceptions = true; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the default request options. |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | 24 | public static function defaultOptions() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Set the default request options. |
||
| 64 | * |
||
| 65 | * @param array $options |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | 1 | public static function setDefaultOptions(array $options) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Create a http client instance. |
||
| 75 | * |
||
| 76 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
| 77 | * |
||
| 78 | * @throws \InvalidArgumentException |
||
| 79 | */ |
||
| 80 | 23 | public function __construct($options = []) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Get the Guzzle client instance. |
||
| 95 | * |
||
| 96 | * @return \GuzzleHttp\Client |
||
| 97 | */ |
||
| 98 | 2 | public function getClient() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Get whether to catch Guzzle exceptions or not. |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function areExceptionsCaught() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set whether to catch Guzzle exceptions or not. |
||
| 115 | * |
||
| 116 | * @param bool $catch |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function catchExceptions($catch) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the request options. |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | 9 | public function getOptions() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Set the request options. |
||
| 138 | * |
||
| 139 | * @param array $options |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function setOptions(array $options) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Merge the given options to the request options. |
||
| 151 | * |
||
| 152 | * @param array $options |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function mergeOptions(array $options) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Remove options using "dot" notation. |
||
| 162 | * |
||
| 163 | * @param string|array|null $key |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | 16 | public function removeOptions($key = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Get a request option using "dot" notation. |
||
| 179 | * |
||
| 180 | * @param string $key |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | 3 | public function getOption($key) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Set a request option using "dot" notation. |
||
| 190 | * |
||
| 191 | * @param string|array $key |
||
| 192 | * @param mixed $value |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | 10 | public function option($key, $value = null) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Set the request header. |
||
| 208 | * |
||
| 209 | * @param string $name |
||
| 210 | * @param mixed $value |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 4 | public function header($name, $value) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set the request content type. |
||
| 220 | * |
||
| 221 | * @param string $type |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | 1 | public function contentType($type) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Set the request accept type. |
||
| 231 | * |
||
| 232 | * @param string $type |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 2 | public function accept($type) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Set the request accept type to JSON. |
||
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | 1 | public function acceptJson() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Specify where the body of a response will be saved. |
||
| 252 | * Set the "sink" option. |
||
| 253 | * |
||
| 254 | * @param mixed $dest |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 1 | public function saveTo($dest) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get the Guzzle response instance. |
||
| 264 | * |
||
| 265 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 266 | */ |
||
| 267 | 8 | public function getResponse() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get the status code of response. |
||
| 274 | * |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | public function getStatusCode() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the response header value. |
||
| 286 | * |
||
| 287 | * @param string $name |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | public function getHeader($name) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get all response headers values. |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function getHeaders() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get response body. |
||
| 309 | * |
||
| 310 | * @return \GuzzleHttp\Psr7\Stream|null |
||
| 311 | */ |
||
| 312 | 2 | public function getBody() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get response content. |
||
| 321 | * |
||
| 322 | * @return string|null |
||
| 323 | */ |
||
| 324 | 2 | public function getContent() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get JSON decoded response content. |
||
| 333 | * |
||
| 334 | * @param bool $assoc |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | 1 | public function getJson($assoc = true) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Make request to a URI. |
||
| 346 | * |
||
| 347 | * @param string $uri |
||
| 348 | * @param string $method |
||
| 349 | * @param array $options |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | 9 | public function request($uri, $method = 'GET', array $options = []) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Make request to a URI, expecting JSON content. |
||
| 369 | * |
||
| 370 | * @param string $uri |
||
| 371 | * @param string $method |
||
| 372 | * @param array $options |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | 3 | public function requestJson($uri, $method = 'GET', array $options = []) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Request the URI and return the response content. |
||
| 384 | * |
||
| 385 | * @param string $uri |
||
| 386 | * @param string $method |
||
| 387 | * @param array $options |
||
| 388 | * @return string|null |
||
| 389 | */ |
||
| 390 | 1 | public function fetchContent($uri, $method = 'GET', array $options = []) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Request the URI and return the JSON decoded response content. |
||
| 397 | * |
||
| 398 | * @param string $uri |
||
| 399 | * @param string $method |
||
| 400 | * @param array $options |
||
| 401 | * @param bool $assoc |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | 1 | public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Dynamically methods to set request option, send request, or get |
||
| 411 | * response properties. |
||
| 412 | * |
||
| 413 | * @param string $method |
||
| 414 | * @param array $args |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | 6 | public function __call($method, $args) |
|
| 434 | } |
||
| 435 |