| Conditions | 13 |
| Paths | 144 |
| Total Lines | 53 |
| 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 |
||
| 80 | public function call(string $method, array $data = [], string $requestType = 'get') |
||
| 81 | { |
||
| 82 | $rawRequest = $data; |
||
| 83 | $startTime = microtime(true); |
||
| 84 | $headers = $this->getHeaders(); |
||
| 85 | try { |
||
| 86 | if (\in_array($requestType, ['get', 'delete'])) { |
||
| 87 | $response = $this->httpClient->request($requestType, $method, ['headers' => $headers]); |
||
| 88 | } else { |
||
| 89 | $data = Json::encode($data); |
||
| 90 | if (Config::$encryptDataTransfer) { |
||
| 91 | $data = $this->encryptData($data); |
||
| 92 | } |
||
| 93 | $response = $this->httpClient->request($requestType, $method, ['headers' => $headers, 'body' => $data]); |
||
| 94 | } |
||
| 95 | $rawResponse = (string) $response->getBody(); |
||
| 96 | $encryptedHeader = $response->getHeader('encrypted'); |
||
| 97 | if ($encryptedHeader && 1 == $response->getHeader('encrypted')[0]) { |
||
| 98 | $rawResponse = $this->decryptData($rawResponse); |
||
| 99 | } |
||
| 100 | $responseBody = Json::decode($rawResponse); |
||
| 101 | } catch (\Throwable $e) { |
||
| 102 | if (Config::$logs) { |
||
| 103 | $this->addLogs($method, $data, '', $e->__toString()); |
||
| 104 | } |
||
| 105 | throw new Exceptions\AppException('An error occurred while communicating with the CRM', 500, $e); |
||
| 106 | } |
||
| 107 | if (\App\Config::$debugApi) { |
||
| 108 | $_SESSION['debugApi'][] = [ |
||
| 109 | 'date' => date('Y-m-d H:i:s', $startTime), |
||
| 110 | 'time' => round(microtime(true) - $startTime, 2), |
||
| 111 | 'method' => $method, |
||
| 112 | 'requestType' => strtoupper($requestType), |
||
| 113 | 'rawRequest' => [$headers, $rawRequest], |
||
| 114 | 'rawResponse' => $rawResponse, |
||
| 115 | 'response' => $responseBody, |
||
| 116 | 'trace' => Debug::getBacktrace() |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | if (Config::$logs) { |
||
| 120 | $this->addLogs($method, $data, $response, $rawResponse); |
||
| 121 | } |
||
| 122 | if (empty($responseBody) || 200 !== $response->getStatusCode()) { |
||
| 123 | throw new Exceptions\AppException('API returned an error: ' . $response->getReasonPhrase(), $response->getStatusCode()); |
||
| 124 | } |
||
| 125 | if (isset($responseBody['error'])) { |
||
| 126 | $_SESSION['systemError'][] = $responseBody['error']; |
||
| 127 | throw new Exceptions\AppException($responseBody['error']['message'], $responseBody['error']['code'] ?? 500); |
||
| 128 | } |
||
| 129 | if (isset($responseBody['result'])) { |
||
| 130 | return $responseBody['result']; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 207 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.