Conditions | 13 |
Paths | 31 |
Total Lines | 46 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 15.6406 |
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 | 55 | public static function create( |
|
45 | RequestInterface $request, |
||
46 | ResponseInterface $response = null, |
||
47 | \Exception $previous = null |
||
48 | ) { |
||
49 | 55 | if (is_null($response)) { |
|
50 | 2 | return new self('Error completing request', $request, null, $previous); |
|
51 | } |
||
52 | |||
53 | 53 | $level = floor($response->getStatusCode() / 100); |
|
54 | 53 | if ($level == 4) { |
|
55 | 44 | $label = 'Client error response'; |
|
56 | 9 | } elseif ($level == 5) { |
|
57 | 9 | $label = 'Server error response'; |
|
58 | } else { |
||
59 | $label = 'Unsuccessful response'; |
||
60 | } |
||
61 | |||
62 | 53 | $message = $label . ' [url] ' . $request->getUri() |
|
63 | 53 | . ' [status code] ' . $response->getStatusCode() |
|
64 | 53 | . ' [reason phrase] ' . $response->getReasonPhrase(); |
|
65 | |||
66 | 53 | switch ($response->getStatusCode()) { |
|
67 | case 400: |
||
68 | 24 | return new ErrorResponseException($message, $request, $response, $previous); |
|
69 | case 401: |
||
70 | 6 | if (strpos((string)$response->getBody(), 'invalid_token') !== false) { |
|
71 | 3 | return new InvalidTokenException($message, $request, $response, $previous); |
|
72 | } |
||
73 | 3 | return new InvalidClientCredentialsException($message, $request, $response, $previous); |
|
74 | case 404: |
||
75 | 8 | return new NotFoundException($message, $request, $response, $previous); |
|
76 | case 409: |
||
77 | 4 | return new ConcurrentModificationException($message, $request, $response, $previous); |
|
78 | case 500: |
||
79 | 6 | return new InternalServerErrorException($message, $request, $response, $previous); |
|
80 | case 502: |
||
81 | 1 | return new BadGatewayException($message, $request, $response, $previous); |
|
82 | case 503: |
||
83 | 1 | return new ServiceUnavailableException($message, $request, $response, $previous); |
|
84 | 2 | case 504: |
|
85 | 1 | return new GatewayTimeoutException($message, $request, $response, $previous); |
|
86 | } |
||
87 | |||
88 | 2 | return new self($message, $request, $response, $previous); |
|
89 | } |
||
90 | |||
148 |