Conditions | 18 |
Paths | 62 |
Total Lines | 72 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
28 | public function httpGet($url, array $options) |
||
29 | { |
||
30 | if (Context::inCoroutine()) { |
||
31 | $parts = parse_url($url); |
||
32 | $path = isset($parts['path']) ? $parts['path'] : '/'; |
||
33 | if (isset($parts['query'])) { |
||
34 | $path .= '?' . $parts['query']; |
||
35 | } |
||
36 | if (isset($parts['fragment'])) { |
||
37 | $path .= '#' . $parts['fragment']; |
||
38 | } |
||
39 | $client = new CoroutineClient($parts['host'], isset($parts['port']) ? $parts['port'] : 80, isset($parts['scheme']) && $parts['scheme'] === 'https'); |
||
40 | if (isset($options['timeout'])) { |
||
41 | $client->set([ |
||
42 | 'timeout' => $options['timeout'], |
||
43 | ]); |
||
44 | } |
||
45 | $client->get($path); |
||
46 | $client->close(); |
||
47 | if ($client->errCode === 110) { |
||
48 | return ['statusCode' => 0, 'headers' => [], 'body' => '']; |
||
49 | } |
||
50 | if ($client->errCode !== 0) { |
||
51 | $msg = sprintf('Failed to send Http request(%s), errcode=%d, errmsg=%s', $url, $client->errCode, $client->errMsg); |
||
52 | throw new \RuntimeException($msg, $client->errCode); |
||
53 | } |
||
54 | return ['statusCode' => $client->statusCode, 'headers' => $client->headers, 'body' => $client->body]; |
||
55 | } |
||
56 | |||
57 | $handle = curl_init(); |
||
58 | $finalOptions = [ |
||
59 | CURLOPT_URL => $url, |
||
60 | CURLOPT_HTTPGET => true, |
||
61 | ] + $this->curlOptions; |
||
62 | if (isset($options['timeout'])) { |
||
63 | $finalOptions[CURLOPT_TIMEOUT] = $options['timeout']; |
||
64 | } |
||
65 | curl_setopt_array($handle, $finalOptions); |
||
66 | $responseStr = curl_exec($handle); |
||
67 | $errno = curl_errno($handle); |
||
68 | $errmsg = curl_error($handle); |
||
69 | // Fix: curl_errno() always return 0 when fail |
||
70 | if ($errno !== 0 || $errmsg !== '') { |
||
71 | curl_close($handle); |
||
72 | $msg = sprintf('Failed to send Http request(%s), errcode=%d, errmsg=%s', $url, $errno, $errmsg); |
||
73 | throw new \RuntimeException($msg, $errno); |
||
74 | } |
||
75 | |||
76 | $headerSize = curl_getinfo($handle, CURLINFO_HEADER_SIZE); |
||
77 | $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
||
78 | curl_close($handle); |
||
79 | |||
80 | $header = substr($responseStr, 0, $headerSize); |
||
81 | $body = substr($responseStr, $headerSize); |
||
82 | $lines = explode("\n", $header); |
||
83 | array_shift($lines); // Remove status |
||
84 | |||
85 | $headers = []; |
||
86 | foreach ($lines as $part) { |
||
87 | $middle = explode(':', $part); |
||
88 | $key = trim($middle[0]); |
||
89 | if ($key === '') { |
||
90 | continue; |
||
91 | } |
||
92 | if (isset($headers[$key])) { |
||
93 | $headers[$key] = (array)$headers[$key]; |
||
94 | $headers[$key][] = isset($middle[1]) ? trim($middle[1]) : ''; |
||
95 | } else { |
||
96 | $headers[$key] = isset($middle[1]) ? trim($middle[1]) : ''; |
||
97 | } |
||
98 | } |
||
99 | return ['statusCode' => $statusCode, 'headers' => $headers, 'body' => $body]; |
||
100 | } |
||
101 | } |