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 |
||
| 22 | class Eventbrite |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The current version of this library. |
||
| 26 | */ |
||
| 27 | const VERSION = '0.1'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The API endpoint to get the current user's details. |
||
| 31 | */ |
||
| 32 | const CURRENT_USER_ENDPOINT = 'users/me/'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string The Eventbrite OAuth token. |
||
| 36 | */ |
||
| 37 | private $token; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \GuzzleHttp\Client |
||
| 41 | */ |
||
| 42 | private $client; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \Psr\Http\Message\ResponseInterface |
||
| 46 | */ |
||
| 47 | private $last_response = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $token |
||
| 51 | * The OAuth token to authenticate the request |
||
| 52 | * @param array $config |
||
| 53 | * An array of Guzzle config options so that everything is configurable. |
||
| 54 | * |
||
| 55 | * @throws \Exception |
||
| 56 | */ |
||
| 57 | public function __construct($token, $config = []) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Make the call to Eventbrite, only synchronous calls at present. |
||
| 80 | * |
||
| 81 | * @param string $verb |
||
| 82 | * @param string $endpoint |
||
| 83 | * @param array $options |
||
| 84 | * |
||
| 85 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 86 | * @throws \Exception |
||
| 87 | */ |
||
| 88 | public function call($verb, $endpoint, $options = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * A slightly abstracted wrapper around call(). |
||
| 120 | * |
||
| 121 | * This essentially splits the call options array into different parameters |
||
| 122 | * to make it more obvious to less advanced users what parameters can be |
||
| 123 | * passed to the client. |
||
| 124 | * |
||
| 125 | * @param $verb |
||
| 126 | * @param $endpoint |
||
| 127 | * @param null $params |
||
| 128 | * @param null $body |
||
| 129 | * @param null $headers |
||
| 130 | * @param array $options |
||
| 131 | * |
||
| 132 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 133 | * @throws \Exception |
||
| 134 | */ |
||
| 135 | public function makeRequest($verb, $endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Checks if the HTTP method being used is correct. |
||
| 160 | * |
||
| 161 | * @param $http_method |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function validMethod($http_method) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Parses the response from |
||
| 176 | * |
||
| 177 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | private function parseResponse(ResponseInterface $response) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Checks a string to see if it's JSON. True if it is, false if it's not. |
||
| 194 | * |
||
| 195 | * @param string $string |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isValidJson($string) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Checks if the class can connect to the Eventbrite API. |
||
| 210 | * |
||
| 211 | * Checks if we can connect to the API by calling the user endpoint and |
||
| 212 | * checking the response code. If the response code is 2xx it returns true, |
||
| 213 | * otherwise false. |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function canConnect() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Wrapper shortcut on the makeRequest method for "GET" requests. |
||
| 228 | * |
||
| 229 | * @param string $endpoint |
||
| 230 | * @param array $options |
||
| 231 | * |
||
| 232 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 233 | * @throws \Exception |
||
| 234 | */ |
||
| 235 | public function get($endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Wrapper shortcut on the makeRequest method for "POST" requests. |
||
| 242 | * |
||
| 243 | * @param $endpoint |
||
| 244 | * @param array $options |
||
| 245 | * |
||
| 246 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 247 | * @throws \Exception |
||
| 248 | */ |
||
| 249 | public function post($endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Wrapper shortcut on the makeRequest method for "PUT" requests. |
||
| 257 | * |
||
| 258 | * @param $endpoint |
||
| 259 | * @param array $options |
||
| 260 | * |
||
| 261 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 262 | * @throws \Exception |
||
| 263 | */ |
||
| 264 | public function put($endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Wrapper shortcut on the makeRequest method for "PATCH" requests. |
||
| 271 | * |
||
| 272 | * @param $endpoint |
||
| 273 | * @param array $options |
||
| 274 | * |
||
| 275 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | public function patch($endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Wrapper shortcut on the makeRequest method for "DELETE" requests. |
||
| 285 | * |
||
| 286 | * @param $endpoint |
||
| 287 | * @param array $options |
||
| 288 | * |
||
| 289 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
| 290 | * @throws \Exception |
||
| 291 | */ |
||
| 292 | public function delete($endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the last response object for inspection. |
||
| 299 | * |
||
| 300 | * @return \Psr\Http\Message\ResponseInterface |
||
| 301 | */ |
||
| 302 | public function getLastResponse() |
||
| 306 | |||
| 307 | } |
||
| 308 |
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.