Conditions | 15 |
Paths | 204 |
Total Lines | 57 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
168 | public function curl(): CurlResponse |
||
169 | { |
||
170 | $post_data = null; |
||
171 | $send_headers = []; |
||
172 | $request_uri = $this->getURL(); |
||
173 | |||
174 | // Headers |
||
175 | foreach ($this->getHeaders() as $k => $v) $send_headers[] = "{$k}: {$v}"; |
||
176 | |||
177 | // Request Data |
||
178 | switch ($this->getMethod()) { |
||
179 | case 'GET': |
||
180 | case 'DELETE': |
||
181 | foreach ($this->getDataParams() as $name => $value) $request_uri = URL::setGetVar($name, $value, $request_uri); |
||
182 | break; |
||
183 | |||
184 | case 'POST': |
||
185 | $post_data = ($this->isFileUpload()) ? $this->getDataParams() : http_build_query($this->getDataParams()); |
||
186 | break; |
||
187 | case 'PUT': |
||
188 | $post_data = http_build_query($this->getDataParams()); |
||
189 | break; |
||
190 | } |
||
191 | |||
192 | if ($this->_enable_cache && $this->getMethod() === 'GET') { |
||
193 | $cache_key = urlencode($request_uri) . implode(',', $send_headers); |
||
194 | if (array_key_exists($cache_key, self::$_cache)) { |
||
195 | return self::$_cache[$cache_key]; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $ch = curl_init(); |
||
200 | curl_setopt($ch, CURLOPT_URL, $request_uri); |
||
201 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
202 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod()); |
||
203 | curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); |
||
204 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
205 | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
||
206 | curl_setopt($ch, CURLOPT_ENCODING, ''); |
||
207 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
208 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||
209 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
210 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
||
211 | |||
212 | if (!empty($send_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $send_headers); |
||
213 | if (!is_null($post_data)) { |
||
214 | curl_setopt($ch, CURLOPT_POST, 1); |
||
215 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
||
216 | } |
||
217 | |||
218 | $result = curl_exec($ch); |
||
219 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
220 | |||
221 | $response = new CurlResponse((int) $http_code, (string) $result); |
||
222 | if ($this->_enable_cache && isset($cache_key)) self::$_cache[$cache_key] = $response; |
||
223 | |||
224 | return $response; |
||
225 | } |
||
238 |