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 |
||
47 | class Client |
||
48 | { |
||
49 | /** |
||
50 | * @var Credential |
||
51 | */ |
||
52 | protected $credential; |
||
53 | |||
54 | /** |
||
55 | * 客户端id(固定值) |
||
56 | * @var int |
||
57 | */ |
||
58 | protected static $clientId = 53999199; |
||
59 | |||
60 | /** |
||
61 | * 获取ptwebqq的地址 |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $certificationUrl; |
||
65 | |||
66 | /** |
||
67 | * @var HttpClient |
||
68 | */ |
||
69 | protected $httpClient; |
||
70 | |||
71 | /** |
||
72 | * @var CookieJar |
||
73 | */ |
||
74 | protected $cookies; |
||
75 | |||
76 | public function __construct(Credential $credential = null, HttpClient $httpClient = null) |
||
88 | |||
89 | /** |
||
90 | * 开启登录流程自行获取凭证 |
||
91 | * @param string $loginQRImage 二维码图片位置 |
||
92 | * @return Credential |
||
93 | */ |
||
94 | public function login($loginQRImage) |
||
117 | |||
118 | /** |
||
119 | * 创建登录所需的二维码 |
||
120 | * @param string $loginQRImage |
||
121 | * @return string |
||
122 | */ |
||
123 | protected function makeQrCodeImage($loginQRImage) |
||
134 | |||
135 | /** |
||
136 | * 验证二维码状态 |
||
137 | * @param int $ptQrToken qr token |
||
138 | * @return int |
||
139 | */ |
||
140 | protected function verifyQrCodeStatus($ptQrToken) |
||
161 | |||
162 | /** |
||
163 | * 获取ptwebqq的参数值 |
||
164 | * @param string $certificationUrl |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function getPtWebQQ($certificationUrl) |
||
179 | |||
180 | /** |
||
181 | * @param string $ptWebQQ |
||
182 | * @return string |
||
183 | */ |
||
184 | protected function getVfWebQQ($ptWebQQ) |
||
190 | |||
191 | /** |
||
192 | * 获取pessionid和uin |
||
193 | * @param string $ptWebQQ |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function getUinAndPSessionId($ptWebQQ) |
||
207 | |||
208 | /** |
||
209 | * @param Credential $credential |
||
210 | */ |
||
211 | public function setCredential(Credential $credential) |
||
216 | |||
217 | /** |
||
218 | * @return Credential |
||
219 | */ |
||
220 | public function getCredential() |
||
227 | |||
228 | /** |
||
229 | * @return CookieJar |
||
230 | */ |
||
231 | public function getCookies() |
||
235 | |||
236 | /** |
||
237 | * @param HttpClient $httpClient |
||
238 | */ |
||
239 | public function setHttpClient($httpClient) |
||
243 | |||
244 | /** |
||
245 | * @return HttpClient |
||
246 | */ |
||
247 | public function getHttpClient() |
||
251 | |||
252 | /** |
||
253 | * 获取所有的群 |
||
254 | * @return EntityCollection |
||
255 | */ |
||
256 | public function getGroups() |
||
262 | |||
263 | /** |
||
264 | * 获取群详细信息 |
||
265 | * @param Group $group |
||
266 | * @return GroupDetail |
||
267 | */ |
||
268 | public function getGroupDetail(Group $group) |
||
274 | |||
275 | /** |
||
276 | * 获取所有讨论组 |
||
277 | * @return EntityCollection |
||
278 | */ |
||
279 | public function getDiscusses() |
||
285 | |||
286 | /** |
||
287 | * 获取讨论组详情 |
||
288 | * @param Discuss $discuss |
||
289 | * @return DiscussDetail |
||
290 | */ |
||
291 | public function getDiscussDetail(Discuss $discuss) |
||
297 | |||
298 | /** |
||
299 | * 获取所有的好友 |
||
300 | * @return EntityCollection |
||
301 | */ |
||
302 | public function getFriends() |
||
308 | |||
309 | /** |
||
310 | * 获取好友的详细信息 |
||
311 | * @param Friend $friend |
||
312 | * @return Profile |
||
313 | */ |
||
314 | public function getFriendDetail(Friend $friend) |
||
320 | |||
321 | /** |
||
322 | * 获取好友的QQ号 |
||
323 | * @param Friend $friend |
||
324 | * @return int |
||
325 | */ |
||
326 | public function getFriendQQ(Friend $friend) |
||
334 | |||
335 | /** |
||
336 | * 获取好友的个性签名 |
||
337 | * @param Friend $friend |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getFriendLnick(Friend $friend) |
||
346 | |||
347 | /** |
||
348 | * 获取好友在线状态 |
||
349 | * @return EntityCollection |
||
350 | */ |
||
351 | public function getFriendsOnlineStatus() |
||
357 | |||
358 | /** |
||
359 | * 获取最近的会话 |
||
360 | * @return EntityCollection |
||
361 | */ |
||
362 | public function getRecentList() |
||
368 | |||
369 | /** |
||
370 | * 获取当前登录用户信息 |
||
371 | * @return Profile |
||
372 | */ |
||
373 | public function getCurrentUserInfo() |
||
379 | |||
380 | /** |
||
381 | * 轮询消息, |
||
382 | * client并不会组装信息,只是将接口返回的信息完整抽象并返回 |
||
383 | * 如果需要查询信息对应的数据,如发送人、发送群,请自行获取 |
||
384 | * @return ResponseMessage[] |
||
385 | */ |
||
386 | public function pollMessages() |
||
392 | |||
393 | /** |
||
394 | * 发送消息,包括好友消息,群消息,讨论组消息 |
||
395 | * @param RequestMessage $message |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function sendMessage(RequestMessage $message) |
||
410 | |||
411 | /** |
||
412 | * @param RequestInterface $request |
||
413 | * @return ResponseInterface |
||
414 | */ |
||
415 | protected function sendRequest(RequestInterface $request) |
||
436 | } |
||
437 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.