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 |
||
| 11 | class HttpClient |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The default request options. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected static $defaultOptions = [ |
||
| 19 | 'connect_timeout' => 5, |
||
| 20 | 'timeout' => 25, |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The Guzzle client. |
||
| 25 | * |
||
| 26 | * @var \GuzzleHttp\Client |
||
| 27 | */ |
||
| 28 | protected $client; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The Guzzle response. |
||
| 32 | * |
||
| 33 | * @var \GuzzleHttp\Psr7\Response |
||
| 34 | */ |
||
| 35 | protected $response; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The request options. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $options = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Indicates whether throws Guzzle exceptions. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $withExceptions = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the default request options. |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 1 | public static function defaultOptions() |
|
| 57 | { |
||
| 58 | 1 | return static::$defaultOptions; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set the default request options. |
||
| 63 | * |
||
| 64 | * @param array $options |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | 1 | public static function setDefaultOptions(array $options) |
|
| 68 | { |
||
| 69 | 1 | static::$defaultOptions = $options; |
|
| 70 | 1 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Create a http client instance. |
||
| 74 | * |
||
| 75 | * @param array|string $config base_uri or any request options |
||
| 76 | */ |
||
| 77 | 23 | public function __construct($config = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Get the Guzzle client instance. |
||
| 90 | * |
||
| 91 | * @return \GuzzleHttp\Client |
||
| 92 | */ |
||
| 93 | 2 | public function getClient() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Trun on/off Guzzle exceptions. |
||
| 100 | * |
||
| 101 | * @param bool $throws |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | 1 | public function withExceptions($throws) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get the request options. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 9 | public function getOptions() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Merge request options. |
||
| 123 | * |
||
| 124 | * @param array $options |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | 4 | public function options(array ...$options) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Remove one or many options using "dot" notation. |
||
| 136 | * |
||
| 137 | * @param string|array|null $key |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | 16 | public function removeOptions($key = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Set a request option using "dot" notation. |
||
| 153 | * |
||
| 154 | * @param string $key |
||
| 155 | * @param mixed $value |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | 7 | public function option($key, $value) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get a request option using "dot" notation. |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 3 | public function getOption($key) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Set the request header. |
||
| 180 | * |
||
| 181 | * @param string $name |
||
| 182 | * @param mixed $value |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 4 | public function header($name, $value) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Set the request content type. |
||
| 192 | * |
||
| 193 | * @param string $type |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | 1 | public function contentType($type) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Set the request accept type. |
||
| 203 | * |
||
| 204 | * @param string $type |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | 2 | public function accept($type) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Set the request accept type to JSON. |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 1 | public function acceptJson() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Specify where the body of a response will be saved. |
||
| 224 | * Set the "sink" option. |
||
| 225 | * |
||
| 226 | * @param mixed $value |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | 1 | public function saveTo($value) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get the Guzzle response instance. |
||
| 236 | * |
||
| 237 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 238 | */ |
||
| 239 | 8 | public function getResponse() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get the status code of response. |
||
| 246 | * |
||
| 247 | * @return int |
||
| 248 | */ |
||
| 249 | public function getStatusCode() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the response header value. |
||
| 258 | * |
||
| 259 | * @param string $name |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | public function getHeader($name) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get all response headers values. |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getHeaders() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get response body. |
||
| 281 | * |
||
| 282 | * @return \GuzzleHttp\Psr7\Stream|null |
||
| 283 | */ |
||
| 284 | 2 | public function getBody() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get response content. |
||
| 293 | * |
||
| 294 | * @return string|null |
||
| 295 | */ |
||
| 296 | 2 | public function getContent() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get JSON decoded response content. |
||
| 305 | * |
||
| 306 | * @param bool $assoc |
||
| 307 | * @return mixed |
||
| 308 | */ |
||
| 309 | 1 | public function getJson($assoc = true) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Make request to a URL. |
||
| 318 | * |
||
| 319 | * @param string $url |
||
| 320 | * @param string $method |
||
| 321 | * @param array $options |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | 9 | public function request($url, $method = 'GET', $options = []) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Make request to a URL, expecting JSON content. |
||
| 341 | * |
||
| 342 | * @param string $url |
||
| 343 | * @param string $method |
||
| 344 | * @param array $options |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | 3 | public function requestJson($url, $method = 'GET', $options = []) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Request the URL and return the response content. |
||
| 356 | * |
||
| 357 | * @param string $url |
||
| 358 | * @param string $method |
||
| 359 | * @param array $options |
||
| 360 | * @return string|null |
||
| 361 | */ |
||
| 362 | 1 | public function fetchContent($url, $method = 'GET', $options = []) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Request the URL and return the JSON decoded response content. |
||
| 369 | * |
||
| 370 | * @param string $url |
||
| 371 | * @param string $method |
||
| 372 | * @param array $options |
||
| 373 | * @param bool $assoc |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | 1 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
|
| 380 | |||
| 381 | 3 | public function __call($method, $args) |
|
| 398 | } |
||
| 399 |