Conditions | 9 |
Paths | 23 |
Total Lines | 66 |
Code Lines | 48 |
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 |
||
43 | public function sendRequest( |
||
44 | string $method, |
||
45 | string $url, |
||
46 | array $options = [], |
||
47 | bool $expectJson = true |
||
48 | ): array { |
||
49 | $result = []; |
||
50 | |||
51 | try { |
||
52 | $response = $this->client->send( |
||
53 | $this->client->createRequest($method, $url, $options) |
||
54 | ); |
||
55 | if ($expectJson) { |
||
56 | $result = $response->json(); |
||
57 | } else { |
||
58 | $result = [ 'body' => $response->getBody() ]; |
||
59 | } |
||
60 | } catch (BadResponseException $e) { |
||
61 | if ($e->getCode() == 400) { |
||
62 | throw new Exception\InvalidData( |
||
63 | 'Data validation failed', |
||
64 | 400, |
||
65 | $e, |
||
66 | $e->getResponse()->json() |
||
67 | ); |
||
68 | } elseif ($e->getCode() == 403) { |
||
69 | throw new Exception\InvalidApiKey( |
||
70 | 'Access forbidden. Invalid API key.', |
||
71 | 403, |
||
72 | $e, |
||
73 | $e->getResponse()->getBody() |
||
74 | ); |
||
75 | } elseif ($e->getCode() == 404) { |
||
76 | throw new Exception\NotFound( |
||
77 | 'Requested URL was not found.', |
||
78 | 404, |
||
79 | $e, |
||
80 | $e->getResponse()->getBody() |
||
81 | ); |
||
82 | } elseif ($e->getCode() == 500) { |
||
83 | throw new Exception\ServerError( |
||
84 | 'Error occurred on server side while handling request', |
||
85 | 500, |
||
86 | $e, |
||
87 | $e->getResponse()->getBody() |
||
88 | ); |
||
89 | } elseif ($e->getCode() == 504) { |
||
90 | throw new Exception\Timeout( |
||
91 | 'Request timeout', |
||
92 | 504, |
||
93 | $e, |
||
94 | $e->getResponse()->getBody() |
||
95 | ); |
||
96 | } else { |
||
97 | throw new Exception\UnexpectedResponse( |
||
98 | 'Unexpected error occurred', |
||
99 | $e->getCode(), |
||
100 | $e, |
||
101 | $e->getResponse()->getBody() |
||
102 | ); |
||
103 | } |
||
104 | } catch (\Exception $e) { |
||
105 | throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e); |
||
106 | } |
||
107 | |||
108 | return $result; |
||
109 | } |
||
111 |