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