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 namespace Arcanedev\Stripe\Http\Curl; |
||
| 12 | class HttpClient implements HttpClientInterface |
||
| 13 | { |
||
| 14 | /* ------------------------------------------------------------------------------------------------ |
||
| 15 | | Constants |
||
| 16 | | ------------------------------------------------------------------------------------------------ |
||
| 17 | */ |
||
| 18 | const DEFAULT_TIMEOUT = 80; |
||
| 19 | const DEFAULT_CONNECT_TIMEOUT = 30; |
||
| 20 | |||
| 21 | /* ------------------------------------------------------------------------------------------------ |
||
| 22 | | Properties |
||
| 23 | | ------------------------------------------------------------------------------------------------ |
||
| 24 | */ |
||
| 25 | /** |
||
| 26 | * The HTTP Client instance. |
||
| 27 | * |
||
| 28 | * @var \Arcanedev\Stripe\Http\Curl\HttpClient |
||
| 29 | */ |
||
| 30 | private static $instance; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $apiKey; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $apiBaseUrl; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Arcanedev\Stripe\Http\Curl\HeaderBag |
||
| 44 | */ |
||
| 45 | private $headers; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Arcanedev\Stripe\Http\Curl\CurlOptions |
||
| 49 | */ |
||
| 50 | private $options; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var resource |
||
| 54 | */ |
||
| 55 | private $curl; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $timeout = self::DEFAULT_TIMEOUT; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var mixed |
||
| 69 | */ |
||
| 70 | private $response; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | private $errorCode; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $errorMessage; |
||
| 81 | |||
| 82 | /* ------------------------------------------------------------------------------------------------ |
||
| 83 | | Constructor & Destructor |
||
| 84 | | ------------------------------------------------------------------------------------------------ |
||
| 85 | */ |
||
| 86 | /** |
||
| 87 | * Create a HttpClient instance. |
||
| 88 | */ |
||
| 89 | 5 | private function __construct() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Destroy the instance. |
||
| 98 | */ |
||
| 99 | public function __destruct() |
||
| 103 | |||
| 104 | /* ------------------------------------------------------------------------------------------------ |
||
| 105 | | Getters & Setters |
||
| 106 | | ------------------------------------------------------------------------------------------------ |
||
| 107 | */ |
||
| 108 | /** |
||
| 109 | * Set API Key. |
||
| 110 | * |
||
| 111 | * @param string $apiKey |
||
| 112 | * |
||
| 113 | * @return self |
||
| 114 | */ |
||
| 115 | 744 | public function setApiKey($apiKey) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Set Base URL. |
||
| 124 | * |
||
| 125 | * @param string $apiBaseUrl |
||
| 126 | * |
||
| 127 | * @return self |
||
| 128 | */ |
||
| 129 | public function setApiBaseUrl($apiBaseUrl) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the timeout. |
||
| 138 | * |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | 5 | public function getTimeout() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set the timeout. |
||
| 148 | * |
||
| 149 | * @param int $seconds |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | 1434 | public function setTimeout($seconds) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get the connect timeout. |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | 5 | public function getConnectTimeout() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set the connect timeout. |
||
| 172 | * |
||
| 173 | * @param int $seconds |
||
| 174 | * |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | 5 | public function setConnectTimeout($seconds) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get array options. |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getOptions() |
||
| 193 | 744 | ||
| 194 | /** |
||
| 195 | 744 | * Set array options. |
|
| 196 | * |
||
| 197 | * @param array $options |
||
| 198 | * |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | public function setOptionArray(array $options) |
||
| 207 | 744 | ||
| 208 | 744 | /* ------------------------------------------------------------------------------------------------ |
|
| 209 | | Curl Functions |
||
| 210 | | ------------------------------------------------------------------------------------------------ |
||
| 211 | */ |
||
| 212 | /** |
||
| 213 | 744 | * Init curl. |
|
| 214 | */ |
||
| 215 | 744 | private function init() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Execute curl. |
||
| 222 | */ |
||
| 223 | 744 | private function execute() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Close curl. |
||
| 233 | */ |
||
| 234 | private function close() |
||
| 240 | |||
| 241 | 1434 | /* ------------------------------------------------------------------------------------------------ |
|
| 242 | 5 | | Main Functions |
|
| 243 | 4 | | ------------------------------------------------------------------------------------------------ |
|
| 244 | */ |
||
| 245 | 1434 | /** |
|
| 246 | * Make the HTTP Client with options. |
||
| 247 | * |
||
| 248 | * @param array $options |
||
| 249 | * |
||
| 250 | * @return static |
||
| 251 | */ |
||
| 252 | public static function make(array $options = []) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the HTTP. |
||
| 259 | 744 | * |
|
| 260 | * @return self |
||
| 261 | 744 | */ |
|
| 262 | 520 | public static function instance() |
|
| 270 | 744 | ||
| 271 | 744 | /** |
|
| 272 | 744 | * Curl the request. |
|
| 273 | 595 | * |
|
| 274 | * @param string $method |
||
| 275 | 744 | * @param string $url |
|
| 276 | 744 | * @param array|string $params |
|
| 277 | * @param array $headers |
||
| 278 | 744 | * @param bool $hasFile |
|
| 279 | 744 | * |
|
| 280 | 744 | * @return array |
|
| 281 | */ |
||
| 282 | 744 | public function request($method, $url, $params, $headers, $hasFile) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Check Cert Errors. |
||
| 314 | */ |
||
| 315 | private function checkCertErrors() |
||
| 331 | 510 | ||
| 332 | 510 | /** |
|
| 333 | 510 | * Encode array to query string |
|
| 334 | 408 | * |
|
| 335 | * @param array|string $array |
||
| 336 | 610 | * @param string|null $prefix |
|
| 337 | 610 | * |
|
| 338 | 488 | * @return string |
|
| 339 | 510 | */ |
|
| 340 | 530 | protected static function encode($array, $prefix = null) |
|
| 367 | |||
| 368 | /* ------------------------------------------------------------------------------------------------ |
||
| 369 | | Other Functions |
||
| 370 | | ------------------------------------------------------------------------------------------------ |
||
| 371 | */ |
||
| 372 | /** |
||
| 373 | * Check Response. |
||
| 374 | * |
||
| 375 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 376 | */ |
||
| 377 | private function checkResponse() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Handle CURL errors. |
||
| 387 | * |
||
| 388 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 389 | */ |
||
| 390 | private function handleCurlError() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Prepare Response Headers. |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | private function prepareResponseHeaders(array &$respHeaders) |
||
| 438 | } |
||
| 439 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.