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 |
||
| 15 | class Client extends \GuzzleHttp\Client |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @const string Version number |
||
| 19 | */ |
||
| 20 | const VERSION = '0.0.1'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
||
| 24 | * (127.0.0.1). |
||
| 25 | * |
||
| 26 | * @var array[] |
||
| 27 | */ |
||
| 28 | protected $options = [ |
||
| 29 | 'scheme' => 'https', |
||
| 30 | 'hostname' => 'localhost', |
||
| 31 | 'port' => '8080', |
||
| 32 | 'token' => null, |
||
| 33 | 'username' => null, |
||
| 34 | 'password' => null, |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $options array |
||
| 39 | */ |
||
| 40 | 17 | public function __construct($options = []) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Set the bearer token. |
||
| 66 | * |
||
| 67 | * @param string $token Bearer Token from Auth request |
||
| 68 | */ |
||
| 69 | 9 | public function setBearerToken($token) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Authenticate and get Bearer token from SmartCall. |
||
| 76 | * |
||
| 77 | * @param string $username |
||
| 78 | * @param string $password |
||
| 79 | * |
||
| 80 | * @throws Exception |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 3 | View Code Duplication | public function auth($username, $password) |
| 116 | |||
| 117 | /** |
||
| 118 | * Authenticate and invalidates all the user allocated tokens. |
||
| 119 | * |
||
| 120 | * @throws Exception |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 2 | View Code Duplication | public function authDelete() |
| 125 | { |
||
| 126 | try { |
||
| 127 | 2 | $response = $this->delete( |
|
| 128 | 2 | '/webservice/auth', |
|
| 129 | [ |
||
| 130 | 'headers' => [ |
||
| 131 | 2 | 'Authorization' => sprintf( |
|
| 132 | 2 | 'Bearer %s', |
|
| 133 | 2 | $this->options['token'] |
|
| 134 | ), |
||
| 135 | ], |
||
| 136 | ] |
||
| 137 | ); |
||
| 138 | |||
| 139 | return [ |
||
| 140 | 1 | 'status' => 'ok', |
|
| 141 | 1 | 'http_code' => $response->getStatusCode(), |
|
| 142 | 1 | 'body' => (string) $response->getBody(), |
|
| 143 | ]; |
||
| 144 | 1 | } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
| 145 | 1 | return $this->clientError($e); |
|
| 146 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
| 147 | return $this->parseError($e); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Authenticate and invalidates all the user allocated tokens. |
||
| 153 | * |
||
| 154 | * @param string $username |
||
| 155 | * @param string $password |
||
| 156 | * |
||
| 157 | * @throws Exception |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function authFlush($username, $password) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Authenticate and gets the number of available session tokens. |
||
| 196 | * |
||
| 197 | * @param string $username |
||
| 198 | * @param string $password |
||
| 199 | * |
||
| 200 | * @throws Exception |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 3 | View Code Duplication | public function authToken($username, $password) |
| 236 | |||
| 237 | /** |
||
| 238 | * Authenticate and request period based cashup reports. |
||
| 239 | * |
||
| 240 | * @param string $dealerMsisdn |
||
| 241 | * @param string $start |
||
| 242 | * @param string $end |
||
| 243 | * |
||
| 244 | * @throws Exception |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function cashup($dealerMsisdn, $start, $end) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Authenticate and request current day cashup report. |
||
| 282 | * |
||
| 283 | * @param string $dealerMsisdn |
||
| 284 | * |
||
| 285 | * @throws Exception |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function cashupToday($dealerMsisdn) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Authenticate and retrieves a list of all available networks. |
||
| 326 | * |
||
| 327 | * @throws Exception |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | 2 | View Code Duplication | public function network($id) |
| 360 | |||
| 361 | /** |
||
| 362 | * Authenticate and retrieves a list of all available networks. |
||
| 363 | * |
||
| 364 | * @throws Exception |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 2 | View Code Duplication | public function networks() |
| 394 | |||
| 395 | /** |
||
| 396 | * Test SmartCall is responding. |
||
| 397 | * |
||
| 398 | * @throws Exception |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | 1 | public function ping() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Authenticate and retrieves a list of all available networks. |
||
| 423 | * |
||
| 424 | * @throws Exception |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | 2 | View Code Duplication | public function products($id) |
| 457 | |||
| 458 | /** |
||
| 459 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
| 460 | * |
||
| 461 | * @param \GuzzleHttp\Exception\ServerException $exception |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | 8 | View Code Duplication | private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
| 475 | |||
| 476 | /** |
||
| 477 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
| 478 | * |
||
| 479 | * @param \GuzzleHttp\Exception\ServerException $exception |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | View Code Duplication | private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
| 493 | } |
||
| 494 |
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..