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 string default language for API results */ |
||
| 53 | public $lang; |
||
| 54 | |||
| 55 | /** @var Client Network zone instance */ |
||
| 56 | public $nz; |
||
| 57 | |||
| 58 | /** @var HttpClientInterface */ |
||
| 59 | protected $http; |
||
| 60 | |||
| 61 | /** @var RequestFactoryInterface */ |
||
| 62 | protected $requestFactory; |
||
| 63 | |||
| 64 | /** @var UriFactory */ |
||
| 65 | protected $uriFactory; |
||
| 66 | |||
| 67 | /** @var SruClient */ |
||
| 68 | public $sru; |
||
| 69 | |||
| 70 | /** @var Bibs */ |
||
| 71 | public $bibs; |
||
| 72 | |||
| 73 | /** @var Analytics */ |
||
| 74 | public $analytics; |
||
| 75 | |||
| 76 | /** @var Users */ |
||
| 77 | public $users; |
||
| 78 | |||
| 79 | /** @var Items */ |
||
| 80 | public $items; |
||
| 81 | |||
| 82 | /** @var int Max number of retries if we get 429 errors */ |
||
| 83 | public $maxAttempts = 10; |
||
| 84 | |||
| 85 | /** @var int Max number of retries if we get 5XX errors */ |
||
| 86 | public $maxAttemptsOnServerError = 1; |
||
| 87 | |||
| 88 | /** @var float Number of seconds to sleep before retrying */ |
||
| 89 | public $sleepTimeOnRetry = 0.5; |
||
| 90 | |||
| 91 | /** @var float Number of seconds to sleep before retrying after a server error */ |
||
| 92 | public $sleepTimeOnServerError = 10; |
||
| 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 | * |
||
| 124 | * @throws \ErrorException |
||
| 125 | */ |
||
| 126 | public function __construct( |
||
| 167 | |||
| 168 | public function lendingRequests(Library $library, $params = []) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Attach an SRU client (so you can search for Bib records). |
||
| 175 | * |
||
| 176 | * @param SruClient $sru |
||
| 177 | */ |
||
| 178 | public function setSruClient(SruClient $sru) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
| 185 | * |
||
| 186 | * @throws SruClientNotSetException |
||
| 187 | */ |
||
| 188 | public function assertHasSruClient() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the API key for this Alma instance. |
||
| 197 | * |
||
| 198 | * @param string $key The API key |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function setKey($key) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set the default language for the API. |
||
| 211 | * |
||
| 212 | * @param string $lang The language |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function setLang($lang) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
| 225 | * |
||
| 226 | * @param $regionCode |
||
| 227 | * |
||
| 228 | * @throws \ErrorException |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function setRegion($regionCode) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Extend an URL with query string parameters and return an UriInterface object. |
||
| 244 | * |
||
| 245 | * @param string $url |
||
| 246 | * @param array $query |
||
| 247 | * |
||
| 248 | * @return UriInterface |
||
| 249 | */ |
||
| 250 | public function buildUrl($url, $query = []) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
| 272 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
| 273 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
| 274 | * between each attempt to avoid hammering the server. |
||
| 275 | * |
||
| 276 | * @param RequestInterface $request |
||
| 277 | * @param int $attempt |
||
| 278 | * |
||
| 279 | * @return ResponseInterface |
||
| 280 | */ |
||
| 281 | public function request(RequestInterface $request, $attempt = 1) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Make a GET request. |
||
| 339 | * |
||
| 340 | * @param string $url |
||
| 341 | * @param array $query |
||
| 342 | * @param string $contentType |
||
| 343 | * |
||
| 344 | * @return string The response body |
||
| 345 | */ |
||
| 346 | public function get($url, $query = [], $contentType = 'application/json') |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Make a GET request, accepting JSON. |
||
| 359 | * |
||
| 360 | * @param string $url |
||
| 361 | * @param array $query |
||
| 362 | * |
||
| 363 | * @return \stdClass JSON response as an object. |
||
| 364 | */ |
||
| 365 | public function getJSON($url, $query = []) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Make a GET request, accepting XML. |
||
| 374 | * |
||
| 375 | * @param string $url |
||
| 376 | * @param array $query |
||
| 377 | * |
||
| 378 | * @return QuiteSimpleXMLElement |
||
| 379 | */ |
||
| 380 | public function getXML($url, $query = []) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Make a PUT request. |
||
| 389 | * |
||
| 390 | * @param string $url |
||
| 391 | * @param $data |
||
| 392 | * @param string $contentType |
||
| 393 | * |
||
| 394 | * @return string The response body |
||
| 395 | */ |
||
| 396 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Make a PUT request, sending JSON data. |
||
| 414 | * |
||
| 415 | * @param string $url |
||
| 416 | * @param $data |
||
| 417 | * |
||
| 418 | * @return \stdClass |
||
| 419 | */ |
||
| 420 | public function putJSON($url, $data) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Make a PUT request, sending XML data. |
||
| 429 | * |
||
| 430 | * @param string $url |
||
| 431 | * @param $data |
||
| 432 | * |
||
| 433 | * @return QuiteSimpleXMLElement |
||
| 434 | */ |
||
| 435 | public function putXML($url, $data) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Make a POST request. |
||
| 444 | * |
||
| 445 | * @param string $url |
||
| 446 | * @param $data |
||
| 447 | * @param string $contentType |
||
| 448 | * |
||
| 449 | * @return string The response body |
||
| 450 | */ |
||
| 451 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Make a POST request, sending JSON data. |
||
| 469 | * |
||
| 470 | * @param string $url |
||
| 471 | * @param $data |
||
| 472 | * |
||
| 473 | * @return \stdClass |
||
| 474 | */ |
||
| 475 | public function postJSON($url, $data = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Make a POST request, sending XML data. |
||
| 484 | * |
||
| 485 | * @param string $url |
||
| 486 | * @param $data |
||
| 487 | * |
||
| 488 | * @return QuiteSimpleXMLElement |
||
| 489 | */ |
||
| 490 | public function postXML($url, $data = null) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get the redirect target location of an URL, or null if not a redirect. |
||
| 499 | * |
||
| 500 | * @param string $url |
||
| 501 | * @param array $query |
||
| 502 | * |
||
| 503 | * @return string|null |
||
| 504 | */ |
||
| 505 | public function getRedirectLocation($url, $query = []) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param class $className |
||
| 524 | * @param array ...$params |
||
| 525 | * |
||
| 526 | * @return mixed |
||
| 527 | */ |
||
| 528 | public function make($className, ...$params) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Generate a client exception. |
||
| 535 | * |
||
| 536 | * @param HttpException $exception |
||
| 537 | * |
||
| 538 | * @return RequestFailed |
||
| 539 | */ |
||
| 540 | protected function parseClientError(HttpException $exception) |
||
| 583 | } |
||
| 584 |
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.