Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CurlClient 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 CurlClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class CurlClient implements ClientInterface |
||
30 | { |
||
31 | private static $instance; |
||
32 | |||
33 | public static function instance() |
||
40 | |||
41 | protected $defaultOptions; |
||
42 | |||
43 | protected $userAgentInfo; |
||
44 | |||
45 | protected $enablePersistentConnections = null; |
||
46 | |||
47 | protected $enableHttp2 = null; |
||
48 | |||
49 | protected $curlHandle = null; |
||
50 | |||
51 | /** |
||
52 | * CurlClient constructor. |
||
53 | * |
||
54 | * Pass in a callable to $defaultOptions that returns an array of CURLOPT_* values to start |
||
55 | * off a request with, or an flat array with the same format used by curl_setopt_array() to |
||
56 | * provide a static set of options. Note that many options are overridden later in the request |
||
57 | * call, including timeouts, which can be set via setTimeout() and setConnectTimeout(). |
||
58 | * |
||
59 | * Note that request() will silently ignore a non-callable, non-array $defaultOptions, and will |
||
60 | * throw an exception if $defaultOptions returns a non-array value. |
||
61 | * |
||
62 | * @param array|callable|null $defaultOptions |
||
63 | */ |
||
64 | public function __construct($defaultOptions = null, $randomGenerator = null) |
||
65 | { |
||
66 | $this->defaultOptions = $defaultOptions; |
||
67 | $this->randomGenerator = $randomGenerator ?: new Util\RandomGenerator(); |
||
68 | $this->initUserAgentInfo(); |
||
69 | |||
70 | // TODO: curl_reset requires PHP >= 5.5.0. Once we drop support for PHP 5.4, we can simply |
||
71 | // initialize this to true. |
||
72 | $this->enablePersistentConnections = function_exists('curl_reset'); |
||
73 | |||
74 | $this->enableHttp2 = $this->canSafelyUseHttp2(); |
||
75 | } |
||
76 | |||
77 | public function __destruct() |
||
81 | |||
82 | public function initUserAgentInfo() |
||
90 | |||
91 | public function getDefaultOptions() |
||
92 | { |
||
93 | return $this->defaultOptions; |
||
94 | } |
||
95 | |||
96 | public function getUserAgentInfo() |
||
100 | |||
101 | /** |
||
102 | * @return boolean |
||
103 | */ |
||
104 | public function getEnablePersistentConnections() |
||
108 | |||
109 | /** |
||
110 | * @param boolean $enable |
||
111 | */ |
||
112 | public function setEnablePersistentConnections($enable) |
||
116 | |||
117 | /** |
||
118 | * @return boolean |
||
119 | */ |
||
120 | public function getEnableHttp2() |
||
124 | |||
125 | /** |
||
126 | * @param boolean $enable |
||
127 | */ |
||
128 | public function setEnableHttp2($enable) |
||
132 | |||
133 | // USER DEFINED TIMEOUTS |
||
134 | |||
135 | const DEFAULT_TIMEOUT = 80; |
||
136 | const DEFAULT_CONNECT_TIMEOUT = 30; |
||
137 | |||
138 | private $timeout = self::DEFAULT_TIMEOUT; |
||
139 | private $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT; |
||
140 | |||
141 | public function setTimeout($seconds) |
||
146 | |||
147 | public function setConnectTimeout($seconds) |
||
152 | |||
153 | public function getTimeout() |
||
157 | |||
158 | public function getConnectTimeout() |
||
162 | |||
163 | // END OF USER DEFINED TIMEOUTS |
||
164 | |||
165 | public function request($method, $absUrl, $headers, $params, $hasFile) |
||
260 | |||
261 | /** |
||
262 | * @param array $opts cURL options |
||
263 | */ |
||
264 | private function executeRequestWithRetries($opts, $absUrl) |
||
265 | { |
||
266 | $numRetries = 0; |
||
267 | |||
268 | while (true) { |
||
269 | $rcode = 0; |
||
270 | $errno = 0; |
||
271 | |||
272 | $this->resetCurlHandle(); |
||
273 | curl_setopt_array($this->curlHandle, $opts); |
||
274 | $rbody = curl_exec($this->curlHandle); |
||
275 | |||
276 | if ($rbody === false) { |
||
277 | $errno = curl_errno($this->curlHandle); |
||
278 | $message = curl_error($this->curlHandle); |
||
279 | } else { |
||
280 | $rcode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); |
||
281 | } |
||
282 | if (!$this->getEnablePersistentConnections()) { |
||
283 | $this->closeCurlHandle(); |
||
284 | } |
||
285 | |||
286 | if ($this->shouldRetry($errno, $rcode, $numRetries)) { |
||
287 | $numRetries += 1; |
||
288 | $sleepSeconds = $this->sleepTime($numRetries); |
||
289 | usleep(intval($sleepSeconds * 1000000)); |
||
290 | } else { |
||
291 | break; |
||
292 | } |
||
293 | } |
||
294 | |||
295 | if ($rbody === false) { |
||
296 | $this->handleCurlError($absUrl, $errno, $message, $numRetries); |
||
297 | } |
||
298 | |||
299 | return [$rbody, $rcode]; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $url |
||
304 | * @param int $errno |
||
305 | * @param string $message |
||
306 | * @param int $numRetries |
||
307 | * @throws Error\ApiConnection |
||
308 | */ |
||
309 | private function handleCurlError($url, $errno, $message, $numRetries) |
||
341 | |||
342 | /** |
||
343 | * Checks if an error is a problem that we should retry on. This includes both |
||
344 | * socket errors that may represent an intermittent problem and some special |
||
345 | * HTTP statuses. |
||
346 | * @param int $errno |
||
347 | * @param int $rcode |
||
348 | * @param int $numRetries |
||
349 | * @return bool |
||
350 | */ |
||
351 | private function shouldRetry($errno, $rcode, $numRetries) |
||
352 | { |
||
353 | if ($numRetries >= Stripe::getMaxNetworkRetries()) { |
||
354 | return false; |
||
355 | } |
||
356 | |||
357 | // Retry on timeout-related problems (either on open or read). |
||
358 | if ($errno === CURLE_OPERATION_TIMEOUTED) { |
||
359 | return true; |
||
360 | } |
||
361 | |||
362 | // Destination refused the connection, the connection was reset, or a |
||
363 | // variety of other connection failures. This could occur from a single |
||
364 | // saturated server, so retry in case it's intermittent. |
||
365 | if ($errno === CURLE_COULDNT_CONNECT) { |
||
366 | return true; |
||
367 | } |
||
368 | |||
369 | // 409 conflict |
||
370 | if ($rcode === 409) { |
||
371 | return true; |
||
372 | } |
||
373 | |||
374 | return false; |
||
375 | } |
||
376 | |||
377 | private function sleepTime($numRetries) |
||
378 | { |
||
379 | // Apply exponential backoff with $initialNetworkRetryDelay on the |
||
380 | // number of $numRetries so far as inputs. Do not allow the number to exceed |
||
381 | // $maxNetworkRetryDelay. |
||
382 | $sleepSeconds = min( |
||
383 | Stripe::getInitialNetworkRetryDelay() * 1.0 * pow(2, $numRetries - 1), |
||
384 | Stripe::getMaxNetworkRetryDelay() |
||
385 | ); |
||
386 | |||
387 | // Apply some jitter by randomizing the value in the range of |
||
388 | // ($sleepSeconds / 2) to ($sleepSeconds). |
||
389 | $sleepSeconds *= 0.5 * (1 + $this->randomGenerator->randFloat()); |
||
390 | |||
391 | // But never sleep less than the base sleep seconds. |
||
392 | $sleepSeconds = max(Stripe::getInitialNetworkRetryDelay(), $sleepSeconds); |
||
393 | |||
394 | return $sleepSeconds; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Initializes the curl handle. If already initialized, the handle is closed first. |
||
399 | */ |
||
400 | private function initCurlHandle() |
||
405 | |||
406 | /** |
||
407 | * Closes the curl handle if initialized. Do nothing if already closed. |
||
408 | */ |
||
409 | private function closeCurlHandle() |
||
416 | |||
417 | /** |
||
418 | * Resets the curl handle. If the handle is not already initialized, or if persistent |
||
419 | * connections are disabled, the handle is reinitialized instead. |
||
420 | */ |
||
421 | private function resetCurlHandle() |
||
429 | |||
430 | /** |
||
431 | * Indicates whether it is safe to use HTTP/2 or not. |
||
432 | * |
||
433 | * @return boolean |
||
434 | */ |
||
435 | private function canSafelyUseHttp2() |
||
442 | |||
443 | /** |
||
444 | * Checks if a list of headers contains a specific header name. |
||
445 | * |
||
446 | * @param string[] $headers |
||
447 | * @param string $name |
||
448 | * @return boolean |
||
449 | */ |
||
450 | private function hasHeader($headers, $name) |
||
460 | } |
||
461 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.