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() |
|
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 = []) |
|
437 | |||
438 | /** |
||
439 | * 获取最后一次请求的 URL |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 1 | public function getLastUrl() |
|
447 | |||
448 | /** |
||
449 | * 获取请求时间 |
||
450 | * |
||
451 | * @return double |
||
452 | */ |
||
453 | 1 | public function getTransferTime() |
|
457 | |||
458 | /** |
||
459 | * 设置日志 |
||
460 | * |
||
461 | * @param LoggerInterface $logger |
||
462 | * @param mixed $format |
||
463 | */ |
||
464 | public function setLogger(LoggerInterface $logger, $format = null) |
||
476 | |||
477 | /** |
||
478 | * 创建一个日志中间件 |
||
479 | * |
||
480 | * @param $logger |
||
481 | * @param $Format |
||
482 | * |
||
483 | * @return callable |
||
484 | */ |
||
485 | private function createGuzzleLoggingMiddleware($logger, $Format) |
||
489 | |||
490 | /** |
||
491 | * 将所有请求参数转为字符串 |
||
492 | * |
||
493 | * @param array $param |
||
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | 10 | public function standardizeParam($param) |
|
507 | |||
508 | /** |
||
509 | * 获取 auth param |
||
510 | * |
||
511 | * @param array $options |
||
512 | * |
||
513 | * @return mixed |
||
514 | */ |
||
515 | 9 | private function getAuthParams($options) |
|
527 | |||
528 | /** |
||
529 | * 验证 api 必须的是否已经被初始化 |
||
530 | * |
||
531 | * @throws ApiClientInitException |
||
532 | */ |
||
533 | 9 | private function validateApiOptions() |
|
542 | |||
543 | /** |
||
544 | * 替换 url 中的变量 |
||
545 | * |
||
546 | * @param string $uri |
||
547 | * @param array $optionals |
||
548 | * |
||
549 | * @return string |
||
550 | */ |
||
551 | protected function compileRoute($uri, $optionals) |
||
557 | |||
558 | /** |
||
559 | * 去掉 uri 中已存在的变量 |
||
560 | * |
||
561 | * @param string $uri |
||
562 | * @param array $optionals |
||
563 | * |
||
564 | * @return array |
||
565 | */ |
||
566 | 9 | protected function extractOptionalParameters($uri, $optionals) |
|
572 | } |
||
573 |
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..