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 $entrypoint; |
||
| 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 $headers = []; |
||
| 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 the entrypoint 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 | * |
||
| 124 | * @throws \ErrorException |
||
| 125 | */ |
||
| 126 | public function __construct( |
||
| 177 | |||
| 178 | public function lendingRequests(Library $library, $params = []) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Attach an SRU client (so you can search for Bib records). |
||
| 185 | * |
||
| 186 | * @param SruClient $sru |
||
| 187 | */ |
||
| 188 | public function setSruClient(SruClient $sru) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
| 195 | * |
||
| 196 | * @throws SruClientNotSetException |
||
| 197 | */ |
||
| 198 | public function assertHasSruClient() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set the API key for this Alma instance. |
||
| 207 | * |
||
| 208 | * @param string $key The API key |
||
| 209 | * |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function setKey($key) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
| 221 | * |
||
| 222 | * @param $regionCode |
||
| 223 | * @throws AlmaClientException |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setRegion($regionCode) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set the Alma API base url. |
||
| 238 | * |
||
| 239 | * @param string $url |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setEntryPoint(string $url) |
||
| 243 | { |
||
| 244 | $this->entrypoint = $url; |
||
| 245 | |||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set extra request headers. |
||
| 251 | * |
||
| 252 | * @param array $headers |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function setExtraHeaders(array $headers) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Extend an URL with query string parameters and return an UriInterface object. |
||
| 264 | * |
||
| 265 | * @param string $url |
||
| 266 | * @param array $query |
||
| 267 | * |
||
| 268 | * @return UriInterface |
||
| 269 | */ |
||
| 270 | public function buildUrl($url, $query = []) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
| 290 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
| 291 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
| 292 | * between each attempt to avoid hammering the server. |
||
| 293 | * |
||
| 294 | * @param RequestInterface $request |
||
| 295 | * @param int $attempt |
||
| 296 | * |
||
| 297 | * @return ResponseInterface |
||
| 298 | */ |
||
| 299 | public function request(RequestInterface $request, $attempt = 1) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Make a GET request. |
||
| 360 | * |
||
| 361 | * @param string $url |
||
| 362 | * @param array $query |
||
| 363 | * @param string $contentType |
||
| 364 | * |
||
| 365 | * @return string The response body |
||
| 366 | */ |
||
| 367 | public function get($url, $query = [], $contentType = 'application/json') |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Make a GET request, accepting JSON. |
||
| 380 | * |
||
| 381 | * @param string $url |
||
| 382 | * @param array $query |
||
| 383 | * |
||
| 384 | * @return \stdClass JSON response as an object. |
||
| 385 | */ |
||
| 386 | public function getJSON($url, $query = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Make a GET request, accepting XML. |
||
| 395 | * |
||
| 396 | * @param string $url |
||
| 397 | * @param array $query |
||
| 398 | * |
||
| 399 | * @return QuiteSimpleXMLElement |
||
| 400 | */ |
||
| 401 | public function getXML($url, $query = []) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Make a PUT request. |
||
| 410 | * |
||
| 411 | * @param string $url |
||
| 412 | * @param $data |
||
| 413 | * @param string $contentType |
||
| 414 | * |
||
| 415 | * @return string The response body |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Make a PUT request, sending JSON data. |
||
| 435 | * |
||
| 436 | * @param string $url |
||
| 437 | * @param $data |
||
| 438 | * |
||
| 439 | * @return \stdClass |
||
| 440 | */ |
||
| 441 | public function putJSON($url, $data) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Make a PUT request, sending XML data. |
||
| 450 | * |
||
| 451 | * @param string $url |
||
| 452 | * @param $data |
||
| 453 | * |
||
| 454 | * @return QuiteSimpleXMLElement |
||
| 455 | */ |
||
| 456 | public function putXML($url, $data) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Make a POST request. |
||
| 465 | * |
||
| 466 | * @param string $url |
||
| 467 | * @param $data |
||
| 468 | * @param string $contentType |
||
| 469 | * |
||
| 470 | * @return string The response body |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Make a POST request, sending JSON data. |
||
| 490 | * |
||
| 491 | * @param string $url |
||
| 492 | * @param $data |
||
| 493 | * |
||
| 494 | * @return \stdClass |
||
| 495 | */ |
||
| 496 | public function postJSON($url, $data = null) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Make a POST request, sending XML data. |
||
| 505 | * |
||
| 506 | * @param string $url |
||
| 507 | * @param $data |
||
| 508 | * |
||
| 509 | * @return QuiteSimpleXMLElement |
||
| 510 | */ |
||
| 511 | public function postXML($url, $data = null) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get the redirect target location of an URL, or null if not a redirect. |
||
| 520 | * |
||
| 521 | * @param string $url |
||
| 522 | * @param array $query |
||
| 523 | * |
||
| 524 | * @return string|null |
||
| 525 | */ |
||
| 526 | public function getRedirectLocation($url, $query = []) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param class $className |
||
| 545 | * @param array ...$params |
||
| 546 | * |
||
| 547 | * @return mixed |
||
| 548 | */ |
||
| 549 | public function make($className, ...$params) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Generate a client exception. |
||
| 556 | * |
||
| 557 | * @param HttpException $exception |
||
| 558 | * |
||
| 559 | * @return RequestFailed |
||
| 560 | */ |
||
| 561 | protected function parseClientError(HttpException $exception) |
||
| 608 | } |
||
| 609 |
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.