Conditions | 8 |
Paths | 8 |
Total Lines | 57 |
Code Lines | 43 |
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 |
||
98 | protected function sendRequest( |
||
99 | string $method, |
||
100 | string $url, |
||
101 | array $options = [] |
||
102 | ): ResponseInterface { |
||
103 | try { |
||
104 | $response = $this->client->request($method, $url, $options); |
||
105 | |||
106 | return $response; |
||
107 | } catch (BadResponseException $e) { |
||
108 | if ($e->getCode() == 400) { |
||
109 | throw new Exception\InvalidData( |
||
110 | 'Data validation failed', |
||
111 | 400, |
||
112 | $e, |
||
113 | json_decode($e->getResponse()->getBody()->getContents(), true) |
||
114 | ); |
||
115 | } elseif ($e->getCode() == 403) { |
||
116 | throw new Exception\InvalidApiKey( |
||
117 | 'Access forbidden. Invalid API key.', |
||
118 | 403, |
||
119 | $e, |
||
120 | $e->getResponse()->getBody() |
||
121 | ); |
||
122 | } elseif ($e->getCode() == 404) { |
||
123 | throw new Exception\NotFound( |
||
124 | 'Requested URL was not found.', |
||
125 | 404, |
||
126 | $e, |
||
127 | $e->getResponse()->getBody() |
||
128 | ); |
||
129 | } elseif ($e->getCode() == 500) { |
||
130 | throw new Exception\ServerError( |
||
131 | 'Error occurred on server side while handling request', |
||
132 | 500, |
||
133 | $e, |
||
134 | $e->getResponse()->getBody() |
||
135 | ); |
||
136 | } elseif ($e->getCode() == 504) { |
||
137 | throw new Exception\Timeout( |
||
138 | 'Request timeout', |
||
139 | 504, |
||
140 | $e, |
||
141 | $e->getResponse()->getBody() |
||
142 | ); |
||
143 | } else { |
||
144 | throw new Exception\UnexpectedResponse( |
||
145 | 'Unexpected error occurred', |
||
146 | $e->getCode(), |
||
147 | $e, |
||
148 | $e->getResponse()->getBody() |
||
149 | ); |
||
150 | } |
||
151 | } catch (\Exception $e) { |
||
152 | throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 |