| Conditions | 10 |
| Paths | 28 |
| Total Lines | 41 |
| 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 |
||
| 23 | public static function sendRequest( |
||
| 24 | $actionUrl, |
||
| 25 | array $data = [], |
||
| 26 | $method = 'GET', |
||
| 27 | AccessToken $token = null, |
||
| 28 | $baseUrl = '' |
||
| 29 | ) { |
||
| 30 | if (empty($baseUrl)) { |
||
| 31 | $baseUrl = LPTrackerBase::DEFAULT_ADDRESS; |
||
| 32 | } |
||
| 33 | $url = $baseUrl . $actionUrl; |
||
| 34 | $curl = new cURL(); |
||
| 35 | $request = $curl->newRequest($method, $url, $data, Request::ENCODING_JSON); |
||
| 36 | $request->setHeader('Content-Type', 'application/json'); |
||
| 37 | if ($token instanceof AccessToken) { |
||
| 38 | $request->setHeader('token', $token->getValue()); |
||
| 39 | } |
||
| 40 | $response = $request->send(); |
||
| 41 | if ($response === false) { |
||
| 42 | throw new LPTrackerServerException('Can`t get response from server'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $body = json_decode($response->body, true); |
||
| 46 | if ($body === false) { |
||
| 47 | throw new LPTrackerServerException('Can`t decode response'); |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!empty($body['errors'])) { |
||
| 51 | if (!empty($body['errors'][0]['message'])) { |
||
| 52 | throw new LPTrackerResponseException($body['errors'][0]['message']); |
||
| 53 | } |
||
| 54 | |||
| 55 | throw new LPTrackerResponseException($body['errors'][0]); |
||
| 56 | } |
||
| 57 | |||
| 58 | if (empty($body['status']) || $body['status'] !== 'success') { |
||
| 59 | throw new LPTrackerResponseException('Unknown response error'); |
||
| 60 | } |
||
| 61 | |||
| 62 | return isset($body['result']) ? $body['result'] : null; |
||
| 63 | } |
||
| 64 | } |
||
| 65 |