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 RestClient 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 RestClient, and based on these observations, apply Extract Interface, too.
| 1 | <?PHP |
||
| 29 | class RestClient |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Your API key. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $apiKey; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var HttpClient |
||
| 40 | */ |
||
| 41 | protected $httpClient; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $apiHost; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The version of the API to use. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $apiVersion = 'v2'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If we should use SSL or not. |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | * |
||
| 60 | * @deprecated To be removed in 3.0 |
||
| 61 | */ |
||
| 62 | protected $sslEnabled = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $apiKey |
||
| 66 | * @param string $apiHost |
||
| 67 | * @param HttpClient $httpClient |
||
| 68 | */ |
||
| 69 | 7 | public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $method |
||
| 78 | * @param string $uri |
||
| 79 | * @param mixed $body |
||
| 80 | * @param array $files |
||
| 81 | * @param array $headers |
||
| 82 | * |
||
| 83 | * @throws GenericHTTPError |
||
| 84 | * @throws InvalidCredentials |
||
| 85 | * @throws MissingEndpoint |
||
| 86 | * @throws MissingRequiredParameters |
||
| 87 | * |
||
| 88 | * @return \stdClass |
||
| 89 | */ |
||
| 90 | protected function send($method, $uri, $body = null, $files = [], array $headers = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $url |
||
| 115 | * |
||
| 116 | * @throws GenericHTTPError |
||
| 117 | * @throws InvalidCredentials |
||
| 118 | * @throws MissingEndpoint |
||
| 119 | * @throws MissingRequiredParameters |
||
| 120 | * |
||
| 121 | * @return \stdClass |
||
| 122 | */ |
||
| 123 | 2 | public function getAttachment($url) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $endpointUrl |
||
| 135 | * @param array $postData |
||
| 136 | * @param array $files |
||
| 137 | * |
||
| 138 | * @throws GenericHTTPError |
||
| 139 | * @throws InvalidCredentials |
||
| 140 | * @throws MissingEndpoint |
||
| 141 | * @throws MissingRequiredParameters |
||
| 142 | * |
||
| 143 | * @return \stdClass |
||
| 144 | */ |
||
| 145 | 5 | public function post($endpointUrl, array $postData = [], $files = []) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $endpointUrl |
||
| 184 | * @param array $queryString |
||
| 185 | * |
||
| 186 | * @throws GenericHTTPError |
||
| 187 | * @throws InvalidCredentials |
||
| 188 | * @throws MissingEndpoint |
||
| 189 | * @throws MissingRequiredParameters |
||
| 190 | * |
||
| 191 | * @return \stdClass |
||
| 192 | */ |
||
| 193 | public function get($endpointUrl, $queryString = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $endpointUrl |
||
| 200 | * |
||
| 201 | * @throws GenericHTTPError |
||
| 202 | * @throws InvalidCredentials |
||
| 203 | * @throws MissingEndpoint |
||
| 204 | * @throws MissingRequiredParameters |
||
| 205 | * |
||
| 206 | * @return \stdClass |
||
| 207 | */ |
||
| 208 | public function delete($endpointUrl) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $endpointUrl |
||
| 215 | * @param mixed $putData |
||
| 216 | * |
||
| 217 | * @throws GenericHTTPError |
||
| 218 | * @throws InvalidCredentials |
||
| 219 | * @throws MissingEndpoint |
||
| 220 | * @throws MissingRequiredParameters |
||
| 221 | * |
||
| 222 | * @return \stdClass |
||
| 223 | */ |
||
| 224 | public function put($endpointUrl, $putData) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param ResponseInterface $responseObj |
||
| 231 | * |
||
| 232 | * @throws GenericHTTPError |
||
| 233 | * @throws InvalidCredentials |
||
| 234 | * @throws MissingEndpoint |
||
| 235 | * @throws MissingRequiredParameters |
||
| 236 | * |
||
| 237 | * @return \stdClass |
||
| 238 | */ |
||
| 239 | 2 | public function responseHandler(ResponseInterface $responseObj) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param ResponseInterface $responseObj |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | protected function getResponseExceptionMessage(ResponseInterface $responseObj) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Prepare a file for the postBody. |
||
| 282 | * |
||
| 283 | * @param string $fieldName |
||
| 284 | * @param string|array $filePath |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 3 | protected function prepareFile($fieldName, $filePath) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return HttpClient |
||
| 322 | */ |
||
| 323 | protected function getHttpClient() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $uri |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function getApiUrl($uri) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $apiEndpoint |
||
| 344 | * @param string $apiVersion |
||
| 345 | * @param bool $ssl |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $apiVersion |
||
| 356 | * |
||
| 357 | * @return RestClient |
||
| 358 | */ |
||
| 359 | public function setApiVersion($apiVersion) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param bool $sslEnabled |
||
| 368 | * |
||
| 369 | * @return RestClient |
||
| 370 | * |
||
| 371 | * @deprecated To be removed in 3.0 |
||
| 372 | */ |
||
| 373 | public function setSslEnabled($sslEnabled) |
||
| 379 | } |
||
| 380 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.