Conditions | 7 |
Paths | 7 |
Total Lines | 60 |
Code Lines | 33 |
Lines | 24 |
Ratio | 40 % |
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 |
||
51 | public function verifyCallback($args) |
||
52 | { |
||
53 | $helper = $this->facebook->getRedirectLoginHelper(); |
||
54 | |||
55 | try { |
||
56 | $accessToken = $helper->getAccessToken(); |
||
57 | } catch (FacebookResponseException $e) { |
||
58 | // When Graph returns an error |
||
59 | echo 'Graph returned an error: '.$e->getMessage(); |
||
60 | |||
61 | return false; |
||
62 | } catch (FacebookSDKException $e) { |
||
63 | // When validation fails or other local issues |
||
64 | echo 'Facebook SDK returned an error: '.$e->getMessage(); |
||
65 | |||
66 | return false; |
||
67 | } |
||
68 | |||
69 | View Code Duplication | if (!isset($accessToken)) { |
|
70 | if ($helper->getError()) { |
||
71 | header('HTTP/1.0 401 Unauthorized'); |
||
72 | echo 'Error: '.$helper->getError()."\n"; |
||
73 | echo 'Error Code: '.$helper->getErrorCode()."\n"; |
||
74 | echo 'Error Reason: '.$helper->getErrorReason()."\n"; |
||
75 | echo 'Error Description: '.$helper->getErrorDescription()."\n"; |
||
76 | } else { |
||
77 | header('HTTP/1.0 400 Bad Request'); |
||
78 | echo 'Bad request'; |
||
79 | } |
||
80 | |||
81 | return false; |
||
82 | } |
||
83 | |||
84 | // The OAuth 2.0 client handler helps us manage access tokens |
||
85 | $oAuth2Client = $this->facebook->getOAuth2Client(); |
||
86 | |||
87 | // Get the access token metadata from /debug_token |
||
88 | $tokenMetadata = $oAuth2Client->debugToken($accessToken); |
||
89 | |||
90 | // Validation (these will throw FacebookSDKException's when they fail) |
||
91 | $tokenMetadata->validateAppId($this->appId); // Replace {app-id} with your app id |
||
92 | // If you know the user ID this access token belongs to, you can validate it here |
||
93 | //$tokenMetadata->validateUserId('123'); |
||
94 | $tokenMetadata->validateExpiration(); |
||
95 | |||
96 | View Code Duplication | if (!$accessToken->isLongLived()) { |
|
97 | // Exchanges a short-lived access token for a long-lived one |
||
98 | try { |
||
99 | $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); |
||
100 | } catch (FacebookSDKException $e) { |
||
101 | echo '<p>Error getting long-lived access token: '.$helper->getMessage()."</p>\n\n"; |
||
102 | |||
103 | return false; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $_SESSION['fb_access_token'] = (string) $accessToken; |
||
108 | |||
109 | return true; |
||
110 | } |
||
111 | |||
151 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: