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 HttpClientContract |
||
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 | 4 | private function __construct() |
|
95 | |||
96 | /** |
||
97 | * Destroy the instance. |
||
98 | */ |
||
99 | 2 | public function __destruct() |
|
100 | { |
||
101 | 2 | $this->close(); |
|
102 | 2 | } |
|
103 | |||
104 | /* ------------------------------------------------------------------------------------------------ |
||
105 | | Getters & Setters |
||
106 | | ------------------------------------------------------------------------------------------------ |
||
107 | */ |
||
108 | /** |
||
109 | * Set API Key. |
||
110 | * |
||
111 | * @param string $apiKey |
||
112 | * |
||
113 | * @return self |
||
114 | */ |
||
115 | 306 | 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 | 2 | public function getTimeout() |
|
145 | |||
146 | /** |
||
147 | * Set the timeout. |
||
148 | * |
||
149 | * @param int $seconds |
||
150 | * |
||
151 | * @return self |
||
152 | */ |
||
153 | 612 | public function setTimeout($seconds) |
|
159 | |||
160 | /** |
||
161 | * Get the connect timeout. |
||
162 | * |
||
163 | * @return int |
||
164 | */ |
||
165 | 2 | public function getConnectTimeout() |
|
169 | |||
170 | /** |
||
171 | * Set the connect timeout. |
||
172 | * |
||
173 | * @param int $seconds |
||
174 | * |
||
175 | * @return self |
||
176 | */ |
||
177 | 2 | public function setConnectTimeout($seconds) |
|
183 | |||
184 | /** |
||
185 | * Get array options. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 308 | public function getOptions() |
|
193 | |||
194 | /** |
||
195 | * Set array options. |
||
196 | * |
||
197 | * @param array $options |
||
198 | * |
||
199 | * @return self |
||
200 | */ |
||
201 | 308 | public function setOptionArray(array $options) |
|
207 | |||
208 | /* ------------------------------------------------------------------------------------------------ |
||
209 | | Curl Functions |
||
210 | | ------------------------------------------------------------------------------------------------ |
||
211 | */ |
||
212 | /** |
||
213 | * Init curl. |
||
214 | */ |
||
215 | 306 | private function init() |
|
219 | |||
220 | /** |
||
221 | * Execute curl. |
||
222 | */ |
||
223 | 306 | private function execute() |
|
230 | |||
231 | /** |
||
232 | * Close curl. |
||
233 | */ |
||
234 | 308 | private function close() |
|
239 | |||
240 | /* ------------------------------------------------------------------------------------------------ |
||
241 | | Main Functions |
||
242 | | ------------------------------------------------------------------------------------------------ |
||
243 | */ |
||
244 | /** |
||
245 | * Make the HTTP Client with options. |
||
246 | * |
||
247 | * @param array $options |
||
248 | * |
||
249 | * @return static |
||
250 | */ |
||
251 | 2 | public static function make(array $options = []) |
|
255 | |||
256 | /** |
||
257 | * Get the HTTP. |
||
258 | * |
||
259 | * @return self |
||
260 | */ |
||
261 | 612 | public static function instance() |
|
268 | |||
269 | /** |
||
270 | * Curl the request. |
||
271 | * |
||
272 | * @param string $method |
||
273 | * @param string $url |
||
274 | * @param array|string $params |
||
275 | * @param array $headers |
||
276 | * @param bool $hasFile |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | 306 | public function request($method, $url, $params, $headers, $hasFile) |
|
307 | |||
308 | /** |
||
309 | * Check Cert Errors. |
||
310 | */ |
||
311 | 306 | private function checkCertErrors() |
|
312 | { |
||
313 | 306 | if (SslChecker::hasCertErrors($this->errorCode)) { |
|
314 | $this->headers->set( |
||
315 | 'X-Stripe-Client-Info', |
||
316 | '{"ca":"using Stripe-supplied CA bundle"}' |
||
317 | ); |
||
318 | |||
319 | $this->setOptionArray([ |
||
320 | CURLOPT_HTTPHEADER => $this->headers->get(), |
||
321 | CURLOPT_CAINFO => SslChecker::caBundle() |
||
322 | ]); |
||
323 | |||
324 | $this->execute(); |
||
325 | } |
||
326 | 306 | } |
|
327 | |||
328 | /** |
||
329 | * Encode array to query string |
||
330 | * |
||
331 | * @param array|string $array |
||
332 | * @param string|null $prefix |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | 256 | protected static function encode($array, $prefix = null) |
|
337 | { |
||
338 | // @codeCoverageIgnoreStart |
||
339 | if ( ! is_array($array)) return $array; |
||
340 | // @codeCoverageIgnoreEnd |
||
341 | |||
342 | 256 | $result = []; |
|
343 | |||
344 | 256 | foreach ($array as $key => $value) { |
|
345 | 252 | if (is_null($value)) continue; |
|
346 | |||
347 | if ($prefix) |
||
348 | 252 | $key = ($key !== null && (! is_int($key) || is_array($value))) |
|
349 | 210 | ? "{$prefix}[{$key}]" |
|
350 | 210 | : "{$prefix}[]"; |
|
351 | |||
352 | 252 | if ( ! is_array($value)) { |
|
353 | 252 | $result[] = urlencode($key) . '=' . urlencode($value); |
|
354 | 252 | } |
|
355 | 210 | elseif ($enc = self::encode($value, $key)) { |
|
356 | 210 | $result[] = $enc; |
|
357 | 210 | } |
|
358 | 256 | } |
|
359 | |||
360 | 256 | return implode('&', $result); |
|
361 | } |
||
362 | |||
363 | /* ------------------------------------------------------------------------------------------------ |
||
364 | | Other Functions |
||
365 | | ------------------------------------------------------------------------------------------------ |
||
366 | */ |
||
367 | /** |
||
368 | * Check Response. |
||
369 | * |
||
370 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
371 | */ |
||
372 | 306 | private function checkResponse() |
|
379 | |||
380 | /** |
||
381 | * Handle CURL errors. |
||
382 | * |
||
383 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
384 | */ |
||
385 | private function handleCurlError() |
||
386 | { |
||
387 | switch ($this->errorCode) { |
||
388 | case CURLE_COULDNT_CONNECT: |
||
389 | case CURLE_COULDNT_RESOLVE_HOST: |
||
390 | case CURLE_OPERATION_TIMEOUTED: |
||
391 | $msg = "Could not connect to Stripe ({$this->apiBaseUrl}). Please check your internet connection " |
||
392 | . 'and try again. If this problem persists, you should check Stripe\'s service status at ' |
||
393 | . 'https://twitter.com/stripestatus, or'; |
||
394 | break; |
||
395 | |||
396 | case CURLE_SSL_CACERT: |
||
397 | case CURLE_SSL_PEER_CERTIFICATE: |
||
398 | $msg = 'Could not verify Stripe\'s SSL certificate. Please make sure that your network is not ' |
||
399 | . "intercepting certificates. (Try going to {$this->apiBaseUrl} in your browser.) " |
||
400 | . 'If this problem persists,'; |
||
401 | break; |
||
402 | |||
403 | default: |
||
404 | $msg = 'Unexpected error communicating with Stripe. If this problem persists,'; |
||
405 | // no break |
||
406 | } |
||
407 | |||
408 | $msg .= ' let us know at [email protected].'; |
||
409 | |||
410 | $msg .= "\n\n(Network error [errno {$this->errorCode}]: {$this->errorMessage})"; |
||
411 | |||
412 | throw new ApiConnectionException($msg); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Prepare Response Headers. |
||
417 | * |
||
418 | * @param array $respHeaders |
||
419 | */ |
||
420 | private function prepareResponseHeaders(array &$respHeaders) |
||
433 | } |
||
434 |
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.