| Conditions | 16 |
| Paths | 13 |
| Total Lines | 59 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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); |
||
| 67 | public static function fetchStreamData($url, $maxattempts, $maxredirect) { |
||
| 68 | $timeout = 10; |
||
| 69 | $streamTitle = ""; |
||
| 70 | $pUrl = self::parseStreamUrl($url); |
||
| 71 | if (($pUrl['sockadd']) && ($pUrl['port'])) { |
||
| 72 | $fp = fsockopen($pUrl['sockadd'], $pUrl['port'], $errno, $errstr, $timeout); |
||
| 73 | if ($fp != false) { |
||
| 74 | $out = "GET " . $pUrl['pathname'] . " HTTP/1.1\r\n"; |
||
| 75 | $out .= "Host: ". $pUrl['hostname'] . "\r\n"; |
||
| 76 | $out .= "Accept: */*\r\n"; /* test */ |
||
| 77 | $out .= "User-Agent: OCMusic/1.52\r\n"; |
||
| 78 | $out .= "Icy-MetaData: 1\r\n"; |
||
| 79 | $out .= "Connection: Close\r\n\r\n"; |
||
| 80 | fwrite($fp, $out); |
||
| 81 | stream_set_timeout($fp, $timeout); |
||
| 82 | |||
| 83 | $header = fread($fp, 1024); |
||
| 84 | $headers = explode("\n", $header); |
||
| 85 | |||
| 86 | if (strstr($headers[0], "200 OK") !== false) { |
||
| 87 | $interval = 0; |
||
| 88 | $line = self::findStr($headers, "icy-metaint:"); |
||
| 89 | if ($line) { |
||
| 90 | $interval = trim(explode(':', $line)[1]); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (($interval)&&($interval<64001)) { |
||
| 94 | $attempts = 0; |
||
| 95 | while ($attempts < $maxattempts) { |
||
| 96 | for ($j = 0; $j < $interval; $j++) { |
||
| 97 | fread($fp, 1); |
||
| 98 | } |
||
| 99 | |||
| 100 | $meta_length = ord(fread($fp, 1)) * 16; |
||
| 101 | if ($meta_length) { |
||
| 102 | $metadatas = explode(';', fread($fp, $meta_length)); |
||
| 103 | $metadata = self::findStr($metadatas, "StreamTitle"); |
||
| 104 | if ($metadata) { |
||
| 105 | $streamTitle = trim(explode('=', $metadata)[1], "'"); |
||
| 106 | if (strlen($streamTitle) > 256) { |
||
| 107 | $streamTitle = ""; |
||
| 108 | } |
||
| 109 | break; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $attempts++; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } else if (($maxredirect>0)&&(strstr($headers[0], "302 Found") !== false)) { |
||
| 116 | $value = self::findStr($headers, "Location:"); |
||
| 117 | if ($value) { |
||
| 118 | $location = trim(substr($value, 10), "\r"); |
||
| 119 | $streamTitle = RadioMetadata::fetchStreamData($location, $maxattempts, $maxredirect-1); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | fclose($fp); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return $streamTitle; |
||
| 126 | } |
||
| 129 |