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 Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | final class Client implements ClientInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Flag to cache no requests |
||
| 18 | * |
||
| 19 | * @const int |
||
| 20 | */ |
||
| 21 | const CACHE_MODE_NONE = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Flag to cache only GET requests |
||
| 25 | * |
||
| 26 | * @const int |
||
| 27 | */ |
||
| 28 | const CACHE_MODE_GET = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Flag to cache only TOKEN requests |
||
| 32 | * |
||
| 33 | * @const int |
||
| 34 | */ |
||
| 35 | const CACHE_MODE_TOKEN = 2; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Flag to cache ALL requests |
||
| 39 | * |
||
| 40 | * @const int |
||
| 41 | */ |
||
| 42 | const CACHE_MODE_ALL = 3; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Flag to refresh cache on ALL requests |
||
| 46 | * |
||
| 47 | * @const int |
||
| 48 | */ |
||
| 49 | const CACHE_MODE_REFRESH = 4; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | const CACHE_MODES = [ |
||
| 55 | self::CACHE_MODE_NONE, |
||
| 56 | self::CACHE_MODE_GET, |
||
| 57 | self::CACHE_MODE_TOKEN, |
||
| 58 | self::CACHE_MODE_ALL, |
||
| 59 | self::CACHE_MODE_REFRESH, |
||
| 60 | ]; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Base url of the API server |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $baseUrl; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * HTTP Adapter for sending request to the api |
||
| 71 | * |
||
| 72 | * @var Adapter |
||
| 73 | */ |
||
| 74 | private $adapter; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Oauth authentication implementation |
||
| 78 | * |
||
| 79 | * @var Authentication |
||
| 80 | */ |
||
| 81 | private $authentication; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * API access token |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $accessToken; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * API refresh token |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $refreshToken; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Storage for cached API responses |
||
| 99 | * |
||
| 100 | * @var Cache |
||
| 101 | */ |
||
| 102 | private $cache; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Strategy for caching |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | private $cacheMode; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Handles set in start() |
||
| 113 | * |
||
| 114 | * @var array like [opaqueKey => [cached response (Response), adapter handle (opaque), Request]] |
||
| 115 | */ |
||
| 116 | private $handles = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Array of headers that are passed on every request unless they are overridden |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | private $defaultHeaders = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Create a new instance of Client |
||
| 127 | * |
||
| 128 | * @param Adapter $adapter |
||
| 129 | * @param Authentication $authentication |
||
| 130 | * @param string $baseUrl |
||
| 131 | * @param int $cacheMode |
||
| 132 | * @param Cache $cache |
||
| 133 | * @param string $accessToken |
||
| 134 | * @param string $refreshToken |
||
| 135 | * |
||
| 136 | * @throws \InvalidArgumentException Thrown if $baseUrl is not a non-empty string |
||
| 137 | * @throws \InvalidArgumentException Thrown if $cacheMode is not one of the cache mode constants |
||
| 138 | */ |
||
| 139 | public function __construct( |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get access token and refresh token |
||
| 168 | * |
||
| 169 | * @return array two string values, access token and refresh token |
||
| 170 | */ |
||
| 171 | public function getTokens() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Search the API resource using the specified $filters |
||
| 178 | * |
||
| 179 | * @param string $resource |
||
| 180 | * @param array $filters |
||
| 181 | * |
||
| 182 | * @return mixed opaque handle to be given to endIndex() |
||
| 183 | */ |
||
| 184 | public function startIndex($resource, array $filters = []) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @see startIndex() |
||
| 193 | */ |
||
| 194 | public function index($resource, array $filters = []) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the details of an API resource based on $id |
||
| 201 | * |
||
| 202 | * @param string $resource |
||
| 203 | * @param string $id |
||
| 204 | * @param array $parameters |
||
| 205 | * |
||
| 206 | * @return mixed opaque handle to be given to endGet() |
||
| 207 | */ |
||
| 208 | public function startGet($resource, $id, array $parameters = []) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @see startGet() |
||
| 221 | */ |
||
| 222 | public function get($resource, $id, array $parameters = []) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Create a new instance of an API resource using the provided $data |
||
| 229 | * |
||
| 230 | * @param string $resource |
||
| 231 | * @param array $data |
||
| 232 | * |
||
| 233 | * @return mixed opaque handle to be given to endPost() |
||
| 234 | */ |
||
| 235 | public function startPost($resource, array $data) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @see startPost() |
||
| 244 | */ |
||
| 245 | public function post($resource, array $data) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Update an existing instance of an API resource specified by $id with the provided $data |
||
| 252 | * |
||
| 253 | * @param string $resource |
||
| 254 | * @param string $id |
||
| 255 | * @param array $data |
||
| 256 | * |
||
| 257 | * @return mixed opaque handle to be given to endPut() |
||
| 258 | */ |
||
| 259 | public function startPut($resource, $id, array $data) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @see startPut() |
||
| 268 | */ |
||
| 269 | public function put($resource, $id, array $data) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Delete an existing instance of an API resource specified by $id |
||
| 276 | * |
||
| 277 | * @param string $resource |
||
| 278 | * @param string $id |
||
| 279 | * @param array $data |
||
| 280 | * |
||
| 281 | * @return mixed opaque handle to be given to endDelete() |
||
| 282 | */ |
||
| 283 | public function startDelete($resource, $id = null, array $data = null) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @see startDelete() |
||
| 298 | */ |
||
| 299 | public function delete($resource, $id = null, array $data = null) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get response of start*() method |
||
| 306 | * |
||
| 307 | * @param mixed $handle opaque handle from start*() |
||
| 308 | * |
||
| 309 | * @return Response |
||
| 310 | */ |
||
| 311 | public function end($handle) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set the default headers |
||
| 348 | * |
||
| 349 | * @param array The default headers |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | public function setDefaultHeaders($defaultHeaders) |
||
| 357 | |||
| 358 | private static function isExpiredToken(Response $response) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Obtains a new access token from the API |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | private function refreshAccessToken() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Helper method to set this clients access token from cache |
||
| 406 | * |
||
| 407 | * @return void |
||
| 408 | */ |
||
| 409 | private function setTokenFromCache() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Calls adapter->start() using caches |
||
| 427 | * |
||
| 428 | * @param string $url |
||
| 429 | * @param string $method |
||
| 430 | * @param string|null $body |
||
| 431 | * @param array $headers Authorization key will be overwritten with the bearer token, and Accept-Encoding wil be |
||
| 432 | * overwritten with gzip. |
||
| 433 | * |
||
| 434 | * @return mixed opaque handle to be given to end() |
||
| 435 | */ |
||
| 436 | private function start($url, $method, $body = null, array $headers = []) |
||
| 466 | |||
| 467 | private function getCacheKey(Request $request) : string |
||
| 471 | } |
||
| 472 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..