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) |
||
| 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) |
||
| 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') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Make a PUT request. |
||
| 237 | * |
||
| 238 | * @param string $url |
||
| 239 | * @param $data |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function putXML($url, $data) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the redirect target location of an URL, or null if not a redirect. |
||
| 250 | * |
||
| 251 | * @param string $url |
||
| 252 | * @param array $query |
||
| 253 | * |
||
| 254 | * @return string|null |
||
| 255 | */ |
||
| 256 | public function getRedirectLocation($url, $query = []) |
||
| 276 | } |
||
| 277 |
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..