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 |
||
42 | class Client implements LoggerAwareInterface |
||
43 | { |
||
44 | private $apiroot = 'https://connect.liblynx.com'; |
||
45 | |||
46 | /** @var string client ID obtain from LibLynx Connect admin portal */ |
||
47 | private $clientId; |
||
48 | |||
49 | /** @var string client secret obtain from LibLynx Connect admin portal */ |
||
50 | private $clientSecret; |
||
51 | |||
52 | /** @var GuzzleClient HTTP client for API requests */ |
||
53 | private $guzzle; |
||
54 | |||
55 | /** @var \stdClass entry point resource */ |
||
56 | private $entrypoint; |
||
57 | |||
58 | /** @var callable RequestStack handler for API requests */ |
||
59 | private $apiHandler = null; |
||
60 | |||
61 | /** @var callable RequestStack handler for OAuth2 requests */ |
||
62 | private $oauth2Handler = null; |
||
63 | |||
64 | /** @var CacheInterface */ |
||
65 | protected $cache; |
||
66 | |||
67 | /** @var LoggerInterface */ |
||
68 | protected $log; |
||
69 | |||
70 | /** |
||
71 | * Create new LibLynx API client |
||
72 | */ |
||
73 | 16 | public function __construct() |
|
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | 2 | public function setLogger(LoggerInterface $logger) |
|
92 | |||
93 | /** |
||
94 | * Set API root |
||
95 | * An alternative root may be provided to you by LibLynx Technical Support |
||
96 | * @param string $url |
||
97 | */ |
||
98 | 10 | public function setAPIRoot($url) |
|
102 | |||
103 | /** |
||
104 | * Set OAuth2.0 client credentials |
||
105 | * These will be provided to you by LibLynx Technical Support |
||
106 | * |
||
107 | * @param string $clientId |
||
108 | * @param string $clientSecret |
||
109 | */ |
||
110 | 12 | public function setCredentials($clientId, $clientSecret) |
|
115 | |||
116 | /** |
||
117 | * @return array containing client id and secret |
||
118 | */ |
||
119 | 2 | public function getCredentials() |
|
123 | |||
124 | /** |
||
125 | * Attempt an identification using the LibLynx API |
||
126 | * |
||
127 | * @param IdentificationRequest $request |
||
128 | * @return Identification|null |
||
129 | */ |
||
130 | 8 | public function authorize(IdentificationRequest $request) |
|
153 | |||
154 | /** |
||
155 | * General purpose 'GET' request against API |
||
156 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function get($entrypoint) |
||
163 | |||
164 | /** |
||
165 | * General purpose 'POST' request against API |
||
166 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
167 | * @param $json string contains JSON formatted data to post |
||
168 | * @return mixed |
||
169 | */ |
||
170 | 8 | public function post($entrypoint, $json) |
|
174 | |||
175 | /** |
||
176 | * General purpose 'PUT' request against API |
||
177 | * @param $entrypoint string contains either an @entrypoint or full URL obtained from a resource |
||
178 | * @return mixed string contains JSON formatted data to put |
||
179 | */ |
||
180 | public function put($entrypoint, $json) |
||
184 | |||
185 | |||
186 | /** |
||
187 | * This is primarily to facilitate testing - we can add a MockHandler to return |
||
188 | * test responses |
||
189 | * |
||
190 | * @param callable $handler |
||
191 | * @return self |
||
192 | */ |
||
193 | 10 | public function setAPIHandler(callable $handler) |
|
198 | |||
199 | /** |
||
200 | * This is primarily to facilitate testing - we can add a MockHandler to return |
||
201 | * test responses |
||
202 | * |
||
203 | * @param callable $handler |
||
204 | * @return self |
||
205 | */ |
||
206 | 10 | public function setOAuth2Handler(callable $handler) |
|
211 | |||
212 | /** |
||
213 | * @param $method |
||
214 | * @param $entrypoint |
||
215 | * @param null $json |
||
216 | * @return \stdClass object containing JSON decoded response |
||
217 | */ |
||
218 | 8 | protected function makeAPIRequest($method, $entrypoint, $json = null) |
|
262 | |||
263 | 8 | public function resolveEntryPoint($nameOrUrl) |
|
271 | |||
272 | 14 | public function getEntryPoint($name) |
|
314 | |||
315 | /** |
||
316 | * @param $key |
||
317 | * @return bool |
||
318 | * @codeCoverageIgnore |
||
319 | */ |
||
320 | View Code Duplication | protected function cacheHas($key) |
|
329 | |||
330 | /** |
||
331 | * @param $key |
||
332 | * @return mixed |
||
333 | * @codeCoverageIgnore |
||
334 | */ |
||
335 | View Code Duplication | protected function cacheGet($key) |
|
344 | |||
345 | /** |
||
346 | * @param $key |
||
347 | * @param $value |
||
348 | * @param null $ttl |
||
349 | * @return mixed |
||
350 | * @codeCoverageIgnore |
||
351 | */ |
||
352 | View Code Duplication | protected function cacheSet($key, $value, $ttl = null) |
|
361 | |||
362 | 14 | public function getCache() |
|
369 | |||
370 | 12 | public function setCache(CacheInterface $cache) |
|
374 | |||
375 | /** |
||
376 | * Internal helper to provide an OAuth2 capable HTTP client |
||
377 | */ |
||
378 | 12 | protected function getClient() |
|
399 | |||
400 | 10 | protected function createOAuth2Middleware(): OAuth2Middleware |
|
422 | |||
423 | 10 | protected function createCacheMiddleware(): CacheMiddleware |
|
431 | } |
||
432 |
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.