Conditions | 9 |
Paths | 5 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | public static function parseResponse(Response $response) |
||
45 | { |
||
46 | $jsonData = \GuzzleHttp\json_decode($response->getBody(), true); |
||
47 | if ($jsonData && 0 == $jsonData['retcode']) { |
||
48 | //群成员的vip信息 |
||
49 | $vipInfos = (new Collection($jsonData['result']['vipinfo']))->combine('u', function ($entity) { |
||
50 | return $entity; |
||
51 | })->toArray(); |
||
52 | //群成员的名片信息, 如果全部成员都没有设置群名片则会不存在这个字段 |
||
53 | $cards = isset($jsonData['result']['cards']) ? (new Collection($jsonData['result']['cards'])) |
||
54 | ->combine('muin', 'card') |
||
55 | ->toArray() : []; |
||
56 | //群成员的简要信息 |
||
57 | $flags = (new Collection($jsonData['result']['ginfo']['members']))->combine('muin', 'mflag') |
||
58 | ->toArray(); |
||
59 | //群基本详细信息 |
||
60 | $groupData = $jsonData['result']['ginfo']; |
||
61 | $groupDetailData = [ |
||
62 | 'gid' => $groupData['gid'], |
||
63 | 'name' => $groupData['name'], |
||
64 | 'code' => $groupData['code'], |
||
65 | 'owner' => $groupData['owner'], |
||
66 | 'level' => $groupData['level'], |
||
67 | 'createTime' => $groupData['createtime'], |
||
68 | 'flag' => $groupData['flag'], |
||
69 | 'memo' => $groupData['memo'], |
||
70 | 'members' => null, |
||
71 | ]; |
||
72 | $members = []; |
||
73 | foreach ($jsonData['result']['minfo'] as $memberData) { |
||
74 | $uin = $memberData['uin']; |
||
75 | $member = EntityFactory::createEntity(GroupMember::class, [ |
||
76 | 'flag' => isset($flags[$uin]) ? $flags[$uin] : null, |
||
77 | 'nick' => $memberData['nick'], |
||
78 | 'province' => $memberData['province'], |
||
79 | 'gender' => $memberData['gender'], |
||
80 | 'uin' => $uin, |
||
81 | 'country' => $memberData['country'], |
||
82 | 'city' => $memberData['city'], |
||
83 | 'card' => isset($cards[$uin]) ? $cards[$uin] : null, |
||
84 | 'isVip' => isset($vipInfos[$uin]) ? 1 == $vipInfos[$uin]['is_vip'] : false, |
||
85 | 'vipLevel' => isset($vipInfos[$uin]) ? $vipInfos[$uin]['vip_level'] : 0, |
||
86 | ]); |
||
87 | $members[] = $member; |
||
88 | } |
||
89 | $groupDetailData['members'] = new EntityCollection($members); |
||
90 | |||
91 | return EntityFactory::createEntity(GroupDetail::class, $groupDetailData); |
||
92 | } |
||
93 | throw new ResponseException($jsonData['retcode'], $response); |
||
94 | } |
||
95 | } |
||
96 |