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 |
||
37 | class Client |
||
38 | { |
||
39 | public $baseUrl; |
||
40 | |||
41 | /** @var string Alma zone (institution or network) */ |
||
42 | public $zone; |
||
43 | |||
44 | /** @var string Alma Developers Network API key for this zone */ |
||
45 | public $key; |
||
46 | |||
47 | /** @var Client Network zone instance */ |
||
48 | public $nz; |
||
49 | |||
50 | /** @var HttpClient */ |
||
51 | protected $http; |
||
52 | |||
53 | /** @var MessageFactory */ |
||
54 | protected $messageFactory; |
||
55 | |||
56 | /** @var UriFactory */ |
||
57 | protected $uriFactory; |
||
58 | |||
59 | /** @var SruClient */ |
||
60 | public $sru; |
||
61 | |||
62 | /** @var Bibs */ |
||
63 | public $bibs; |
||
64 | |||
65 | /** @var Analytics */ |
||
66 | public $analytics; |
||
67 | |||
68 | /** @var Users */ |
||
69 | public $users; |
||
70 | |||
71 | /** @var Items */ |
||
72 | public $items; |
||
73 | |||
74 | /** @var int Max number of retries if we get 429 errors */ |
||
75 | public $maxAttempts = 10; |
||
76 | |||
77 | /** @var float Number of seconds to sleep before retrying */ |
||
78 | public $sleepTimeOnRetry = 0.5; |
||
79 | |||
80 | /** |
||
81 | * Create a new client to connect to a given Alma instance. |
||
82 | * |
||
83 | * @param string $key API key |
||
84 | * @param string $region Hosted region code, used to build base URL |
||
85 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
86 | * @param HttpClient $http |
||
87 | * @param MessageFactory $messageFactory |
||
88 | * @param UriFactory $uriFactory |
||
89 | * |
||
90 | * @throws \ErrorException |
||
91 | */ |
||
92 | public function __construct( |
||
129 | |||
130 | /** |
||
131 | * Attach an SRU client (so you can search for Bib records). |
||
132 | * |
||
133 | * @param SruClient $sru |
||
134 | */ |
||
135 | public function setSruClient(SruClient $sru) |
||
139 | |||
140 | /** |
||
141 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
142 | * |
||
143 | * @throws SruClientNotSetException |
||
144 | */ |
||
145 | public function assertHasSruClient() |
||
151 | |||
152 | /** |
||
153 | * Set the API key for this Alma instance. |
||
154 | * |
||
155 | * @param string $key The API key |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function setKey($key) |
||
165 | |||
166 | /** |
||
167 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
168 | * |
||
169 | * @param $regionCode |
||
170 | * |
||
171 | * @throws \ErrorException |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setRegion($regionCode) |
||
184 | |||
185 | /** |
||
186 | * @param string $url|UriInterface |
||
187 | * @param array $query |
||
188 | * |
||
189 | * @return UriInterface |
||
190 | */ |
||
191 | public function buildUrl($url, $query = []) |
||
204 | |||
205 | /** |
||
206 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
207 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
208 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
209 | * between each attempt to avoid hammering the server. |
||
210 | * |
||
211 | * @param RequestInterface $request |
||
212 | * @param int $attempt |
||
213 | * |
||
214 | * @return ResponseInterface |
||
215 | */ |
||
216 | public function request(RequestInterface $request, $attempt = 1) |
||
258 | |||
259 | /** |
||
260 | * Make a GET request. |
||
261 | * |
||
262 | * @param string $url|UriInterface |
||
263 | * @param array $query |
||
264 | * @param string $contentType |
||
265 | * |
||
266 | * @return string Response body |
||
267 | */ |
||
268 | public function get($url, $query = [], $contentType = 'application/json') |
||
279 | |||
280 | /** |
||
281 | * Make a GET request, accepting JSON. |
||
282 | * |
||
283 | * @param string $url |
||
284 | * @param array $query |
||
285 | * |
||
286 | * @return \stdClass JSON response as an object. |
||
287 | */ |
||
288 | public function getJSON($url, $query = []) |
||
294 | |||
295 | /** |
||
296 | * Make a GET request, accepting XML. |
||
297 | * |
||
298 | * @param string $url |
||
299 | * @param array $query |
||
300 | * |
||
301 | * @return QuiteSimpleXMLElement |
||
302 | */ |
||
303 | public function getXML($url, $query = []) |
||
309 | |||
310 | /** |
||
311 | * Make a PUT request. |
||
312 | * |
||
313 | * @param string $url |
||
314 | * @param $data |
||
315 | * @param string $contentType |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
333 | |||
334 | /** |
||
335 | * Make a PUT request, sending JSON data. |
||
336 | * |
||
337 | * @param string $url |
||
338 | * @param $data |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function putJSON($url, $data) |
||
348 | |||
349 | /** |
||
350 | * Make a PUT request, sending XML data. |
||
351 | * |
||
352 | * @param string $url |
||
353 | * @param $data |
||
354 | * |
||
355 | * @return bool |
||
356 | */ |
||
357 | public function putXML($url, $data) |
||
361 | |||
362 | /** |
||
363 | * Make a POST request. |
||
364 | * |
||
365 | * @param string $url |
||
366 | * @param $data |
||
367 | * @param string $contentType |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
385 | |||
386 | /** |
||
387 | * Make a POST request, sending JSON data. |
||
388 | * |
||
389 | * @param string $url |
||
390 | * @param $data |
||
391 | * |
||
392 | * @return bool |
||
393 | */ |
||
394 | public function postJSON($url, $data = null) |
||
400 | |||
401 | /** |
||
402 | * Make a POST request, sending XML data. |
||
403 | * |
||
404 | * @param string $url |
||
405 | * @param $data |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | public function postXML($url, $data = null) |
||
413 | |||
414 | /** |
||
415 | * Get the redirect target location of an URL, or null if not a redirect. |
||
416 | * |
||
417 | * @param string $url |
||
418 | * @param array $query |
||
419 | * |
||
420 | * @return string|null |
||
421 | */ |
||
422 | public function getRedirectLocation($url, $query = []) |
||
438 | |||
439 | /** |
||
440 | * @param class $className |
||
441 | * @param array ...$params |
||
442 | * @return mixed |
||
443 | */ |
||
444 | public function make($className, ...$params) |
||
448 | |||
449 | /** |
||
450 | * Generate a client exception. |
||
451 | * |
||
452 | * @param HttpClientErrorException $exception |
||
453 | * @return RequestFailed |
||
454 | */ |
||
455 | protected function exceptionFromClientError(HttpClientErrorException $exception) |
||
472 | } |
||
473 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: