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 | /** |
||
| 92 | * @var Conf |
||
| 93 | */ |
||
| 94 | public $conf; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Libraries |
||
| 98 | */ |
||
| 99 | public $libraries; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var Jobs |
||
| 103 | */ |
||
| 104 | public $jobs; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var TaskLists |
||
| 108 | */ |
||
| 109 | public $taskLists; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Create a new client to connect to a given Alma instance. |
||
| 113 | * |
||
| 114 | * @param ?string $key API key |
||
|
|
|||
| 115 | * @param string $region Hosted region code, used to build base URL |
||
| 116 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
| 117 | * @param ?HttpClientInterface $http |
||
| 118 | * @param ?RequestFactoryInterface $requestFactory |
||
| 119 | * @param ?UriFactoryInterface $uriFactory |
||
| 120 | * @param ?string $baseUrl |
||
| 121 | * |
||
| 122 | * @throws \ErrorException |
||
| 123 | */ |
||
| 124 | public function __construct( |
||
| 125 | $key = null, |
||
| 126 | $region = 'eu', |
||
| 127 | $zone = Zones::INSTITUTION, |
||
| 128 | HttpClientInterface $http = null, |
||
| 129 | RequestFactoryInterface $requestFactory = null, |
||
| 130 | UriFactoryInterface $uriFactory = null, |
||
| 131 | string $baseUrl = null |
||
| 132 | ) { |
||
| 133 | $this->http = new PluginClient( |
||
| 134 | $http ?: HttpClient::client(), |
||
| 135 | [ |
||
| 136 | new ContentLengthPlugin(), |
||
| 137 | new ErrorPlugin(), |
||
| 138 | ] |
||
| 139 | ); |
||
| 140 | $this->requestFactory = $requestFactory ?: HttpFactory::requestFactory(); |
||
| 141 | $this->uriFactory = $uriFactory ?: HttpFactory::uriFactory(); |
||
| 142 | |||
| 143 | $this->key = $key; |
||
| 144 | |||
| 145 | if (!is_null($baseUrl)) { |
||
| 146 | $this->setBaseUrl($baseUrl); |
||
| 147 | } else { |
||
| 148 | $this->setRegion($region); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->zone = $zone; |
||
| 152 | |||
| 153 | $this->bibs = new Bibs($this); |
||
| 154 | $this->items = new Items($this); // Only needed for the fromBarcode method :/ |
||
| 155 | |||
| 156 | $this->analytics = new Analytics($this); |
||
| 157 | $this->users = new Users($this); |
||
| 158 | |||
| 159 | $this->conf = new Conf($this); |
||
| 160 | $this->libraries = $this->conf->libraries; // shortcut |
||
| 161 | $this->jobs = $this->conf->jobs; // shortcut |
||
| 162 | |||
| 163 | $this->taskLists = new TaskLists($this); |
||
| 164 | |||
| 165 | if ($zone == Zones::INSTITUTION) { |
||
| 166 | $this->nz = new self(null, $region, Zones::NETWORK, $this->http, $this->requestFactory, $this->uriFactory, $baseUrl); |
||
| 167 | } elseif ($zone != Zones::NETWORK) { |
||
| 168 | throw new AlmaClientException('Invalid zone name.'); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | public function lendingRequests(Library $library, $params = []) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Attach an SRU client (so you can search for Bib records). |
||
| 179 | * |
||
| 180 | * @param SruClient $sru |
||
| 181 | */ |
||
| 182 | public function setSruClient(SruClient $sru) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
| 189 | * |
||
| 190 | * @throws SruClientNotSetException |
||
| 191 | */ |
||
| 192 | public function assertHasSruClient() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set the API key for this Alma instance. |
||
| 201 | * |
||
| 202 | * @param string $key The API key |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function setKey($key) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
| 215 | * |
||
| 216 | * @param $regionCode |
||
| 217 | * @throws AlmaClientException |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function setRegion($regionCode) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the Alma API base url. |
||
| 232 | * |
||
| 233 | * @param string $baseUrl |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function setBaseUrl(string $baseUrl) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Extend an URL with query string parameters and return an UriInterface object. |
||
| 249 | * |
||
| 250 | * @param string $url |
||
| 251 | * @param array $query |
||
| 252 | * |
||
| 253 | * @return UriInterface |
||
| 254 | */ |
||
| 255 | public function buildUrl($url, $query = []) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
| 276 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
| 277 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
| 278 | * between each attempt to avoid hammering the server. |
||
| 279 | * |
||
| 280 | * @param RequestInterface $request |
||
| 281 | * @param int $attempt |
||
| 282 | * |
||
| 283 | * @return ResponseInterface |
||
| 284 | */ |
||
| 285 | public function request(RequestInterface $request, $attempt = 1) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Make a GET request. |
||
| 343 | * |
||
| 344 | * @param string $url |
||
| 345 | * @param array $query |
||
| 346 | * @param string $contentType |
||
| 347 | * |
||
| 348 | * @return string The response body |
||
| 349 | */ |
||
| 350 | public function get($url, $query = [], $contentType = 'application/json') |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Make a GET request, accepting JSON. |
||
| 363 | * |
||
| 364 | * @param string $url |
||
| 365 | * @param array $query |
||
| 366 | * |
||
| 367 | * @return \stdClass JSON response as an object. |
||
| 368 | */ |
||
| 369 | public function getJSON($url, $query = []) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Make a GET request, accepting XML. |
||
| 378 | * |
||
| 379 | * @param string $url |
||
| 380 | * @param array $query |
||
| 381 | * |
||
| 382 | * @return QuiteSimpleXMLElement |
||
| 383 | */ |
||
| 384 | public function getXML($url, $query = []) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Make a PUT request. |
||
| 393 | * |
||
| 394 | * @param string $url |
||
| 395 | * @param $data |
||
| 396 | * @param string $contentType |
||
| 397 | * |
||
| 398 | * @return string The response body |
||
| 399 | */ |
||
| 400 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Make a PUT request, sending JSON data. |
||
| 418 | * |
||
| 419 | * @param string $url |
||
| 420 | * @param $data |
||
| 421 | * |
||
| 422 | * @return \stdClass |
||
| 423 | */ |
||
| 424 | public function putJSON($url, $data) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Make a PUT request, sending XML data. |
||
| 433 | * |
||
| 434 | * @param string $url |
||
| 435 | * @param $data |
||
| 436 | * |
||
| 437 | * @return QuiteSimpleXMLElement |
||
| 438 | */ |
||
| 439 | public function putXML($url, $data) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Make a POST request. |
||
| 448 | * |
||
| 449 | * @param string $url |
||
| 450 | * @param $data |
||
| 451 | * @param string $contentType |
||
| 452 | * |
||
| 453 | * @return string The response body |
||
| 454 | */ |
||
| 455 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Make a POST request, sending JSON data. |
||
| 473 | * |
||
| 474 | * @param string $url |
||
| 475 | * @param $data |
||
| 476 | * |
||
| 477 | * @return \stdClass |
||
| 478 | */ |
||
| 479 | public function postJSON($url, $data = null) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Make a POST request, sending XML data. |
||
| 488 | * |
||
| 489 | * @param string $url |
||
| 490 | * @param $data |
||
| 491 | * |
||
| 492 | * @return QuiteSimpleXMLElement |
||
| 493 | */ |
||
| 494 | public function postXML($url, $data = null) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get the redirect target location of an URL, or null if not a redirect. |
||
| 503 | * |
||
| 504 | * @param string $url |
||
| 505 | * @param array $query |
||
| 506 | * |
||
| 507 | * @return string|null |
||
| 508 | */ |
||
| 509 | public function getRedirectLocation($url, $query = []) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param class $className |
||
| 528 | * @param array ...$params |
||
| 529 | * |
||
| 530 | * @return mixed |
||
| 531 | */ |
||
| 532 | public function make($className, ...$params) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Generate a client exception. |
||
| 539 | * |
||
| 540 | * @param HttpException $exception |
||
| 541 | * |
||
| 542 | * @return RequestFailed |
||
| 543 | */ |
||
| 544 | protected function parseClientError(HttpException $exception) |
||
| 587 | } |
||
| 588 |
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.