| Conditions | 16 |
| Paths | 22 |
| 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); |
||
| 85 | // cut the URL from the last '/' and append 7.html |
||
| 86 | $lastSlash = \strrpos($streamUrl, '/'); |
||
| 87 | $metaUrl = \substr($streamUrl, 0, $lastSlash) . '/7.html'; |
||
| 88 | |||
| 89 | list('content' => $content, 'status_code' => $status_code, 'message' => $message) = HttpUtil::loadFromUrl($metaUrl); |
||
| 90 | |||
| 91 | if ($status_code == 200) { |
||
| 92 | $content = \strip_tags($content); // get rid of the <html><body>...</html></body> decorations |
||
| 93 | $data = \explode(',', $content); |
||
| 94 | return \count($data) > 6 ? \trim($data[6]) : null; // the title field is optional |
||
| 95 | } else { |
||
| 96 | $this->logger->log("Failed to read $metaUrl: $status_code $message", 'debug'); |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | public function readShoutcastV2Metadata(string $streamUrl) : ?string { |
||
| 102 | // cut the URL from the last '/' and append 'stats' |
||
| 103 | $lastSlash = \strrpos($streamUrl, '/'); |
||
| 104 | $metaUrl = \substr($streamUrl, 0, $lastSlash) . '/stats'; |
||
| 105 | |||
| 106 | list('content' => $content, 'status_code' => $status_code, 'message' => $message) = HttpUtil::loadFromUrl($metaUrl); |
||
| 107 | |||
| 108 | if ($status_code == 200) { |
||
| 109 | $rootNode = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_NOCDATA); |
||
| 110 | return (string)$rootNode->SONGTITLE; |
||
| 111 | } else { |
||
| 112 | $this->logger->log("Failed to read $metaUrl: $status_code $message", 'debug'); |
||
| 113 | return null; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 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) { |
||
| 173 |