Conditions | 9 |
Paths | 26 |
Total Lines | 54 |
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 |
||
46 | public static function parseResponse(Response $response) |
||
47 | { |
||
48 | $jsonData = \GuzzleHttp\json_decode($response->getBody(), true); |
||
49 | //有时候获取好友接口retcode=100003时也可以获取数据,但数据不完整故当做无效返回 |
||
50 | if ($jsonData && 0 == $jsonData['retcode']) { |
||
51 | //好友基本信息 |
||
52 | $friendDatas = (new Collection($jsonData['result']['friends']))->combine('uin', function ($entity) { |
||
53 | return $entity; |
||
54 | })->toArray(); |
||
55 | //markNames |
||
56 | $markNames = (new Collection($jsonData['result']['marknames']))->combine('uin', function ($entity) { |
||
57 | return $entity; |
||
58 | })->toArray(); |
||
59 | //分类 |
||
60 | $categories = (new Collection($jsonData['result']['categories']))->combine('index', function ($entity) { |
||
61 | return $entity; |
||
62 | })->toArray(); |
||
63 | //vip信息 |
||
64 | $vipInfos = (new Collection($jsonData['result']['vipinfo']))->combine('u', function ($entity) { |
||
65 | return $entity; |
||
66 | })->toArray(); |
||
67 | $friends = []; |
||
68 | foreach ($jsonData['result']['info'] as $friendData) { |
||
69 | $uin = $friendData['uin']; |
||
70 | $friend = [ |
||
71 | 'uin' => $friendData['uin'], |
||
72 | 'flag' => $friendData['flag'], |
||
73 | 'face' => $friendData['face'], |
||
74 | 'nick' => $friendData['nick'], |
||
75 | 'markName' => isset($markNames[$uin]) ? $markNames[$uin]['markname'] : null, |
||
76 | 'isVip' => isset($vipInfos[$uin]) ? 1 == $vipInfos[$uin]['is_vip'] : false, |
||
77 | 'vipLevel' => isset($vipInfos[$uin]) ? $vipInfos[$uin]['vip_level'] : 0, |
||
78 | ]; |
||
79 | $category = null; |
||
80 | if (isset($friendDatas[$uin])) { |
||
81 | $categoryIndex = $friendDatas[$uin]['categories']; |
||
82 | if (0 == $categoryIndex) { |
||
83 | $category = Category::createMyFriendCategory(); |
||
84 | } else { |
||
85 | $category = new Category( |
||
86 | $categories[$categoryIndex]['name'], |
||
87 | $categories[$categoryIndex]['index'], |
||
88 | $categories[$categoryIndex]['sort'] |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 | $friend['category'] = $category; |
||
93 | $friends[] = EntityFactory::createEntity(Friend::class, $friend); |
||
94 | } |
||
95 | |||
96 | return new EntityCollection($friends); |
||
97 | } |
||
98 | throw new ResponseException($jsonData['retcode'], $response); |
||
99 | } |
||
100 | } |
||
101 |