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:
| 1 | <?php |
||
| 13 | class MailChimp |
||
| 14 | { |
||
| 15 | private $api_key; |
||
| 16 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
| 17 | |||
| 18 | /* SSL Verification |
||
| 19 | Read before disabling: |
||
| 20 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
||
| 21 | */ |
||
| 22 | public $verify_ssl = true; |
||
| 23 | |||
| 24 | private $last_error = ''; |
||
| 25 | private $last_response = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a new instance |
||
| 29 | * @param string $api_key Your MailChimp API key |
||
| 30 | */ |
||
| 31 | public function __construct($api_key) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
| 43 | * @param string $email The subscriber's email address |
||
| 44 | * @return string Hashed version of the input |
||
| 45 | */ |
||
| 46 | public function subscriberHash($email) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the last error returned by either the network transport, or by the API. |
||
| 53 | * If something didn't work, this should contain the string describing the problem. |
||
| 54 | * @return array|false describing the error |
||
| 55 | */ |
||
| 56 | public function getLastError() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get an array containing the HTTP headers and the body of the API response. |
||
| 64 | * @return array Assoc array with keys 'headers' and 'body' |
||
| 65 | */ |
||
| 66 | public function getLastResponse() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Make an HTTP DELETE request - for deleting data |
||
| 73 | * @param string URL of the API request method |
||
| 74 | * @param array Assoc array of arguments (if any) |
||
| 75 | * @param int Timeout limit for request in seconds |
||
| 76 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 77 | */ |
||
| 78 | public function delete($method, $args=array(), $timeout=10) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Make an HTTP GET request - for retrieving data |
||
| 85 | * @param string URL of the API request method |
||
| 86 | * @param array Assoc array of arguments (usually your data) |
||
| 87 | * @param int Timeout limit for request in seconds |
||
| 88 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 89 | */ |
||
| 90 | public function get($method, $args=array(), $timeout=10) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Make an HTTP PATCH request - for performing partial updates |
||
| 97 | * @param string URL of the API request method |
||
| 98 | * @param array Assoc array of arguments (usually your data) |
||
| 99 | * @param int Timeout limit for request in seconds |
||
| 100 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 101 | */ |
||
| 102 | public function patch($method, $args=array(), $timeout=10) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Make an HTTP POST request - for creating and updating items |
||
| 109 | * @param string URL of the API request method |
||
| 110 | * @param array Assoc array of arguments (usually your data) |
||
| 111 | * @param int Timeout limit for request in seconds |
||
| 112 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 113 | */ |
||
| 114 | public function post($method, $args=array(), $timeout=10) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Make an HTTP PUT request - for creating new items |
||
| 121 | * @param string URL of the API request method |
||
| 122 | * @param array Assoc array of arguments (usually your data) |
||
| 123 | * @param int Timeout limit for request in seconds |
||
| 124 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 125 | */ |
||
| 126 | public function put($method, $args=array(), $timeout=10) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Performs the underlying HTTP request. Not very exciting |
||
| 133 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
| 134 | * @param string $method The API method to be called |
||
| 135 | * @param array $args Assoc array of parameters to be passed |
||
| 136 | * @return array|false Assoc array of decoded result |
||
| 137 | */ |
||
| 138 | private function makeRequest($http_verb, $method, $args=array(), $timeout=10) |
||
| 199 | |||
| 200 | private function formatResponse($response) |
||
| 217 | } |
||
| 218 |
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.