Conditions | 50 |
Paths | > 20000 |
Total Lines | 122 |
Code Lines | 83 |
Lines | 0 |
Ratio | 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 |
||
216 | function getHTTPSocket($url, $redirs = 0, $prev_parts = '') { |
||
217 | $parts = parse_url($url); |
||
218 | /* relative redirect */ |
||
219 | if (!isset($parts['scheme']) && $prev_parts) $parts['scheme'] = $prev_parts['scheme']; |
||
220 | if (!isset($parts['host']) && $prev_parts) $parts['host'] = $prev_parts['host']; |
||
221 | /* no scheme */ |
||
222 | if (!$this->v('scheme', '', $parts)) return $this->addError('Socket error: Missing URI scheme.'); |
||
223 | /* port tweaks */ |
||
224 | $parts['port'] = ($parts['scheme'] == 'https') ? $this->v1('port', 443, $parts) : $this->v1('port', 80, $parts); |
||
225 | $nl = "\r\n"; |
||
226 | $http_mthd = strtoupper($this->http_method); |
||
227 | if ($this->v1('user', 0, $parts) || $this->useProxy($url)) { |
||
228 | $h_code = $http_mthd . ' ' . $url; |
||
229 | } |
||
230 | else { |
||
231 | $h_code = $http_mthd . ' ' . $this->v1('path', '/', $parts) . (($v = $this->v1('query', 0, $parts)) ? '?' . $v : '') . (($v = $this->v1('fragment', 0, $parts)) ? '#' . $v : ''); |
||
232 | } |
||
233 | $port_code = ($parts['port'] != 80) ? ':' . $parts['port'] : ''; |
||
234 | $h_code .= ' HTTP/1.0' . $nl. |
||
235 | 'Host: ' . $parts['host'] . $port_code . $nl . |
||
236 | (($v = $this->http_accept_header) ? $v . $nl : '') . |
||
237 | (($v = $this->http_user_agent_header) && !preg_match('/User\-Agent\:/', $this->http_custom_headers) ? $v . $nl : '') . |
||
238 | (($http_mthd == 'POST') ? 'Content-Length: ' . strlen($this->message_body) . $nl : '') . |
||
239 | ($this->http_custom_headers ? trim($this->http_custom_headers) . $nl : '') . |
||
240 | $nl . |
||
241 | ''; |
||
242 | /* post body */ |
||
243 | if ($http_mthd == 'POST') { |
||
244 | $h_code .= $this->message_body . $nl; |
||
245 | } |
||
246 | /* connect */ |
||
247 | if ($this->useProxy($url)) { |
||
248 | $s = @fsockopen($this->a['proxy_host'], $this->a['proxy_port'], $errno, $errstr, $this->timeout); |
||
249 | } |
||
250 | elseif (($parts['scheme'] == 'https') && function_exists('stream_socket_client')) { |
||
251 | // SSL options via config array, code by Hannes Muehleisen ([email protected]) |
||
252 | $context = stream_context_create(); |
||
253 | foreach ($this->a as $k => $v) { |
||
254 | if (preg_match('/^arc_reader_ssl_(.+)$/', $k, $m)) { |
||
255 | stream_context_set_option($context, 'ssl', $m[1], $v); |
||
256 | } |
||
257 | } |
||
258 | $s = stream_socket_client('ssl://' . $parts['host'] . $port_code, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context); |
||
259 | } |
||
260 | elseif ($parts['scheme'] == 'https') { |
||
261 | $s = @fsockopen('ssl://' . $parts['host'], $parts['port'], $errno, $errstr, $this->timeout); |
||
262 | } |
||
263 | elseif ($parts['scheme'] == 'http') { |
||
264 | $s = @fsockopen($parts['host'], $parts['port'], $errno, $errstr, $this->timeout); |
||
265 | } |
||
266 | if (!$s) { |
||
267 | return $this->addError('Socket error: Could not connect to "' . $url . '" (proxy: ' . ($this->useProxy($url) ? '1' : '0') . '): ' . $errstr); |
||
268 | } |
||
269 | /* request */ |
||
270 | fwrite($s, $h_code); |
||
271 | /* timeout */ |
||
272 | if ($this->timeout) { |
||
273 | //stream_set_blocking($s, false); |
||
274 | stream_set_timeout($s, $this->timeout); |
||
275 | } |
||
276 | /* response headers */ |
||
277 | $h = array(); |
||
278 | $this->response_headers = $h; |
||
279 | if (!$this->ping_only) { |
||
280 | do { |
||
281 | $line = trim(fgets($s, 4096)); |
||
282 | $info = stream_get_meta_data($s); |
||
283 | if (preg_match("/^HTTP[^\s]+\s+([0-9]{1})([0-9]{2})(.*)$/i", $line, $m)) {/* response code */ |
||
284 | $error = in_array($m[1], array('4', '5')) ? $m[1] . $m[2] . ' ' . $m[3] : ''; |
||
285 | $error = ($m[1].$m[2] == '304') ? '304 '.$m[3] : $error; |
||
286 | $h['response-code'] = $m[1] . $m[2]; |
||
287 | $h['error'] = $error; |
||
288 | $h['redirect'] = ($m[1] == '3') ? true : false; |
||
289 | } |
||
290 | elseif (preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $m)) {/* header */ |
||
291 | $h_name = strtolower($m[1]); |
||
292 | if (!isset($h[$h_name])) {/* 1st value */ |
||
293 | $h[$h_name] = trim($m[2]); |
||
294 | } |
||
295 | elseif (!is_array($h[$h_name])) {/* 2nd value */ |
||
296 | $h[$h_name] = array($h[$h_name], trim($m[2])); |
||
297 | } |
||
298 | else {/* more values */ |
||
299 | $h[$h_name][] = trim($m[2]); |
||
300 | } |
||
301 | } |
||
302 | } while(!$info['timed_out'] && !feof($s) && $line); |
||
303 | $h['format'] = strtolower(preg_replace('/^([^\s]+).*$/', '\\1', $this->v('content-type', '', $h))); |
||
304 | $h['encoding'] = preg_match('/(utf\-8|iso\-8859\-1|us\-ascii)/', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : ''; |
||
305 | $h['encoding'] = preg_match('/charset=\s*([^\s]+)/si', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : $h['encoding']; |
||
306 | $this->response_headers = $h; |
||
307 | /* result */ |
||
308 | if ($info['timed_out']) { |
||
309 | return $this->addError('Connection timed out after ' . $this->timeout . ' seconds'); |
||
310 | } |
||
311 | /* error */ |
||
312 | if ($v = $this->v('error', 0, $h)) { |
||
313 | /* digest auth */ |
||
314 | /* 401 received */ |
||
315 | if (preg_match('/Digest/i', $this->v('www-authenticate', '', $h)) && !$this->digest_auth) { |
||
316 | $this->setCredentials($url); |
||
317 | $this->digest_auth = 1; |
||
318 | return $this->getHTTPSocket($url); |
||
319 | } |
||
320 | return $this->addError($error . ' "' . (!feof($s) ? trim(strip_tags(fread($s, 128))) . '..."' : '')); |
||
321 | } |
||
322 | /* redirect */ |
||
323 | if ($this->v('redirect', 0, $h) && ($new_url = $this->v1('location', 0, $h))) { |
||
324 | fclose($s); |
||
325 | $this->redirects[$url] = $new_url; |
||
326 | $this->base = $new_url; |
||
327 | if ($redirs > $this->max_redirects) { |
||
328 | return $this->addError('Max numbers of redirects exceeded.'); |
||
329 | } |
||
330 | return $this->getHTTPSocket($new_url, $redirs+1, $parts); |
||
331 | } |
||
332 | } |
||
333 | if ($this->timeout) { |
||
334 | stream_set_blocking($s, true); |
||
335 | } |
||
336 | return array('type' => 'socket', 'url' => $url, 'socket' =>& $s, 'headers' => $h, 'pos' => 0, 'size' => $this->v('content-length', 0, $h), 'buffer' => ''); |
||
337 | } |
||
338 | |||
424 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.