| Conditions | 14 |
| Paths | 14 |
| Total Lines | 53 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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); |
||
| 117 | public function readIcyMetadata(string $streamUrl, int $maxattempts, int $maxredirect) : ?string { |
||
| 118 | $timeout = 10; |
||
| 119 | $streamTitle = null; |
||
| 120 | $pUrl = self::parseStreamUrl($streamUrl); |
||
| 121 | if ($pUrl['sockAddress'] && $pUrl['port']) { |
||
| 122 | $fp = \fsockopen($pUrl['sockAddress'], $pUrl['port'], $errno, $errstr, $timeout); |
||
| 123 | if ($fp !== false) { |
||
| 124 | $out = "GET " . $pUrl['pathname'] . " HTTP/1.1\r\n"; |
||
| 125 | $out .= "Host: ". $pUrl['hostname'] . "\r\n"; |
||
| 126 | $out .= "Accept: */*\r\n"; |
||
| 127 | $out .= HttpUtil::userAgentHeader() . "\r\n"; |
||
| 128 | $out .= "Icy-MetaData: 1\r\n"; |
||
| 129 | $out .= "Connection: Close\r\n\r\n"; |
||
| 130 | \fwrite($fp, $out); |
||
| 131 | \stream_set_timeout($fp, $timeout); |
||
| 132 | |||
| 133 | $header = \fread($fp, 1024); |
||
| 134 | $headers = \explode("\n", $header); |
||
| 135 | |||
| 136 | if (\strpos($headers[0], "200 OK") !== false) { |
||
| 137 | $interval = self::findStrFollowing($headers, "icy-metaint:") ?? '0'; |
||
| 138 | $interval = (int)\trim($interval); |
||
| 139 | |||
| 140 | if ($interval > 0 && $interval <= 64*1024) { |
||
| 141 | $attempts = 0; |
||
| 142 | while ($attempts < $maxattempts && $streamTitle === null) { |
||
| 143 | $bytesToSkip = $interval; |
||
| 144 | if ($attempts === 0) { |
||
| 145 | // The first chunk containing the header may also already contain the beginning of the body, |
||
| 146 | // but this depends on the case. Subtract the body bytes which we already got. |
||
| 147 | $headerEndPos = \strpos($header, "\r\n\r\n") + 4; |
||
| 148 | $bytesToSkip -= \strlen($header) - $headerEndPos; |
||
| 149 | } |
||
| 150 | |||
| 151 | \fseek($fp, $bytesToSkip, SEEK_CUR); |
||
| 152 | |||
| 153 | $streamTitle = self::parseTitleFromStreamMetadata($fp); |
||
| 154 | |||
| 155 | $attempts++; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } else if ($maxredirect > 0 && \strpos($headers[0], "302 Found") !== false) { |
||
| 159 | $location = self::findStrFollowing($headers, "Location: "); |
||
| 160 | if ($location) { |
||
| 161 | $location = \trim($location, "\r"); |
||
| 162 | $streamTitle = $this->readIcyMetadata($location, $maxattempts, $maxredirect-1); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | \fclose($fp); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | return $streamTitle === '' ? null : $streamTitle; |
||
| 170 | } |
||
| 173 |