Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Client |
||
30 | { |
||
31 | /** |
||
32 | * @var Credential |
||
33 | */ |
||
34 | protected $credential; |
||
35 | |||
36 | /** |
||
37 | * 客户端id(固定值). |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected static $clientId = 53999199; |
||
42 | |||
43 | /** |
||
44 | * 获取ptwebqq的地址 |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $certificationUrl; |
||
49 | |||
50 | /** |
||
51 | * @var HttpClient |
||
52 | */ |
||
53 | protected $httpClient; |
||
54 | |||
55 | /** |
||
56 | * @var DispatcherInterface |
||
57 | */ |
||
58 | protected $eventDispatcher; |
||
59 | |||
60 | /** |
||
61 | * @var CookieJar |
||
62 | */ |
||
63 | protected $cookies; |
||
64 | |||
65 | /** |
||
66 | * @var MessageHandler |
||
67 | */ |
||
68 | protected $messageHandler; |
||
69 | |||
70 | public function __construct( |
||
90 | |||
91 | /** |
||
92 | * 开启登录流程自行获取凭证 |
||
93 | * |
||
94 | * @param string $loginQRImage 二维码图片位置 |
||
95 | * |
||
96 | * @return Credential |
||
97 | */ |
||
98 | public function login($loginQRImage) |
||
124 | |||
125 | /** |
||
126 | * 创建登录所需的二维码 |
||
127 | * |
||
128 | * @param string $loginQRImage |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function makeQrCodeImage($loginQRImage) |
||
143 | |||
144 | /** |
||
145 | * 验证二维码状态 |
||
146 | * |
||
147 | * @param int $ptQrToken qr token |
||
148 | * |
||
149 | * @return int |
||
150 | */ |
||
151 | protected function verifyQrCodeStatus($ptQrToken) |
||
173 | |||
174 | /** |
||
175 | * 获取ptwebqq的参数值 |
||
176 | * |
||
177 | * @param string $certificationUrl |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getPtWebQQ($certificationUrl) |
||
193 | |||
194 | /** |
||
195 | * @param string $ptWebQQ |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getVfWebQQ($ptWebQQ) |
||
206 | |||
207 | /** |
||
208 | * 获取pessionid和uin. |
||
209 | * |
||
210 | * @param string $ptWebQQ |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | protected function getUinAndPSessionId($ptWebQQ) |
||
226 | |||
227 | /** |
||
228 | * @param Credential $credential |
||
229 | */ |
||
230 | public function setCredential(Credential $credential) |
||
235 | |||
236 | /** |
||
237 | * @return Credential |
||
238 | */ |
||
239 | public function getCredential() |
||
247 | |||
248 | /** |
||
249 | * @return CookieJar |
||
250 | */ |
||
251 | public function getCookies() |
||
255 | |||
256 | /** |
||
257 | * @param HttpClient $httpClient |
||
258 | */ |
||
259 | public function setHttpClient($httpClient) |
||
263 | |||
264 | /** |
||
265 | * @return HttpClient |
||
266 | */ |
||
267 | public function getHttpClient() |
||
271 | |||
272 | /** |
||
273 | * @param DispatcherInterface $eventDispatcher |
||
274 | */ |
||
275 | public function setEventDispatcher($eventDispatcher) |
||
279 | |||
280 | /** |
||
281 | * @return DispatcherInterface |
||
282 | */ |
||
283 | public function getEventDispatcher() |
||
287 | |||
288 | /** |
||
289 | * 获取所有的群. |
||
290 | * |
||
291 | * @return EntityCollection |
||
292 | */ |
||
293 | public function getGroups() |
||
300 | |||
301 | /** |
||
302 | * 获取群详细信息. |
||
303 | * |
||
304 | * @param Entity\Group $group |
||
305 | * |
||
306 | * @return Entity\GroupDetail |
||
307 | */ |
||
308 | public function getGroupDetail(Entity\Group $group) |
||
315 | |||
316 | /** |
||
317 | * 获取所有讨论组. |
||
318 | * |
||
319 | * @return EntityCollection |
||
320 | */ |
||
321 | public function getDiscusses() |
||
328 | |||
329 | /** |
||
330 | * 获取讨论组详情. |
||
331 | * |
||
332 | * @param Entity\Discuss $discuss |
||
333 | * |
||
334 | * @return Entity\DiscussDetail |
||
335 | */ |
||
336 | public function getDiscussDetail(Entity\Discuss $discuss) |
||
343 | |||
344 | /** |
||
345 | * 获取所有的好友. |
||
346 | * |
||
347 | * @return EntityCollection|Entity\Friend[] |
||
348 | */ |
||
349 | public function getFriends() |
||
356 | |||
357 | /** |
||
358 | * 获取好友的详细信息. |
||
359 | * |
||
360 | * @param Entity\Friend $friend |
||
361 | * |
||
362 | * @return Entity\Profile |
||
363 | */ |
||
364 | public function getFriendDetail(Entity\Friend $friend) |
||
371 | |||
372 | /** |
||
373 | * 获取好友的QQ号. |
||
374 | * |
||
375 | * @param Entity\Friend $friend |
||
376 | * |
||
377 | * @return int |
||
378 | * @deprecated 此接口 Smartqq 官方已经不再提供 |
||
379 | */ |
||
380 | public function getFriendQQ(Entity\Friend $friend) |
||
391 | |||
392 | /** |
||
393 | * 获取好友的个性签名. |
||
394 | * |
||
395 | * @param Entity\Friend $friend |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getFriendLnick(Entity\Friend $friend) |
||
406 | |||
407 | /** |
||
408 | * 获取好友在线状态 |
||
409 | * |
||
410 | * @return EntityCollection |
||
411 | */ |
||
412 | public function getFriendsOnlineStatus() |
||
419 | |||
420 | /** |
||
421 | * 获取最近的会话. |
||
422 | * |
||
423 | * @return EntityCollection |
||
424 | */ |
||
425 | public function getRecentList() |
||
432 | |||
433 | /** |
||
434 | * 获取当前登录用户信息. |
||
435 | * |
||
436 | * @return Entity\Profile |
||
437 | */ |
||
438 | public function getCurrentUserInfo() |
||
445 | |||
446 | /** |
||
447 | * 轮询消息, |
||
448 | * client并不会组装信息,只是将接口返回的信息完整抽象并返回 |
||
449 | * 如果需要查询信息对应的数据,如发送人、发送群,请自行获取. |
||
450 | * |
||
451 | * @return ResponseMessage[] |
||
452 | */ |
||
453 | public function pollMessages() |
||
460 | |||
461 | /** |
||
462 | * 获取 Message handler. |
||
463 | * |
||
464 | * @return MessageHandler |
||
465 | */ |
||
466 | public function getMessageHandler() |
||
473 | |||
474 | /** |
||
475 | * 发送消息,包括好友消息,群消息,讨论组消息. |
||
476 | * |
||
477 | * @param RequestMessage $message |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function sendMessage(RequestMessage $message) |
||
494 | |||
495 | |||
496 | /** |
||
497 | * @param Request\RequestInterface $request |
||
498 | * |
||
499 | * @return HttpResponse |
||
500 | */ |
||
501 | protected function sendRequest(Request\RequestInterface $request) |
||
526 | } |
||
527 |
If you suppress an error, we recommend checking for the error condition explicitly: