| Conditions | 9 |
| Paths | 19 |
| Total Lines | 75 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 12 | ||
| Bugs | 2 | Features | 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 |
||
| 116 | public function getResponse( |
||
| 117 | string $model, |
||
| 118 | string $calledMethod, |
||
| 119 | ?array $methodProperties, |
||
| 120 | bool $auth = true |
||
| 121 | ): array |
||
| 122 | { |
||
| 123 | |||
| 124 | $this->model = $model; |
||
| 125 | $this->calledMethod = $calledMethod; |
||
| 126 | $this->methodProperties = $methodProperties; |
||
| 127 | |||
| 128 | $response = $this->getData($auth); |
||
| 129 | |||
| 130 | // Ошибка курла |
||
| 131 | if (!($response instanceof ClientResponse)) { |
||
| 132 | $this->return['info']['error'] = $response; |
||
| 133 | $this->return['info']['StatusCode'] = '20000100016'; // Сервис не доступен |
||
| 134 | $this->return['info']['StatusLocale'] = __('novaposhta::novaposhta.error_data'); |
||
| 135 | |||
| 136 | return $this->return; |
||
| 137 | } |
||
| 138 | |||
| 139 | // Ошибка запроса или 401 |
||
| 140 | if ($response->failed()) { |
||
| 141 | $this->return['info']['error'] = trans('novaposhta::novaposhta.error_data'); |
||
| 142 | |||
| 143 | return $this->return; |
||
| 144 | } |
||
| 145 | |||
| 146 | // если что-то не JSON (типа application/pdf) |
||
| 147 | if ($response->header('Content-Type') !== 'application/json') { |
||
| 148 | $this->return['success'] = $response->ok(); |
||
| 149 | |||
| 150 | $this->return['info']['file'] = true; |
||
| 151 | $this->return['info']['ContentType'] = $response->header('Content-Type'); |
||
| 152 | $this->return['info']['ContentDisposition'] = $response->header('Content-Disposition') ?? ''; |
||
| 153 | |||
| 154 | $this->return['result'] = $response->body(); |
||
| 155 | |||
| 156 | return $this->return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $answer = $response->json(); |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Development. |
||
| 163 | */ |
||
| 164 | if ($this->dev) { |
||
| 165 | $this->return['dev'] = $answer; |
||
| 166 | } |
||
| 167 | |||
| 168 | // TODO: Возможно, исправлено |
||
| 169 | if ($auth === false && isset($answer[0])) { |
||
| 170 | /** |
||
| 171 | * Костыль для Новой Почты. |
||
| 172 | * Спасибо Вам большое, что нормально не выдаете ответ :). |
||
| 173 | */ |
||
| 174 | $answer = $answer[0]; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (isset($answer['success'])) { |
||
| 178 | $this->return['success'] = $answer['success']; |
||
| 179 | } |
||
| 180 | |||
| 181 | if (isset($answer['data'])) { |
||
| 182 | $this->return['result'] = $answer['data']; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Формирование info. |
||
| 187 | */ |
||
| 188 | $this->return['info'] = $this->addInfo($answer); |
||
| 189 | |||
| 190 | return $this->return; |
||
| 191 | } |
||
| 257 |