| Conditions | 10 |
| Paths | 520 |
| Total Lines | 61 |
| 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 |
||
| 18 | public static function getProfile($username) |
||
| 19 | { |
||
| 20 | try { |
||
| 21 | $options = array('http' => array('user_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)')); |
||
| 22 | $context = stream_context_create($options); |
||
| 23 | |||
| 24 | $html = file_get_contents('https://instagram.com/'.$username, false, $context); |
||
| 25 | //Get user ID |
||
| 26 | $subData = substr($html, strpos($html, 'window._sharedData'), strpos($html, '};')); |
||
| 27 | $userID = strstr($subData, '"id":"'); |
||
| 28 | $userID = str_replace('"id":"', '', $userID); |
||
| 29 | $userID = strstr($userID, '"', true); |
||
| 30 | |||
| 31 | //Download user info |
||
| 32 | $jsonData = file_get_contents('https://i.instagram.com/api/v1/users/'.$userID.'/info/', false, $context); |
||
| 33 | $decodedInfo = json_decode($jsonData); |
||
| 34 | |||
| 35 | $data = []; |
||
| 36 | |||
| 37 | if (isset($decodedInfo->user->hd_profile_pic_url_info->url)) { |
||
| 38 | $data['profilePic'] = $decodedInfo->user->hd_profile_pic_url_info->url; |
||
| 39 | } else { |
||
| 40 | $data['profilePic'] = $decodedInfo->user->profile_pic_url; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (isset($decodedInfo->user->username)) { |
||
| 44 | $data['username'] = $decodedInfo->user->username; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (isset($decodedInfo->user->follower_count)) { |
||
| 48 | $data['follower'] = $decodedInfo->user->follower_count; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (isset($decodedInfo->user->following_count)) { |
||
| 52 | $data['following'] = $decodedInfo->user->following_count; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (isset($decodedInfo->user->full_name)) { |
||
| 56 | $data['full_name'] = $decodedInfo->user->full_name; |
||
| 57 | } |
||
| 58 | |||
| 59 | if (isset($decodedInfo->user->is_private)) { |
||
| 60 | $data['isPrivate'] = $decodedInfo->user->is_private; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (isset($decodedInfo->user->is_verified)) { |
||
| 64 | $data['isVerified'] = $decodedInfo->user->is_verified; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (isset($decodedInfo->user->biography)) { |
||
| 68 | $data['bio'] = $decodedInfo->user->biography; |
||
| 69 | } |
||
| 70 | |||
| 71 | return $data; |
||
| 72 | |||
| 73 | } catch (\Exception $e) { |
||
| 74 | ErrorHelper::registerAndReturnMessage($e); |
||
| 75 | } |
||
| 76 | return false; |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 81 |