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