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 |
||
19 | class MailChimp |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var ClientInterface |
||
24 | */ |
||
25 | private $client; |
||
26 | |||
27 | private $api_key; |
||
28 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
29 | |||
30 | const TIMEOUT = 10; |
||
31 | |||
32 | private $request_successful = false; |
||
33 | private $last_error = ''; |
||
34 | private $last_response = array(); |
||
35 | private $last_request = array(); |
||
36 | |||
37 | /** |
||
38 | * Create a new instance |
||
39 | * |
||
40 | * @param string $api_key Your MailChimp API key |
||
41 | * @param string $api_endpoint Optional custom API endpoint |
||
42 | * |
||
43 | * @throws \Exception |
||
44 | */ |
||
45 | public function __construct(ClientInterface $client, $api_key, $api_endpoint = null) |
||
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 | public function delete($method, $args = array(), $timeout = self::TIMEOUT) |
||
151 | |||
152 | /** |
||
153 | * Make an HTTP GET request - for retrieving data |
||
154 | * |
||
155 | * @param string $method URL of the API request method |
||
156 | * @param array $args Assoc array of arguments (usually your data) |
||
157 | * @param int $timeout Timeout limit for request in seconds |
||
158 | * |
||
159 | * @return array|false Assoc array of API response, decoded from JSON |
||
160 | */ |
||
161 | public function get($method, $args = array(), $timeout = self::TIMEOUT) |
||
165 | |||
166 | /** |
||
167 | * Make an HTTP PATCH request - for performing partial updates |
||
168 | * |
||
169 | * @param string $method URL of the API request method |
||
170 | * @param array $args Assoc array of arguments (usually your data) |
||
171 | * @param int $timeout Timeout limit for request in seconds |
||
172 | * |
||
173 | * @return array|false Assoc array of API response, decoded from JSON |
||
174 | */ |
||
175 | public function patch($method, $args = array(), $timeout = self::TIMEOUT) |
||
179 | |||
180 | /** |
||
181 | * Make an HTTP POST request - for creating and updating items |
||
182 | * |
||
183 | * @param string $method URL of the API request method |
||
184 | * @param array $args Assoc array of arguments (usually your data) |
||
185 | * @param int $timeout Timeout limit for request in seconds |
||
186 | * |
||
187 | * @return array|false Assoc array of API response, decoded from JSON |
||
188 | */ |
||
189 | public function post($method, $args = array(), $timeout = self::TIMEOUT) |
||
193 | |||
194 | /** |
||
195 | * Make an HTTP PUT request - for creating new items |
||
196 | * |
||
197 | * @param string $method URL of the API request method |
||
198 | * @param array $args Assoc array of arguments (usually your data) |
||
199 | * @param int $timeout Timeout limit for request in seconds |
||
200 | * |
||
201 | * @return array|false Assoc array of API response, decoded from JSON |
||
202 | */ |
||
203 | public function put($method, $args = array(), $timeout = self::TIMEOUT) |
||
207 | |||
208 | /** |
||
209 | * Performs the underlying HTTP request. Not very exciting. |
||
210 | * |
||
211 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
212 | * @param string $method The API method to be called |
||
213 | * @param array $args Assoc array of parameters to be passed |
||
214 | * @param int $timeout |
||
215 | * |
||
216 | * @return array|false Assoc array of decoded result |
||
217 | */ |
||
218 | private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT) |
||
291 | |||
292 | /** |
||
293 | * @param RequestInterface $request |
||
294 | * @return string |
||
295 | */ |
||
296 | View Code Duplication | private function getRequestHeader(RequestInterface $request): string |
|
313 | |||
314 | /** |
||
315 | * @param ResponseInterface $response |
||
316 | * @return string |
||
317 | */ |
||
318 | View Code Duplication | private function getResponseHeader(ResponseInterface $response): string |
|
335 | |||
336 | /** |
||
337 | * @param string $http_verb |
||
338 | * @param string $method |
||
339 | * @param string $url |
||
340 | * @param integer $timeout |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | private function prepareStateForRequest($http_verb, $method, $url, $timeout) |
||
366 | |||
367 | /** |
||
368 | * Get the HTTP headers as an array of header-name => header-value pairs. |
||
369 | * |
||
370 | * The "Link" header is parsed into an associative array based on the |
||
371 | * rel names it contains. The original value is available under |
||
372 | * the "_raw" key. |
||
373 | * |
||
374 | * @param string $headersAsString |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | private function getHeadersAsArray($headersAsString) |
||
406 | |||
407 | /** |
||
408 | * Extract all rel => URL pairs from the provided Link header value |
||
409 | * |
||
410 | * Mailchimp only implements the URI reference and relation type from |
||
411 | * RFC 5988, so the value of the header is something like this: |
||
412 | * |
||
413 | * 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy", |
||
414 | * <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"' |
||
415 | * |
||
416 | * @param string $linkHeaderAsString |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | private function getLinkHeaderAsArray($linkHeaderAsString) |
||
432 | |||
433 | /** |
||
434 | * Decode the response and format any error messages for debugging |
||
435 | * |
||
436 | * @param array $response The response from the curl request |
||
437 | * |
||
438 | * @return array|false The JSON decoded into an array |
||
439 | */ |
||
440 | private function formatResponse($response) |
||
450 | |||
451 | /** |
||
452 | * Do post-request formatting and setting state from the response |
||
453 | * |
||
454 | * @param array $response The response from the curl request |
||
455 | * @param string $responseContent The body of the response from the curl request |
||
456 | * @param string $errorMessage The error message |
||
457 | * @return array The modified response |
||
458 | */ |
||
459 | private function setResponseState($response, $responseContent, $errorMessage) |
||
476 | |||
477 | /** |
||
478 | * Check if the response was successful or a failure. If it failed, store the error. |
||
479 | * |
||
480 | * @param array $response The response from the curl request |
||
481 | * @param array|false $formattedResponse The response body payload from the curl request |
||
482 | * @param int $timeout The timeout supplied to the curl request. |
||
483 | * |
||
484 | * @return bool If the request was successful |
||
485 | */ |
||
486 | private function determineSuccess($response, $formattedResponse, $timeout) |
||
508 | |||
509 | /** |
||
510 | * Find the HTTP status code from the headers or API response body |
||
511 | * |
||
512 | * @param array $response The response from the curl request |
||
513 | * @param array|false $formattedResponse The response body payload from the curl request |
||
514 | * |
||
515 | * @return int HTTP status code |
||
516 | */ |
||
517 | private function findHTTPStatus($response, $formattedResponse) |
||
529 | } |
||
530 |