| Conditions | 23 |
| Paths | 144 |
| Total Lines | 83 |
| Code Lines | 61 |
| 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 declare(strict_types=1); |
||
| 26 | public static function fetchStreamData($url, $maxattempts, $maxredirect) { |
||
| 27 | $parse_url = parse_url($url); |
||
| 28 | $timeout = 10; |
||
| 29 | $port = 80; |
||
| 30 | if (isset($parse_url['port'])) { |
||
| 31 | $port = $parse_url['port']; |
||
| 32 | } else if ($parse_url['scheme'] == "https") { |
||
| 33 | $port = 443; |
||
| 34 | } |
||
| 35 | $hostname = $parse_url['host']; |
||
| 36 | $pathname = $parse_url['path']; |
||
| 37 | if (isset($parse_url['query'])) { |
||
| 38 | $pathname .= "?" . $parse_url['query']; |
||
| 39 | } |
||
| 40 | if ($parse_url['scheme'] == "https") { |
||
| 41 | $sockadd = "ssl://" . $hostname; |
||
| 42 | } else { |
||
| 43 | $sockadd = $hostname; |
||
| 44 | } |
||
| 45 | $streamTitle = ""; |
||
| 46 | if (($sockadd)&&($port)) { |
||
| 47 | $fp = fsockopen($sockadd, $port, $errno, $errstr, $timeout); |
||
| 48 | if ($fp != false) { |
||
| 49 | $out = "GET " . $pathname . " HTTP/1.1\r\n"; |
||
| 50 | $out .= "Host: ". $hostname . "\r\n"; |
||
| 51 | $out .= "Accept: */*\r\n"; /* test */ |
||
| 52 | $out .= "User-Agent: OCMusic/1.52\r\n"; |
||
| 53 | $out .= "Icy-MetaData: 1\r\n"; |
||
| 54 | $out .= "Connection: Close\r\n\r\n"; |
||
| 55 | fwrite($fp, $out); |
||
| 56 | stream_set_timeout($fp, $timeout); |
||
| 57 | |||
| 58 | $header = fread($fp, 1024); |
||
| 59 | $headers = array(); |
||
|
|
|||
| 60 | $headers = explode("\n", $header); |
||
| 61 | |||
| 62 | if (strstr($headers[0], "200 OK") !== false) { |
||
| 63 | $interval = 0; |
||
| 64 | foreach ($headers as $value) { |
||
| 65 | if (strstr($value, "icy-metaint:") !== false) { |
||
| 66 | $val = explode(':', $value); |
||
| 67 | $interval = trim($val[1]); |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | if (($interval)&&($interval<64001)) { |
||
| 73 | $attempts = 0; |
||
| 74 | while ($attempts < $maxattempts) { |
||
| 75 | for ($j = 0; $j < $interval; $j++) { |
||
| 76 | fread($fp, 1); |
||
| 77 | } |
||
| 78 | |||
| 79 | $meta_length = ord(fread($fp, 1)) * 16; |
||
| 80 | if ($meta_length) { |
||
| 81 | $metadatas = explode(';', fread($fp, $meta_length)); |
||
| 82 | foreach ($metadatas as $metadata) { |
||
| 83 | if (strstr($metadata, "StreamTitle") !== false) { |
||
| 84 | $streamTitle = trim(explode('=', $metadata)[1], "'"); |
||
| 85 | if (strlen($streamTitle) > 120) { |
||
| 86 | $streamTitle = ""; |
||
| 87 | } |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | $attempts++; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } else if (($maxredirect>0)&&(strstr($headers[0], "302 Found") !== false)) { |
||
| 96 | foreach ($headers as $value) { |
||
| 97 | $val = strstr($value, "Location:"); |
||
| 98 | if ($val) { |
||
| 99 | $location = trim(substr($val, 10), "\r"); |
||
| 100 | $streamTitle = RadioMetadata::fetchStreamData($location, $maxattempts, $maxredirect-1); |
||
| 101 | break; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | fclose($fp); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | return $streamTitle; |
||
| 109 | } |
||
| 112 |