| Conditions | 10 |
| Paths | 34 |
| Total Lines | 53 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
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 function makeApiCall(RequestInterface $request) |
||
| 45 | { |
||
| 46 | $response = null; |
||
| 47 | |||
| 48 | if ($this->authenticator->getAccessToken() !== null) { |
||
| 49 | $authenticatedRequest = new OAuthDecorator($request); |
||
| 50 | $authenticatedRequest->setAccessToken($this->authenticator->getAccessToken()); |
||
| 51 | $authenticatedRequest->addAuthentication(); |
||
| 52 | |||
| 53 | $response = $this->client->makeCall( |
||
| 54 | $authenticatedRequest->getFullUrl(), |
||
| 55 | $this->add_default_headers($authenticatedRequest->getHeaders()), |
||
| 56 | $authenticatedRequest->getOptions() |
||
| 57 | ); |
||
| 58 | |||
| 59 | if ($response['status'] == 200) { |
||
| 60 | $this->authenticationRetryLimit = 0; |
||
|
|
|||
| 61 | |||
| 62 | try { |
||
| 63 | return new Response($response['body'], $response['headers']); |
||
| 64 | } catch (ResponseException $e) { |
||
| 65 | throw new ClientException($e->getMessage(), $e->getCode(), $e); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($response === null || $response['status'] == 401) { |
||
| 71 | |||
| 72 | $this->authenticationRetryLimit++; |
||
| 73 | |||
| 74 | if ($this->authenticationRetryLimit > self::MAX_RETRY_LIMIT) { |
||
| 75 | throw new AccessDeniedException('Authentication retry limit reached.'); |
||
| 76 | } |
||
| 77 | |||
| 78 | try { |
||
| 79 | $this->authenticator->setBaseUrl($request->getBaseUrl()); |
||
| 80 | if ($this->authenticator->getAccessToken() !== null) { |
||
| 81 | $this->authenticator->refreshAccessToken(); |
||
| 82 | } else { |
||
| 83 | $this->authenticator->getAuthenticationTokens(); |
||
| 84 | } |
||
| 85 | |||
| 86 | // Reexecute event |
||
| 87 | return $this->makeApiCall($request); |
||
| 88 | } catch (AccessDeniedException $e) { |
||
| 89 | throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e); |
||
| 90 | } catch (AuthenticationException $e) { |
||
| 91 | throw new AccessDeniedException('Could not authenticate against API.', $e->getCode(), $e); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | throw new ClientException(sprintf('The server returned an error with status %s.', $response['status'])); |
||
| 96 | } |
||
| 97 | |||
| 117 |
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: