| Conditions | 6 |
| Paths | 8 |
| Total Lines | 57 |
| Code Lines | 28 |
| 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 function get(AccessToken $accessToken, $requestUri, array $requestHeaders = []) |
||
| 47 | { |
||
| 48 | $refreshedToken = false; |
||
| 49 | if ($accessToken->isExpired()) { |
||
| 50 | error_log('access_token expired'); |
||
| 51 | // access_token is expired, try to refresh it |
||
| 52 | if (is_null($accessToken->getRefreshToken())) { |
||
| 53 | error_log('no refresh_token available, delete access_token'); |
||
| 54 | // we do not have a refresh_token, delete this access token, it |
||
| 55 | // is useless now... |
||
| 56 | call_user_func($this->deleteToken, $accessToken); |
||
| 57 | |||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | error_log('attempting to refresh access_token'); |
||
| 62 | // deal with possibly revoked authorization! XXX |
||
| 63 | try { |
||
| 64 | $accessToken = $this->oauthClient->refreshAccessToken($accessToken); |
||
| 65 | } catch (OAuthServerException $e) { |
||
| 66 | error_log(sprintf('unable to use refresh_token %s', $e->getMessage())); |
||
| 67 | |||
| 68 | // delete the access_token, the refresh_token could not be used |
||
| 69 | |||
| 70 | call_user_func($this->deleteToken, $accessToken); |
||
| 71 | |||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | // maybe delete old accesstoken here? XXX |
||
| 76 | error_log('access_token refreshed'); |
||
| 77 | $refreshedToken = true; |
||
| 78 | } |
||
| 79 | |||
| 80 | // add Authorization header to the request headers |
||
| 81 | $requestHeaders['Authorization'] = sprintf('Bearer %s', $accessToken->getToken()); |
||
| 82 | |||
| 83 | $response = $this->oauthClient->getHttpClient()->get($requestUri, $requestHeaders); |
||
| 84 | if (401 === $response->getStatusCode()) { |
||
| 85 | error_log('access_token appears to be invalid, delete access_token'); |
||
| 86 | // this indicates an invalid access_token |
||
| 87 | call_user_func($this->deleteToken, $accessToken); |
||
| 88 | |||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | error_log('access_token was valid, call succeeded'); |
||
| 93 | |||
| 94 | if ($refreshedToken) { |
||
| 95 | error_log('access_token was refreshed, so store it now for future use'); |
||
| 96 | // if we refreshed the token, and it was successful, i.e. not a 401, |
||
| 97 | // update the stored AccessToken |
||
| 98 | call_user_func($this->setToken, $accessToken); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $response; |
||
| 102 | } |
||
| 103 | |||
| 108 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.