| Total Lines | 74 |
| Code Lines | 44 |
| 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 |
||
| 171 | private function http($url, $headers, $params, $method) |
||
| 172 | { |
||
| 173 | if (!empty($params)) { |
||
| 174 | $params = http_build_query($params); |
||
| 175 | // GETの場合はstream_context_createでクエリを渡しても有効にならないため |
||
| 176 | // URLにクエリストリングを付けることで対処 |
||
| 177 | if ($method === "GET") { |
||
| 178 | $url .= "?" . $params; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | if (empty($headers) && ($method === "POST" || $method === "PUT")) { |
||
| 182 | $contentLength = !is_string($params) ? 0 : strlen($params); |
||
| 183 | $headers = [ |
||
| 184 | "Content-Type: application/x-www-form-urlencoded", |
||
| 185 | "Content-Length: " . $contentLength |
||
| 186 | ]; |
||
| 187 | } |
||
| 188 | $request = [ |
||
| 189 | "method" => $method, |
||
| 190 | "content" => $params, |
||
| 191 | "timeout" => $this->timeout |
||
| 192 | ]; |
||
| 193 | // Proxy設定 |
||
| 194 | if ($this->proxy_host && $this->proxy_port) { |
||
| 195 | $this->proxy($request); |
||
| 196 | } |
||
| 197 | // Basic認証 |
||
| 198 | if ($this->basic_auth_id && $this->basic_auth_password) { |
||
| 199 | $this->basicAuth($headers); |
||
| 200 | } |
||
| 201 | if (!empty($headers)) { |
||
| 202 | $request["header"] = implode("\r\n", $headers); |
||
| 203 | } |
||
| 204 | // レスポンス |
||
| 205 | $response = @file_get_contents($url, false, stream_context_create(["http" => $request])); |
||
| 206 | |||
| 207 | if (!isset($http_response_header)) { |
||
| 208 | $hasHeader = @get_headers($url); |
||
| 209 | if ($hasHeader === false) { // ヘッダを持たない場合、存在しないURL |
||
| 210 | $this->status_code = 404; |
||
| 211 | $this->logger->error("URL not found: " . $url); |
||
| 212 | } else { // ヘッダを持つ場合はタイムアウトが発生 |
||
| 213 | $this->status_code = 408; |
||
| 214 | $this->logger->error("Request timeout: " . $url); |
||
| 215 | } |
||
| 216 | |||
| 217 | return null; |
||
| 218 | } else { |
||
| 219 | $this->responseHeader = $http_response_header; |
||
| 220 | } |
||
| 221 | |||
| 222 | // ヘッダ情報を取得 |
||
| 223 | foreach ($this->responseHeader as $value) { |
||
| 224 | // Content-Type |
||
| 225 | if (preg_match('/^Content-Type:\s.+/', $value)) { |
||
| 226 | $this->content_type = $value; |
||
| 227 | } |
||
| 228 | // ステータスコード |
||
| 229 | if (preg_match('/^HTTP\/.+\s([0-9]{3})/', $this->responseHeader[0], $matches)) { |
||
| 230 | $this->status_code = intval($matches[1]); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($this->status_code === 200) { // HTTP通信の結果が200 |
||
| 235 | $this->logger->info("HTTP {$method} success({$this->status_code}): {$url}"); |
||
| 236 | |||
| 237 | return $response; |
||
| 238 | } else { // HTTP通信の結果が200以外 |
||
| 239 | if ($this->status_code === null) { |
||
| 240 | $this->status_code = 500; |
||
| 241 | } |
||
| 242 | $this->logger->error("HTTP {$method} failure({$this->status_code}): {$url}"); |
||
| 243 | |||
| 244 | return null; |
||
| 245 | } |
||
| 260 |