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 $config base URI or any request options |
||
| 77 | */ |
||
| 78 | 23 | public function __construct($config = []) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Get the Guzzle client instance. |
||
| 93 | * |
||
| 94 | * @return \GuzzleHttp\Client |
||
| 95 | */ |
||
| 96 | 2 | public function getClient() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Get whether to catch Guzzle exceptions or not. |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function areExceptionsCaught() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set whether to catch Guzzle exceptions or not. |
||
| 113 | * |
||
| 114 | * @param bool $catch |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function catchExceptions($catch) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the request options. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 9 | public function getOptions() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Merge the request options. |
||
| 136 | * |
||
| 137 | * @param array $options |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | 2 | public function options(array $options) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Remove one or many options using "dot" notation. |
||
| 149 | * |
||
| 150 | * @param string|array|null $key |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | 16 | public function removeOptions($key = null) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Set a request option using "dot" notation. |
||
| 166 | * |
||
| 167 | * @param string|array $key |
||
| 168 | * @param mixed $value |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | 8 | public function option($key, $value = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get a request option using "dot" notation. |
||
| 184 | * |
||
| 185 | * @param string $key |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | 3 | public function getOption($key) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Set the request header. |
||
| 195 | * |
||
| 196 | * @param string $name |
||
| 197 | * @param mixed $value |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 4 | public function header($name, $value) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Set the request content type. |
||
| 207 | * |
||
| 208 | * @param string $type |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 1 | public function contentType($type) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Set the request accept type. |
||
| 218 | * |
||
| 219 | * @param string $type |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 2 | public function accept($type) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Set the request accept type to JSON. |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | 1 | public function acceptJson() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Specify where the body of a response will be saved. |
||
| 239 | * Set the "sink" option. |
||
| 240 | * |
||
| 241 | * @param mixed $value |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | 1 | public function saveTo($value) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get the Guzzle response instance. |
||
| 251 | * |
||
| 252 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 253 | */ |
||
| 254 | 8 | public function getResponse() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Get the status code of response. |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function getStatusCode() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the response header value. |
||
| 273 | * |
||
| 274 | * @param string $name |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function getHeader($name) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get all response headers values. |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getHeaders() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get response body. |
||
| 296 | * |
||
| 297 | * @return \GuzzleHttp\Psr7\Stream|null |
||
| 298 | */ |
||
| 299 | 2 | public function getBody() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get response content. |
||
| 308 | * |
||
| 309 | * @return string|null |
||
| 310 | */ |
||
| 311 | 2 | public function getContent() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get JSON decoded response content. |
||
| 320 | * |
||
| 321 | * @param bool $assoc |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | 1 | public function getJson($assoc = true) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Make request to a URI. |
||
| 333 | * |
||
| 334 | * @param string $uri |
||
| 335 | * @param string $method |
||
| 336 | * @param array $options |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | 9 | public function request($uri, $method = 'GET', array $options = []) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Make request to a URI, expecting JSON content. |
||
| 356 | * |
||
| 357 | * @param string $uri |
||
| 358 | * @param string $method |
||
| 359 | * @param array $options |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | 3 | public function requestJson($uri, $method = 'GET', array $options = []) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Request the URI and return the response content. |
||
| 371 | * |
||
| 372 | * @param string $uri |
||
| 373 | * @param string $method |
||
| 374 | * @param array $options |
||
| 375 | * @return string|null |
||
| 376 | */ |
||
| 377 | 1 | public function fetchContent($uri, $method = 'GET', array $options = []) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Request the URI and return the JSON decoded response content. |
||
| 384 | * |
||
| 385 | * @param string $uri |
||
| 386 | * @param string $method |
||
| 387 | * @param array $options |
||
| 388 | * @param bool $assoc |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | 1 | public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Dynamically methods to set request option, send request, or get |
||
| 398 | * response properties. |
||
| 399 | * |
||
| 400 | * @param string $method |
||
| 401 | * @param array $args |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | 4 | public function __call($method, $args) |
|
| 421 | } |
||
| 422 |