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 |
||
40 | class Client |
||
41 | { |
||
42 | public $baseUrl; |
||
43 | |||
44 | /** @var string Alma zone (institution or network) */ |
||
45 | public $zone; |
||
46 | |||
47 | /** @var string Alma Developers Network API key for this zone */ |
||
48 | public $key; |
||
49 | |||
50 | /** @var Client Network zone instance */ |
||
51 | public $nz; |
||
52 | |||
53 | /** @var HttpClient */ |
||
54 | protected $http; |
||
55 | |||
56 | /** @var MessageFactory */ |
||
57 | protected $messageFactory; |
||
58 | |||
59 | /** @var UriFactory */ |
||
60 | protected $uriFactory; |
||
61 | |||
62 | /** @var SruClient */ |
||
63 | public $sru; |
||
64 | |||
65 | /** @var Bibs */ |
||
66 | public $bibs; |
||
67 | |||
68 | /** @var Analytics */ |
||
69 | public $analytics; |
||
70 | |||
71 | /** @var Users */ |
||
72 | public $users; |
||
73 | |||
74 | /** @var Items */ |
||
75 | public $items; |
||
76 | |||
77 | /** @var int Max number of retries if we get 429 errors */ |
||
78 | public $maxAttempts = 10; |
||
79 | |||
80 | /** @var float Number of seconds to sleep before retrying */ |
||
81 | public $sleepTimeOnRetry = 0.5; |
||
82 | |||
83 | /** |
||
84 | * Create a new client to connect to a given Alma instance. |
||
85 | * |
||
86 | * @param string $key API key |
||
87 | * @param string $region Hosted region code, used to build base URL |
||
88 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
89 | * @param HttpClient $http |
||
90 | * @param MessageFactory $messageFactory |
||
91 | * @param UriFactory $uriFactory |
||
92 | * |
||
93 | * @throws \ErrorException |
||
94 | */ |
||
95 | public function __construct( |
||
135 | |||
136 | public function lendingRequests(Library $library, $params = []) |
||
140 | |||
141 | /** |
||
142 | * Attach an SRU client (so you can search for Bib records). |
||
143 | * |
||
144 | * @param SruClient $sru |
||
145 | */ |
||
146 | public function setSruClient(SruClient $sru) |
||
150 | |||
151 | /** |
||
152 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
153 | * |
||
154 | * @throws SruClientNotSetException |
||
155 | */ |
||
156 | public function assertHasSruClient() |
||
162 | |||
163 | /** |
||
164 | * Set the API key for this Alma instance. |
||
165 | * |
||
166 | * @param string $key The API key |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function setKey($key) |
||
176 | |||
177 | /** |
||
178 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
179 | * |
||
180 | * @param $regionCode |
||
181 | * |
||
182 | * @throws \ErrorException |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setRegion($regionCode) |
||
195 | |||
196 | /** |
||
197 | * Extend an URL with query string parameters and return an UriInterface object. |
||
198 | * |
||
199 | * @param string $url |
||
200 | * @param array $query |
||
201 | * |
||
202 | * @return UriInterface |
||
203 | */ |
||
204 | protected function buildUrl($url, $query = []) |
||
222 | |||
223 | /** |
||
224 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
225 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
226 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
227 | * between each attempt to avoid hammering the server. |
||
228 | * |
||
229 | * @param RequestInterface $request |
||
230 | * @param int $attempt |
||
231 | * |
||
232 | * @return ResponseInterface |
||
233 | */ |
||
234 | public function request(RequestInterface $request, $attempt = 1) |
||
276 | |||
277 | /** |
||
278 | * Make a GET request. |
||
279 | * |
||
280 | * @param string $url |
||
281 | * @param array $query |
||
282 | * @param string $contentType |
||
283 | * |
||
284 | * @return string The response body |
||
285 | */ |
||
286 | public function get($url, $query = [], $contentType = 'application/json') |
||
297 | |||
298 | /** |
||
299 | * Make a GET request, accepting JSON. |
||
300 | * |
||
301 | * @param string $url |
||
302 | * @param array $query |
||
303 | * |
||
304 | * @return \stdClass JSON response as an object. |
||
305 | */ |
||
306 | public function getJSON($url, $query = []) |
||
312 | |||
313 | /** |
||
314 | * Make a GET request, accepting XML. |
||
315 | * |
||
316 | * @param string $url |
||
317 | * @param array $query |
||
318 | * |
||
319 | * @return QuiteSimpleXMLElement |
||
320 | */ |
||
321 | public function getXML($url, $query = []) |
||
327 | |||
328 | /** |
||
329 | * Make a PUT request. |
||
330 | * |
||
331 | * @param string $url |
||
332 | * @param $data |
||
333 | * @param string $contentType |
||
334 | * |
||
335 | * @return string The response body |
||
336 | */ |
||
337 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
350 | |||
351 | /** |
||
352 | * Make a PUT request, sending JSON data. |
||
353 | * |
||
354 | * @param string $url |
||
355 | * @param $data |
||
356 | * |
||
357 | * @return \stdClass |
||
358 | */ |
||
359 | public function putJSON($url, $data) |
||
365 | |||
366 | /** |
||
367 | * Make a PUT request, sending XML data. |
||
368 | * |
||
369 | * @param string $url |
||
370 | * @param $data |
||
371 | * |
||
372 | * @return QuiteSimpleXMLElement |
||
373 | */ |
||
374 | public function putXML($url, $data) |
||
380 | |||
381 | /** |
||
382 | * Make a POST request. |
||
383 | * |
||
384 | * @param string $url |
||
385 | * @param $data |
||
386 | * @param string $contentType |
||
387 | * |
||
388 | * @return string The response body |
||
389 | */ |
||
390 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
403 | |||
404 | /** |
||
405 | * Make a POST request, sending JSON data. |
||
406 | * |
||
407 | * @param string $url |
||
408 | * @param $data |
||
409 | * |
||
410 | * @return \stdClass |
||
411 | */ |
||
412 | public function postJSON($url, $data = null) |
||
418 | |||
419 | /** |
||
420 | * Make a POST request, sending XML data. |
||
421 | * |
||
422 | * @param string $url |
||
423 | * @param $data |
||
424 | * |
||
425 | * @return QuiteSimpleXMLElement |
||
426 | */ |
||
427 | public function postXML($url, $data = null) |
||
433 | |||
434 | /** |
||
435 | * Get the redirect target location of an URL, or null if not a redirect. |
||
436 | * |
||
437 | * @param string $url |
||
438 | * @param array $query |
||
439 | * |
||
440 | * @return string|null |
||
441 | */ |
||
442 | public function getRedirectLocation($url, $query = []) |
||
458 | |||
459 | /** |
||
460 | * @param class $className |
||
461 | * @param array ...$params |
||
462 | * @return mixed |
||
463 | */ |
||
464 | public function make($className, ...$params) |
||
468 | |||
469 | /** |
||
470 | * Generate a client exception. |
||
471 | * |
||
472 | * @param HttpException $exception |
||
473 | * @return RequestFailed |
||
474 | */ |
||
475 | protected function parseClientError(HttpException $exception) |
||
515 | } |
||
516 |
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: