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( |
||
163 | |||
164 | /** |
||
165 | * Get access token and refresh token |
||
166 | * |
||
167 | * @return array two string values, access token and refresh token |
||
168 | */ |
||
169 | public function getTokens() : array |
||
173 | |||
174 | /** |
||
175 | * Search the API resource using the specified $filters |
||
176 | * |
||
177 | * @param string $resource |
||
178 | * @param array $filters |
||
179 | * |
||
180 | * @return string opaque handle to be given to endIndex() |
||
181 | */ |
||
182 | public function startIndex(string $resource, array $filters = []) : string |
||
187 | |||
188 | /** |
||
189 | * @see startIndex() |
||
190 | */ |
||
191 | public function index(string $resource, array $filters = []) : Response |
||
195 | |||
196 | /** |
||
197 | * Get the details of an API resource based on $id |
||
198 | * |
||
199 | * @param string $resource |
||
200 | * @param string $id |
||
201 | * @param array $parameters |
||
202 | * |
||
203 | * @return string opaque handle to be given to endGet() |
||
204 | */ |
||
205 | public function startGet(string $resource, string $id, array $parameters = []) : string |
||
214 | |||
215 | /** |
||
216 | * @see startGet() |
||
217 | */ |
||
218 | public function get(string $resource, string $id, array $parameters = []) : Response |
||
222 | |||
223 | /** |
||
224 | * Create a new instance of an API resource using the provided $data |
||
225 | * |
||
226 | * @param string $resource |
||
227 | * @param array $data |
||
228 | * |
||
229 | * @return string opaque handle to be given to endPost() |
||
230 | */ |
||
231 | public function startPost(string $resource, array $data) : string |
||
236 | |||
237 | /** |
||
238 | * @see startPost() |
||
239 | */ |
||
240 | public function post(string $resource, array $data) : Response |
||
244 | |||
245 | /** |
||
246 | * Update an existing instance of an API resource specified by $id with the provided $data |
||
247 | * |
||
248 | * @param string $resource |
||
249 | * @param string $id |
||
250 | * @param array $data |
||
251 | * |
||
252 | * @return string opaque handle to be given to endPut() |
||
253 | */ |
||
254 | public function startPut(string $resource, string $id, array $data) : string |
||
259 | |||
260 | /** |
||
261 | * @see startPut() |
||
262 | */ |
||
263 | public function put(string $resource, string $id, array $data) : Response |
||
267 | |||
268 | /** |
||
269 | * Delete an existing instance of an API resource specified by $id |
||
270 | * |
||
271 | * @param string $resource |
||
272 | * @param string $id |
||
273 | * @param array $data |
||
274 | * |
||
275 | * @return string opaque handle to be given to endDelete() |
||
276 | */ |
||
277 | public function startDelete(string $resource, string $id = null, array $data = null) : string |
||
287 | |||
288 | /** |
||
289 | * @see startDelete() |
||
290 | */ |
||
291 | public function delete(string $resource, string $id = null, array $data = null) : Response |
||
295 | |||
296 | /** |
||
297 | * Get response of start*() method |
||
298 | * |
||
299 | * @param string $handle opaque handle from start*() |
||
300 | * |
||
301 | * @return Response |
||
302 | */ |
||
303 | public function end(string $handle) : Response |
||
337 | |||
338 | /** |
||
339 | * Set the default headers |
||
340 | * |
||
341 | * @param array The default headers |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | public function setDefaultHeaders(array $defaultHeaders) |
||
349 | |||
350 | private static function isExpiredToken(Response $response) : bool |
||
377 | |||
378 | /** |
||
379 | * Obtains a new access token from the API |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | private function refreshAccessToken() |
||
395 | |||
396 | /** |
||
397 | * Helper method to set this clients access token from cache |
||
398 | * |
||
399 | * @return void |
||
400 | */ |
||
401 | private function setTokenFromCache() |
||
416 | |||
417 | /** |
||
418 | * Calls adapter->start() using caches |
||
419 | * |
||
420 | * @param string $url |
||
421 | * @param string $method |
||
422 | * @param string|null $body |
||
423 | * @param array $headers Authorization key will be overwritten with the bearer token, and Accept-Encoding wil be |
||
424 | * overwritten with gzip. |
||
425 | * |
||
426 | * @return string opaque handle to be given to end() |
||
427 | */ |
||
428 | private function start(string $url, string $method, string $body = null, array $headers = []) |
||
458 | |||
459 | private function getCacheKey(Request $request) : string |
||
463 | } |
||
464 |
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..