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 |
||
43 | { |
||
44 | public $baseUrl; |
||
45 | |||
46 | /** @var string Alma zone (institution or network) */ |
||
47 | public $zone; |
||
48 | |||
49 | /** @var string Alma Developers Network API key for this zone */ |
||
50 | public $key; |
||
51 | |||
52 | /** @var Client Network zone instance */ |
||
53 | public $nz; |
||
54 | |||
55 | /** @var HttpClientInterface */ |
||
56 | protected $http; |
||
57 | |||
58 | /** @var RequestFactoryInterface */ |
||
59 | protected $requestFactory; |
||
60 | |||
61 | /** @var UriFactory */ |
||
62 | protected $uriFactory; |
||
63 | |||
64 | /** @var SruClient */ |
||
65 | public $sru; |
||
66 | |||
67 | /** @var Bibs */ |
||
68 | public $bibs; |
||
69 | |||
70 | /** @var Analytics */ |
||
71 | public $analytics; |
||
72 | |||
73 | /** @var Users */ |
||
74 | public $users; |
||
75 | |||
76 | /** @var Items */ |
||
77 | public $items; |
||
78 | |||
79 | /** @var int Max number of retries if we get 429 errors */ |
||
80 | public $maxAttempts = 10; |
||
81 | |||
82 | /** @var int Max number of retries if we get 5XX errors */ |
||
83 | public $maxAttemptsOnServerError = 1; |
||
84 | |||
85 | /** @var float Number of seconds to sleep before retrying */ |
||
86 | public $sleepTimeOnRetry = 0.5; |
||
87 | |||
88 | /** @var float Number of seconds to sleep before retrying after a server error */ |
||
89 | public $sleepTimeOnServerError = 10; |
||
90 | |||
91 | /** @var array Extra request headers */ |
||
92 | public $extraHeaders = []; |
||
93 | |||
94 | /** |
||
95 | * @var Conf |
||
96 | */ |
||
97 | public $conf; |
||
98 | |||
99 | /** |
||
100 | * @var Libraries |
||
101 | */ |
||
102 | public $libraries; |
||
103 | |||
104 | /** |
||
105 | * @var Jobs |
||
106 | */ |
||
107 | public $jobs; |
||
108 | |||
109 | /** |
||
110 | * @var TaskLists |
||
111 | */ |
||
112 | public $taskLists; |
||
113 | |||
114 | /** |
||
115 | * Create a new client to connect to a given Alma instance. |
||
116 | * |
||
117 | * @param ?string $key API key |
||
|
|||
118 | * @param string $region Hosted region code, used to build base URL |
||
119 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
120 | * @param ?HttpClientInterface $http |
||
121 | * @param ?RequestFactoryInterface $requestFactory |
||
122 | * @param ?UriFactoryInterface $uriFactory |
||
123 | * @param ?string $baseUrl |
||
124 | * @param array $extraHeaders |
||
125 | * |
||
126 | * @throws \ErrorException |
||
127 | */ |
||
128 | public function __construct( |
||
187 | |||
188 | public function lendingRequests(Library $library, $params = []) |
||
192 | |||
193 | /** |
||
194 | * Attach an SRU client (so you can search for Bib records). |
||
195 | * |
||
196 | * @param SruClient $sru |
||
197 | */ |
||
198 | public function setSruClient(SruClient $sru) |
||
202 | |||
203 | /** |
||
204 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
205 | * |
||
206 | * @throws SruClientNotSetException |
||
207 | */ |
||
208 | public function assertHasSruClient() |
||
214 | |||
215 | /** |
||
216 | * Set the API key for this Alma instance. |
||
217 | * |
||
218 | * @param string $key The API key |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setKey($key) |
||
228 | |||
229 | /** |
||
230 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
231 | * |
||
232 | * @param $regionCode |
||
233 | * @throws AlmaClientException |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function setRegion($regionCode) |
||
245 | |||
246 | /** |
||
247 | * Set the Alma API base url. |
||
248 | * |
||
249 | * @param string $baseUrl |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function setBaseUrl(string $baseUrl) |
||
262 | |||
263 | /** |
||
264 | * Extend an URL with query string parameters and return an UriInterface object. |
||
265 | * |
||
266 | * @param string $url |
||
267 | * @param array $query |
||
268 | * |
||
269 | * @return UriInterface |
||
270 | */ |
||
271 | public function buildUrl($url, $query = []) |
||
288 | |||
289 | /** |
||
290 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
291 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
292 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
293 | * between each attempt to avoid hammering the server. |
||
294 | * |
||
295 | * @param RequestInterface $request |
||
296 | * @param int $attempt |
||
297 | * |
||
298 | * @return ResponseInterface |
||
299 | */ |
||
300 | public function request(RequestInterface $request, $attempt = 1) |
||
358 | |||
359 | /** |
||
360 | * Make a GET request. |
||
361 | * |
||
362 | * @param string $url |
||
363 | * @param array $query |
||
364 | * @param string $contentType |
||
365 | * |
||
366 | * @return string The response body |
||
367 | */ |
||
368 | public function get($url, $query = [], $contentType = 'application/json') |
||
378 | |||
379 | /** |
||
380 | * Make a GET request, accepting JSON. |
||
381 | * |
||
382 | * @param string $url |
||
383 | * @param array $query |
||
384 | * |
||
385 | * @return \stdClass JSON response as an object. |
||
386 | */ |
||
387 | public function getJSON($url, $query = []) |
||
393 | |||
394 | /** |
||
395 | * Make a GET request, accepting XML. |
||
396 | * |
||
397 | * @param string $url |
||
398 | * @param array $query |
||
399 | * |
||
400 | * @return QuiteSimpleXMLElement |
||
401 | */ |
||
402 | public function getXML($url, $query = []) |
||
408 | |||
409 | /** |
||
410 | * Make a PUT request. |
||
411 | * |
||
412 | * @param string $url |
||
413 | * @param $data |
||
414 | * @param string $contentType |
||
415 | * |
||
416 | * @return string The response body |
||
417 | */ |
||
418 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
433 | |||
434 | /** |
||
435 | * Make a PUT request, sending JSON data. |
||
436 | * |
||
437 | * @param string $url |
||
438 | * @param $data |
||
439 | * |
||
440 | * @return \stdClass |
||
441 | */ |
||
442 | public function putJSON($url, $data) |
||
448 | |||
449 | /** |
||
450 | * Make a PUT request, sending XML data. |
||
451 | * |
||
452 | * @param string $url |
||
453 | * @param $data |
||
454 | * |
||
455 | * @return QuiteSimpleXMLElement |
||
456 | */ |
||
457 | public function putXML($url, $data) |
||
463 | |||
464 | /** |
||
465 | * Make a POST request. |
||
466 | * |
||
467 | * @param string $url |
||
468 | * @param $data |
||
469 | * @param string $contentType |
||
470 | * |
||
471 | * @return string The response body |
||
472 | */ |
||
473 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
488 | |||
489 | /** |
||
490 | * Make a POST request, sending JSON data. |
||
491 | * |
||
492 | * @param string $url |
||
493 | * @param $data |
||
494 | * |
||
495 | * @return \stdClass |
||
496 | */ |
||
497 | public function postJSON($url, $data = null) |
||
503 | |||
504 | /** |
||
505 | * Make a POST request, sending XML data. |
||
506 | * |
||
507 | * @param string $url |
||
508 | * @param $data |
||
509 | * |
||
510 | * @return QuiteSimpleXMLElement |
||
511 | */ |
||
512 | public function postXML($url, $data = null) |
||
518 | |||
519 | /** |
||
520 | * Get the redirect target location of an URL, or null if not a redirect. |
||
521 | * |
||
522 | * @param string $url |
||
523 | * @param array $query |
||
524 | * |
||
525 | * @return string|null |
||
526 | */ |
||
527 | public function getRedirectLocation($url, $query = []) |
||
543 | |||
544 | /** |
||
545 | * @param class $className |
||
546 | * @param array ...$params |
||
547 | * |
||
548 | * @return mixed |
||
549 | */ |
||
550 | public function make($className, ...$params) |
||
554 | |||
555 | /** |
||
556 | * Generate a client exception. |
||
557 | * |
||
558 | * @param HttpException $exception |
||
559 | * |
||
560 | * @return RequestFailed |
||
561 | */ |
||
562 | protected function parseClientError(HttpException $exception) |
||
609 | } |
||
610 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.