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:
1 | <?php |
||
18 | class Client |
||
19 | { |
||
20 | public $baseUrl; |
||
21 | |||
22 | /** @var string Alma zone (institution or network) */ |
||
23 | public $zone; |
||
24 | |||
25 | /** @var string Alma Developers Network API key for this zone */ |
||
26 | public $key; |
||
27 | |||
28 | /** @var string Network zone instance */ |
||
29 | public $nz; |
||
30 | |||
31 | /** @var HttpClient */ |
||
32 | protected $httpClient; |
||
33 | |||
34 | /** @var SruClient */ |
||
35 | public $sru; |
||
36 | |||
37 | /** @var Bibs */ |
||
38 | public $bibs; |
||
39 | |||
40 | /** @var Analytics */ |
||
41 | public $analytics; |
||
42 | |||
43 | /** @var Users */ |
||
44 | public $users; |
||
45 | |||
46 | /** |
||
47 | * Create a new client to connect to a given Alma instance. |
||
48 | * |
||
49 | * @param string $key API key |
||
50 | * @param string $region Hosted region code, used to build base URL |
||
51 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
52 | * @param HttpClient $httpClient |
||
53 | * |
||
54 | * @throws \ErrorException |
||
55 | */ |
||
56 | public function __construct($key = null, $region = 'eu', $zone = Zones::INSTITUTION, HttpClient $httpClient = null) |
||
71 | |||
72 | /** |
||
73 | * Attach an SRU client (so you can search for Bib records). |
||
74 | * |
||
75 | * @param SruClient $sru |
||
76 | */ |
||
77 | public function setSruClient(SruClient $sru) |
||
78 | { |
||
79 | $this->sru = $sru; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
84 | * |
||
85 | * @throws SruClientNotSetException |
||
86 | */ |
||
87 | public function assertHasSruClient() |
||
93 | |||
94 | /** |
||
95 | * Set the API key for this Alma instance. |
||
96 | * |
||
97 | * @param string $key The API key |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setKey($key) |
||
101 | { |
||
102 | $this->key = $key; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
109 | * |
||
110 | * @param $regionCode |
||
111 | * @return $this |
||
112 | * @throws \ErrorException |
||
113 | */ |
||
114 | public function setRegion($regionCode) |
||
123 | |||
124 | /** |
||
125 | * @param $url |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function getFullUrl($url) |
||
133 | |||
134 | /** |
||
135 | * @param array $options |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function getHttpOptions($options = []) |
||
150 | |||
151 | /** |
||
152 | * Make a HTTP request. |
||
153 | * |
||
154 | * @param string $method |
||
155 | * @param string $url |
||
156 | * @param array $options |
||
157 | * |
||
158 | * @return \Psr\Http\Message\ResponseInterface |
||
159 | */ |
||
160 | public function request($method, $url, $options = []) |
||
168 | |||
169 | public function handleError($response) |
||
174 | |||
175 | /** |
||
176 | * Make a GET request, accepting JSON. |
||
177 | * |
||
178 | * @param string $url |
||
179 | * @param array $query |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | View Code Duplication | public function getJSON($url, $query = []) |
|
192 | |||
193 | /** |
||
194 | * Make a GET request, accepting XML. |
||
195 | * |
||
196 | * @param string $url |
||
197 | * @param array $query |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | View Code Duplication | public function getXML($url, $query = []) |
|
210 | |||
211 | /** |
||
212 | * Make a PUT request. |
||
213 | * |
||
214 | * @param string $url |
||
215 | * @param $data |
||
216 | * @param string $contentType |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function put($url, $data, $contentType = 'application/json') |
||
232 | |||
233 | /** |
||
234 | * Make a PUT request, sending JSON data. |
||
235 | * |
||
236 | * @param string $url |
||
237 | * @param $data |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function putJSON($url, $data) |
||
247 | |||
248 | /** |
||
249 | * Make a PUT request, sending XML data. |
||
250 | * |
||
251 | * @param string $url |
||
252 | * @param $data |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function putXML($url, $data) |
||
260 | |||
261 | /** |
||
262 | * Get the redirect target location of an URL, or null if not a redirect. |
||
263 | * |
||
264 | * @param string $url |
||
265 | * @param array $query |
||
266 | * |
||
267 | * @return string|null |
||
268 | */ |
||
269 | public function getRedirectLocation($url, $query = []) |
||
289 | } |
||
290 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..