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 MailChimp 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 MailChimp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class MailChimp |
||
14 | { |
||
15 | private $api_key; |
||
16 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
17 | |||
18 | private $timeout = 10; |
||
19 | |||
20 | /* SSL Verification |
||
21 | Read before disabling: |
||
22 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
||
23 | */ |
||
24 | public $verify_ssl = true; |
||
25 | |||
26 | private $request_successful = false; |
||
27 | private $last_error = ''; |
||
28 | private $last_response = array(); |
||
29 | private $last_request = array(); |
||
30 | |||
31 | /** |
||
32 | * Create a new instance |
||
33 | * |
||
34 | * @param string $api_key Your MailChimp API key |
||
35 | * @param string $api_endpoint Optional custom API endpoint |
||
36 | * |
||
37 | * @throws \Exception |
||
38 | */ |
||
39 | public function __construct($api_key, $api_endpoint = null, $parameters = array()) |
||
40 | { |
||
41 | if (!function_exists('curl_init') || !function_exists('curl_setopt')) { |
||
42 | throw new \Exception("cURL support is required, but can't be found."); |
||
43 | } |
||
44 | |||
45 | $this->api_key = $api_key; |
||
46 | |||
47 | if ($api_endpoint === null) { |
||
48 | if (strpos($this->api_key, '-') === false) { |
||
49 | throw new \Exception("Invalid MailChimp API key supplied."); |
||
50 | } |
||
51 | list(, $data_center) = explode('-', $this->api_key); |
||
52 | $this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint); |
||
53 | } else { |
||
54 | $this->api_endpoint = $api_endpoint; |
||
55 | } |
||
56 | |||
57 | if (isset($parameters['timeout'])) { |
||
58 | $this->timeout = $parameters['timeout']; |
||
59 | } |
||
60 | |||
61 | $this->last_response = array('headers' => null, 'body' => null); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create a new instance of a Batch request. Optionally with the ID of an existing batch. |
||
66 | * |
||
67 | * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example. |
||
68 | * |
||
69 | * @return Batch New Batch object. |
||
70 | */ |
||
71 | public function new_batch($batch_id = null) |
||
75 | |||
76 | /** |
||
77 | * @return string The url to the API endpoint |
||
78 | */ |
||
79 | public function getApiEndpoint() |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
87 | * |
||
88 | * @param string $email The subscriber's email address |
||
89 | * |
||
90 | * @return string Hashed version of the input |
||
91 | */ |
||
92 | public function subscriberHash($email) |
||
96 | |||
97 | /** |
||
98 | * Was the last request successful? |
||
99 | * |
||
100 | * @return bool True for success, false for failure |
||
101 | */ |
||
102 | public function success() |
||
106 | |||
107 | /** |
||
108 | * Get the last error returned by either the network transport, or by the API. |
||
109 | * If something didn't work, this should contain the string describing the problem. |
||
110 | * |
||
111 | * @return string|false describing the error |
||
112 | */ |
||
113 | public function getLastError() |
||
117 | |||
118 | /** |
||
119 | * Get an array containing the HTTP headers and the body of the API response. |
||
120 | * |
||
121 | * @return array Assoc array with keys 'headers' and 'body' |
||
122 | */ |
||
123 | public function getLastResponse() |
||
127 | |||
128 | /** |
||
129 | * Get an array containing the HTTP headers and the body of the API request. |
||
130 | * |
||
131 | * @return array Assoc array |
||
132 | */ |
||
133 | public function getLastRequest() |
||
137 | |||
138 | /** |
||
139 | * Make an HTTP DELETE request - for deleting data |
||
140 | * |
||
141 | * @param string $method URL of the API request method |
||
142 | * @param array $args Assoc array of arguments (if any) |
||
143 | * @param int $timeout Timeout limit for request in seconds |
||
144 | * |
||
145 | * @return array|false Assoc array of API response, decoded from JSON |
||
146 | */ |
||
147 | View Code Duplication | public function delete($method, $args = array(), $timeout = 0) |
|
155 | |||
156 | /** |
||
157 | * Make an HTTP GET request - for retrieving data |
||
158 | * |
||
159 | * @param string $method URL of the API request method |
||
160 | * @param array $args Assoc array of arguments (usually your data) |
||
161 | * @param int $timeout Timeout limit for request in seconds |
||
162 | * |
||
163 | * @return array|false Assoc array of API response, decoded from JSON |
||
164 | */ |
||
165 | View Code Duplication | public function get($method, $args = array(), $timeout = 0) |
|
173 | |||
174 | /** |
||
175 | * Make an HTTP PATCH request - for performing partial updates |
||
176 | * |
||
177 | * @param string $method URL of the API request method |
||
178 | * @param array $args Assoc array of arguments (usually your data) |
||
179 | * @param int $timeout Timeout limit for request in seconds |
||
180 | * |
||
181 | * @return array|false Assoc array of API response, decoded from JSON |
||
182 | */ |
||
183 | View Code Duplication | public function patch($method, $args = array(), $timeout = 0) |
|
191 | |||
192 | /** |
||
193 | * Make an HTTP POST request - for creating and updating items |
||
194 | * |
||
195 | * @param string $method URL of the API request method |
||
196 | * @param array $args Assoc array of arguments (usually your data) |
||
197 | * @param int $timeout Timeout limit for request in seconds |
||
198 | * |
||
199 | * @return array|false Assoc array of API response, decoded from JSON |
||
200 | */ |
||
201 | View Code Duplication | public function post($method, $args = array(), $timeout = 0) |
|
209 | |||
210 | /** |
||
211 | * Make an HTTP PUT request - for creating new items |
||
212 | * |
||
213 | * @param string $method URL of the API request method |
||
214 | * @param array $args Assoc array of arguments (usually your data) |
||
215 | * @param int $timeout Timeout limit for request in seconds |
||
216 | * |
||
217 | * @return array|false Assoc array of API response, decoded from JSON |
||
218 | */ |
||
219 | View Code Duplication | public function put($method, $args = array(), $timeout = 0) |
|
226 | |||
227 | /** |
||
228 | * Performs the underlying HTTP request. Not very exciting. |
||
229 | * |
||
230 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
231 | * @param string $method The API method to be called |
||
232 | * @param array $args Assoc array of parameters to be passed |
||
233 | * @param int $timeout |
||
234 | * |
||
235 | * @return array|false Assoc array of decoded result |
||
236 | */ |
||
237 | private function makeRequest($http_verb, $method, $args = array(), $timeout = 0) |
||
310 | |||
311 | /** |
||
312 | * @param string $http_verb |
||
313 | * @param string $method |
||
314 | * @param string $url |
||
315 | * @param integer $timeout |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | private function prepareStateForRequest($http_verb, $method, $url, $timeout) |
||
341 | |||
342 | /** |
||
343 | * Get the HTTP headers as an array of header-name => header-value pairs. |
||
344 | * |
||
345 | * The "Link" header is parsed into an associative array based on the |
||
346 | * rel names it contains. The original value is available under |
||
347 | * the "_raw" key. |
||
348 | * |
||
349 | * @param string $headersAsString |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | private function getHeadersAsArray($headersAsString) |
||
381 | |||
382 | /** |
||
383 | * Extract all rel => URL pairs from the provided Link header value |
||
384 | * |
||
385 | * Mailchimp only implements the URI reference and relation type from |
||
386 | * RFC 5988, so the value of the header is something like this: |
||
387 | * |
||
388 | * 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy", |
||
389 | * <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"' |
||
390 | * |
||
391 | * @param string $linkHeaderAsString |
||
392 | * |
||
393 | * @return array |
||
394 | */ |
||
395 | private function getLinkHeaderAsArray($linkHeaderAsString) |
||
407 | |||
408 | /** |
||
409 | * Encode the data and attach it to the request |
||
410 | * |
||
411 | * @param resource $ch cURL session handle, used by reference |
||
412 | * @param array $data Assoc array of data to attach |
||
413 | */ |
||
414 | private function attachRequestPayload(&$ch, $data) |
||
420 | |||
421 | /** |
||
422 | * Decode the response and format any error messages for debugging |
||
423 | * |
||
424 | * @param array $response The response from the curl request |
||
425 | * |
||
426 | * @return array|false The JSON decoded into an array |
||
427 | */ |
||
428 | private function formatResponse($response) |
||
438 | |||
439 | /** |
||
440 | * Do post-request formatting and setting state from the response |
||
441 | * |
||
442 | * @param array $response The response from the curl request |
||
443 | * @param string $responseContent The body of the response from the curl request |
||
444 | * @param resource $ch The curl resource |
||
445 | * |
||
446 | * @return array The modified response |
||
447 | */ |
||
448 | private function setResponseState($response, $responseContent, $ch) |
||
466 | |||
467 | /** |
||
468 | * Check if the response was successful or a failure. If it failed, store the error. |
||
469 | * |
||
470 | * @param array $response The response from the curl request |
||
471 | * @param array|false $formattedResponse The response body payload from the curl request |
||
472 | * @param int $timeout The timeout supplied to the curl request. |
||
473 | * |
||
474 | * @return bool If the request was successful |
||
475 | */ |
||
476 | private function determineSuccess($response, $formattedResponse, $timeout) |
||
498 | |||
499 | /** |
||
500 | * Find the HTTP status code from the headers or API response body |
||
501 | * |
||
502 | * @param array $response The response from the curl request |
||
503 | * @param array|false $formattedResponse The response body payload from the curl request |
||
504 | * |
||
505 | * @return int HTTP status code |
||
506 | */ |
||
507 | private function findHTTPStatus($response, $formattedResponse) |
||
519 | } |
||
520 |
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.