Complex classes like ApiClient 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 ApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ApiClient |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var |
||
| 28 | */ |
||
| 29 | protected $httpClient; |
||
| 30 | /** |
||
| 31 | * @var mixed|string |
||
| 32 | */ |
||
| 33 | protected $baseUri; |
||
| 34 | /** |
||
| 35 | * @var float|mixed |
||
| 36 | */ |
||
| 37 | protected $outTime; |
||
| 38 | /** |
||
| 39 | * @var mixed|string |
||
| 40 | */ |
||
| 41 | protected $apiKey; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var mixed|string |
||
| 45 | */ |
||
| 46 | protected $apiSecret; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var mixed|string |
||
| 50 | */ |
||
| 51 | protected $apiVersion; |
||
| 52 | /** |
||
| 53 | * @var \Psr\Http\Message\RequestInterface |
||
| 54 | */ |
||
| 55 | protected $request; |
||
| 56 | /** |
||
| 57 | * @var \Psr\Http\Message\ResponseInterface |
||
| 58 | */ |
||
| 59 | protected $response; |
||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $requestData; |
||
| 64 | /** |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | protected $responseCode; |
||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $responseBody; |
||
| 72 | /** |
||
| 73 | * @var |
||
| 74 | */ |
||
| 75 | protected $authenticator; |
||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $lastUrl = ''; |
||
| 80 | /** |
||
| 81 | * @var double |
||
| 82 | */ |
||
| 83 | protected $transferTime = 0; |
||
| 84 | /** |
||
| 85 | * @var callable |
||
| 86 | */ |
||
| 87 | protected $completeCallBack; |
||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $log = false; |
||
| 92 | /** |
||
| 93 | * @var $handlerStack |
||
| 94 | */ |
||
| 95 | protected $handlerStack; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * ApiClient constructor. |
||
| 99 | * |
||
| 100 | * @param array $options |
||
| 101 | */ |
||
| 102 | 18 | public function __construct(array $options = []) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * 设置 base uri |
||
| 113 | * |
||
| 114 | * @param $baseUri |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | 1 | public function setBaseUri($baseUri) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * 获取 base uri |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 1 | public function getBaseUri() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * 设置 api key 和 api secret |
||
| 137 | * |
||
| 138 | * @param $apiKey |
||
| 139 | * @param $apiSecret |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | 1 | public function setCustomer($apiKey, $apiSecret) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * 获取 api key |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public function getApiKey() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * 设置 api key |
||
| 163 | * |
||
| 164 | * @param mixed|string $apiKey |
||
| 165 | */ |
||
| 166 | 1 | public function setApiKey($apiKey) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * 获取 api secret |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 1 | public function getApiSecret() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * 设置 api secret |
||
| 183 | * |
||
| 184 | * @param mixed|string $apiSecret |
||
| 185 | */ |
||
| 186 | 1 | public function setApiSecret($apiSecret) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * 获取 api version |
||
| 193 | * |
||
| 194 | * @return mixed|string |
||
| 195 | */ |
||
| 196 | 1 | public function getApiVersion() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * 设置 api version |
||
| 203 | * |
||
| 204 | * @param $apiVersion |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | 1 | public function setApiVersion($apiVersion) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * 设置超时时间 |
||
| 217 | * |
||
| 218 | * @param $time |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 1 | public function setOutTime($time) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * 获取当前设置的超时时间 |
||
| 231 | * |
||
| 232 | * @return float|mixed |
||
| 233 | */ |
||
| 234 | 1 | public function getOutTime() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * get a response code |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | 1 | public function getResponseCode() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * 获取请求的数据 |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | 1 | public function getRequestData() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @return array |
||
| 260 | * @throws ResponseDataParseException |
||
| 261 | */ |
||
| 262 | 2 | public function getResponseData() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return \Psr\Http\Message\ResponseInterface \GuzzleHttp\Psr7\RequestInterface |
||
| 277 | */ |
||
| 278 | 4 | public function getResponse() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @return \Psr\Http\Message\RequestInterface \GuzzleHttp\Psr7\Request |
||
| 285 | */ |
||
| 286 | 1 | public function getRequest() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @return Client|ClientInterface |
||
| 293 | * @throws ApiClientInitException |
||
| 294 | */ |
||
| 295 | 10 | public function getHttpClient() |
|
| 296 | { |
||
| 297 | 10 | if (!$this->baseUri) { |
|
| 298 | 1 | throw new ApiClientInitException('没有配置有效的 BaseUri'); |
|
| 299 | } |
||
| 300 | 9 | if (!($this->httpClient instanceof ClientInterface)) { |
|
| 301 | 9 | $this->httpClient = new Client([ |
|
| 302 | 9 | 'base_uri' => rtrim($this->baseUri, '/') . '/', |
|
| 303 | 9 | 'time_out' => $this->outTime, |
|
| 304 | ]); |
||
| 305 | } |
||
| 306 | |||
| 307 | 9 | return $this->httpClient; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Call a Get Request |
||
| 312 | * |
||
| 313 | * @param string $uri |
||
| 314 | * @param array $query |
||
| 315 | * |
||
| 316 | * @return mixed|null |
||
| 317 | */ |
||
| 318 | 9 | public function get($uri, $query = []) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Call a Post Request |
||
| 325 | * |
||
| 326 | * @param string $uri |
||
| 327 | * @param array $data |
||
| 328 | * |
||
| 329 | * @return mixed|null |
||
| 330 | */ |
||
| 331 | public function post($uri, $data = []) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Call a Put Request |
||
| 338 | * |
||
| 339 | * @param string $uri |
||
| 340 | * @param array $data |
||
| 341 | * |
||
| 342 | * @return mixed|null |
||
| 343 | */ |
||
| 344 | public function put($uri, $data = []) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Call a delete Request |
||
| 351 | * |
||
| 352 | * @param string $uri |
||
| 353 | * @param array $query |
||
| 354 | * |
||
| 355 | * @return ApiClient |
||
| 356 | */ |
||
| 357 | public function delete($uri, $query = []) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Call a patch Request |
||
| 364 | * |
||
| 365 | * @param string $uri |
||
| 366 | * @param array $data |
||
| 367 | * |
||
| 368 | * @return ApiClient |
||
| 369 | */ |
||
| 370 | public function patch($uri, $data = []) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Call a Request |
||
| 377 | * |
||
| 378 | * @param string $method |
||
| 379 | * @param string $uri |
||
| 380 | * @param array $optionals |
||
| 381 | * |
||
| 382 | * @return $this |
||
| 383 | * @throws \Exception |
||
| 384 | */ |
||
| 385 | 9 | public function request($method, $uri, $optionals = []) |
|
| 386 | { |
||
| 387 | 9 | $options = $this->extractOptionalParameters($uri, $optionals); |
|
| 388 | 9 | $uri = $this->compileRoute($uri, $optionals); |
|
| 389 | 9 | $data = []; |
|
| 390 | 9 | $method = strtoupper($method); |
|
| 391 | |||
| 392 | 9 | $paramType = $method === 'POST' ? RequestOptions::FORM_PARAMS : RequestOptions::QUERY; |
|
| 393 | 9 | if (key_exists(RequestOptions::JSON, $options)) { |
|
| 394 | $json = $options[RequestOptions::JSON]; |
||
| 395 | unset($options[RequestOptions::JSON]); |
||
| 396 | $data[RequestOptions::JSON] = $json; |
||
| 397 | $paramType = RequestOptions::QUERY; |
||
| 398 | } |
||
| 399 | 9 | $data[$paramType] = $this->getAuthParams($options); |
|
| 400 | 9 | $this->requestData = $data; |
|
| 401 | 9 | $data[RequestOptions::ON_STATS] = function (TransferStats $stats) { |
|
| 402 | 9 | $this->lastUrl = (string) $stats->getEffectiveUri(); |
|
| 403 | 9 | $this->request = $stats->getRequest(); |
|
| 404 | 9 | $this->transferTime = $stats->getTransferTime(); |
|
| 405 | 9 | }; |
|
| 406 | 9 | if ($this->log) { |
|
| 407 | $data['handler'] = $this->handlerStack; |
||
| 408 | } |
||
| 409 | 9 | if ($this->apiVersion) { |
|
| 410 | 9 | $uri = $this->apiVersion . $uri; |
|
| 411 | } |
||
| 412 | 9 | $promise = $this->getHttpClient()->requestAsync($method, $uri, $data); |
|
| 413 | 9 | $promise->then( |
|
| 414 | 9 | function (ResponseInterface $response) { |
|
| 415 | 9 | $this->response = $response; |
|
| 416 | 9 | call_user_func($this->completeCallBack); |
|
| 417 | 9 | }, |
|
| 418 | 9 | function ($e) { |
|
| 419 | if ($e instanceof BadResponseException) { |
||
| 420 | $this->request = $e->getRequest(); |
||
| 421 | $this->response = $e->getResponse(); |
||
| 422 | } elseif ($e instanceof RequestException) { |
||
| 423 | $this->request = $e->getRequest(); |
||
| 424 | if ($e->hasResponse()) { |
||
| 425 | $this->response = $e->getResponse(); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | throw $e; |
||
| 429 | 9 | } |
|
| 430 | ); |
||
| 431 | 9 | $promise->wait(); |
|
| 432 | |||
| 433 | 9 | return $this; |
|
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * 获取最后一次请求的 URL |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 1 | public function getLastUrl() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * 获取请求时间 |
||
| 448 | * |
||
| 449 | * @return double |
||
| 450 | */ |
||
| 451 | 1 | public function getTransferTime() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * 设置日志 |
||
| 458 | * |
||
| 459 | * @param LoggerInterface $logger |
||
| 460 | * @param mixed $format |
||
| 461 | */ |
||
| 462 | public function setLogger(LoggerInterface $logger, $format = null) |
||
| 463 | { |
||
| 464 | $this->log = true; |
||
| 465 | $this->handlerStack = HandlerStack::create(); |
||
| 466 | if (is_array($format)) { |
||
| 467 | foreach ($format as $item) { |
||
| 468 | $this->handlerStack->unshift($this->createGuzzleLoggingMiddleware($logger, $item)); |
||
| 469 | } |
||
| 470 | } else { |
||
| 471 | $this->handlerStack->push($this->createGuzzleLoggingMiddleware($logger, $format)); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * 创建一个日志中间件 |
||
| 477 | * |
||
| 478 | * @param $logger |
||
| 479 | * @param $Format |
||
| 480 | * |
||
| 481 | * @return callable |
||
| 482 | */ |
||
| 483 | private function createGuzzleLoggingMiddleware($logger, $Format) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * 将所有请求参数转为字符串 |
||
| 490 | * |
||
| 491 | * @param array $param |
||
| 492 | * |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function standardizeParam($param) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * 获取 auth param |
||
| 508 | * |
||
| 509 | * @param array $options |
||
| 510 | * |
||
| 511 | * @return mixed |
||
| 512 | */ |
||
| 513 | 9 | private function getAuthParams($options) |
|
| 514 | { |
||
| 515 | 9 | $this->validateApiOptions(); |
|
| 516 | |||
| 517 | 9 | if (!$this->authenticator instanceof Authenticator) { |
|
| 518 | 9 | $this->authenticator = new Authenticator($this->apiKey, $this->apiSecret); |
|
| 519 | } |
||
| 520 | |||
| 521 | 9 | $options = $this->standardizeParam($options); |
|
| 522 | |||
| 523 | 9 | return $this->authenticator->getAuthParams($options); |
|
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * 验证 api 必须的是否已经被初始化 |
||
| 528 | * |
||
| 529 | * @throws ApiClientInitException |
||
| 530 | */ |
||
| 531 | 9 | private function validateApiOptions() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * 替换 url 中的变量 |
||
| 543 | * |
||
| 544 | * @param string $uri |
||
| 545 | * @param array $optionals |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | protected function compileRoute($uri, $optionals) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * 去掉 uri 中已存在的变量 |
||
| 558 | * |
||
| 559 | * @param string $uri |
||
| 560 | * @param array $optionals |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | 9 | protected function extractOptionalParameters($uri, $optionals) |
|
| 570 | } |
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..