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 |
||
37 | class Client |
||
38 | { |
||
39 | public $baseUrl; |
||
40 | |||
41 | /** @var string Alma zone (institution or network) */ |
||
42 | public $zone; |
||
43 | |||
44 | /** @var string Alma Developers Network API key for this zone */ |
||
45 | public $key; |
||
46 | |||
47 | /** @var Client Network zone instance */ |
||
48 | public $nz; |
||
49 | |||
50 | /** @var HttpClient */ |
||
51 | protected $http; |
||
52 | |||
53 | /** @var MessageFactory */ |
||
54 | protected $messageFactory; |
||
55 | |||
56 | /** @var UriFactory */ |
||
57 | protected $uriFactory; |
||
58 | |||
59 | /** @var SruClient */ |
||
60 | public $sru; |
||
61 | |||
62 | /** @var Bibs */ |
||
63 | public $bibs; |
||
64 | |||
65 | /** @var Analytics */ |
||
66 | public $analytics; |
||
67 | |||
68 | /** @var Users */ |
||
69 | public $users; |
||
70 | |||
71 | /** @var Items */ |
||
72 | public $items; |
||
73 | |||
74 | /** @var int Max number of retries if we get 429 errors */ |
||
75 | public $maxAttempts = 10; |
||
76 | |||
77 | /** @var float Number of seconds to sleep before retrying */ |
||
78 | public $sleepTimeOnRetry = 0.5; |
||
79 | |||
80 | /** |
||
81 | * Create a new client to connect to a given Alma instance. |
||
82 | * |
||
83 | * @param string $key API key |
||
84 | * @param string $region Hosted region code, used to build base URL |
||
85 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
86 | * @param HttpClient $http |
||
87 | * @param MessageFactory $messageFactory |
||
88 | * @param UriFactory $uriFactory |
||
89 | * |
||
90 | * @throws \ErrorException |
||
91 | */ |
||
92 | public function __construct( |
||
129 | |||
130 | /** |
||
131 | * Attach an SRU client (so you can search for Bib records). |
||
132 | * |
||
133 | * @param SruClient $sru |
||
134 | */ |
||
135 | public function setSruClient(SruClient $sru) |
||
139 | |||
140 | /** |
||
141 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
142 | * |
||
143 | * @throws SruClientNotSetException |
||
144 | */ |
||
145 | public function assertHasSruClient() |
||
151 | |||
152 | /** |
||
153 | * Set the API key for this Alma instance. |
||
154 | * |
||
155 | * @param string $key The API key |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function setKey($key) |
||
165 | |||
166 | /** |
||
167 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
168 | * |
||
169 | * @param $regionCode |
||
170 | * |
||
171 | * @throws \ErrorException |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setRegion($regionCode) |
||
184 | |||
185 | /** |
||
186 | * @param string $url|UriInterface |
||
187 | * @param array $query |
||
188 | * |
||
189 | * @return UriInterface |
||
190 | */ |
||
191 | public function buildUrl($url, $query = []) |
||
204 | |||
205 | /** |
||
206 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
207 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
208 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
209 | * between each attempt to avoid hammering the server. |
||
210 | * |
||
211 | * @param RequestInterface $request |
||
212 | * @param int $attempt |
||
213 | * |
||
214 | * @return ResponseInterface |
||
215 | */ |
||
216 | public function request(RequestInterface $request, $attempt = 1) |
||
259 | |||
260 | /** |
||
261 | * Make a GET request. |
||
262 | * |
||
263 | * @param string $url|UriInterface |
||
264 | * @param array $query |
||
265 | * @param string $contentType |
||
266 | * |
||
267 | * @return string Response body |
||
268 | */ |
||
269 | public function get($url, $query = [], $contentType = 'application/json') |
||
280 | |||
281 | /** |
||
282 | * Make a GET request, accepting JSON. |
||
283 | * |
||
284 | * @param string $url |
||
285 | * @param array $query |
||
286 | * |
||
287 | * @return \stdClass JSON response as an object. |
||
288 | */ |
||
289 | public function getJSON($url, $query = []) |
||
295 | |||
296 | /** |
||
297 | * Make a GET request, accepting XML. |
||
298 | * |
||
299 | * @param string $url |
||
300 | * @param array $query |
||
301 | * |
||
302 | * @return QuiteSimpleXMLElement |
||
303 | */ |
||
304 | public function getXML($url, $query = []) |
||
310 | |||
311 | /** |
||
312 | * Make a PUT request. |
||
313 | * |
||
314 | * @param string $url |
||
315 | * @param $data |
||
316 | * @param string $contentType |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | View Code Duplication | public function put($url, $data, $contentType = 'application/json') |
|
334 | |||
335 | /** |
||
336 | * Make a PUT request, sending JSON data. |
||
337 | * |
||
338 | * @param string $url |
||
339 | * @param $data |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | public function putJSON($url, $data) |
||
349 | |||
350 | /** |
||
351 | * Make a PUT request, sending XML data. |
||
352 | * |
||
353 | * @param string $url |
||
354 | * @param $data |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function putXML($url, $data) |
||
362 | |||
363 | /** |
||
364 | * Make a POST request. |
||
365 | * |
||
366 | * @param string $url |
||
367 | * @param $data |
||
368 | * @param string $contentType |
||
369 | * |
||
370 | * @return bool |
||
371 | */ |
||
372 | View Code Duplication | public function post($url, $data, $contentType = 'application/json') |
|
386 | |||
387 | /** |
||
388 | * Make a POST request, sending JSON data. |
||
389 | * |
||
390 | * @param string $url |
||
391 | * @param $data |
||
392 | * |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function postJSON($url, $data = null) |
||
401 | |||
402 | /** |
||
403 | * Make a POST request, sending XML data. |
||
404 | * |
||
405 | * @param string $url |
||
406 | * @param $data |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | public function postXML($url, $data = null) |
||
414 | |||
415 | /** |
||
416 | * Get the redirect target location of an URL, or null if not a redirect. |
||
417 | * |
||
418 | * @param string $url |
||
419 | * @param array $query |
||
420 | * |
||
421 | * @return string|null |
||
422 | */ |
||
423 | public function getRedirectLocation($url, $query = []) |
||
439 | |||
440 | /** |
||
441 | * @param class $className |
||
442 | * @param array ...$params |
||
443 | * @return mixed |
||
444 | */ |
||
445 | public function make($className, ...$params) |
||
449 | |||
450 | /** |
||
451 | * Generate a client exception. |
||
452 | * |
||
453 | * @param HttpClientErrorException $exception |
||
454 | * @return RequestFailed |
||
455 | */ |
||
456 | protected function parseClientError(HttpClientErrorException $exception) |
||
496 | } |
||
497 |
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: