| Conditions | 26 |
| Paths | 211 |
| Total Lines | 83 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 155 | public function extractMessageIDs( |
||
| 156 | array $nzbContents, |
||
| 157 | string $groupName, |
||
| 158 | int $segmentsToDownload, |
||
| 159 | bool $processThumbnails, |
||
| 160 | bool $processJPGSample, |
||
| 161 | bool $processMediaInfo, |
||
| 162 | bool $processAudioInfo, |
||
| 163 | string $audioFileRegex, |
||
| 164 | string $videoFileRegex, |
||
| 165 | string $supportFileRegex, |
||
| 166 | string $ignoreBookRegex |
||
| 167 | ): array { |
||
| 168 | $result = [ |
||
| 169 | 'hasCompressedFile' => false, |
||
| 170 | 'sampleMessageIDs' => [], |
||
| 171 | 'jpgMessageIDs' => [], |
||
| 172 | 'mediaInfoMessageID' => '', |
||
| 173 | 'audioInfoMessageID' => '', |
||
| 174 | 'audioInfoExtension' => '', |
||
| 175 | 'bookFileCount' => 0, |
||
| 176 | ]; |
||
| 177 | |||
| 178 | foreach ($nzbContents as $file) { |
||
| 179 | try { |
||
| 180 | $title = $file['title'] ?? ''; |
||
| 181 | $segments = $file['segments'] ?? []; |
||
| 182 | |||
| 183 | // Skip support/nfo files |
||
| 184 | if (preg_match('/(?:'.$supportFileRegex.'|nfo\\b|inf\\b|ofn\\b)($|[ ")]|-])(?!.{20,})/i', $title)) { |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Compressed file detection |
||
| 189 | if (! $result['hasCompressedFile'] && preg_match( |
||
| 190 | '/(\\.(part\\d+|[rz]\\d+|rar|0+|0*10?|zipr\\d{2,3}|zipx?|7z(?:\\.\\d{3})?|(?:tar\\.)?(?:gz|bz2|xz))("|\\s*\\.rar)*($|[ ")]|-])|"[a-f0-9]{32}\\.[1-9]\\d{1,2}".*\\(\\d+\\/\\d{2,}\\)$)/i', |
||
| 191 | $title |
||
| 192 | )) { |
||
| 193 | $result['hasCompressedFile'] = true; |
||
| 194 | } |
||
| 195 | |||
| 196 | // Look for a video sample (not an image) |
||
| 197 | if ($processThumbnails && empty($result['sampleMessageIDs']) && ! empty($segments) |
||
| 198 | && stripos($title, 'sample') !== false |
||
| 199 | && ! preg_match('/\.jpe?g$/i', $title) |
||
| 200 | ) { |
||
| 201 | $result['sampleMessageIDs'] = $this->extractSegments($segments, $segmentsToDownload); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Look for a JPG picture (not a CD cover) |
||
| 205 | if ($processJPGSample && empty($result['jpgMessageIDs']) && ! empty($segments) |
||
| 206 | && ! preg_match('/flac|lossless|mp3|music|inner-sanctum|sound/i', $groupName) |
||
| 207 | && preg_match('/\.jpe?g[. ")\]]/i', $title) |
||
| 208 | ) { |
||
| 209 | $result['jpgMessageIDs'] = $this->extractSegments($segments, $segmentsToDownload); |
||
| 210 | } |
||
| 211 | |||
| 212 | // Look for a video file for MediaInfo (sample video) |
||
| 213 | if ($processMediaInfo && empty($result['mediaInfoMessageID']) && ! empty($segments[0]) |
||
| 214 | && stripos($title, 'sample') !== false |
||
| 215 | && preg_match('/'.$videoFileRegex.'[. ")\]]/i', $title) |
||
| 216 | ) { |
||
| 217 | $result['mediaInfoMessageID'] = (string) $segments[0]; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Look for an audio file |
||
| 221 | if ($processAudioInfo && empty($result['audioInfoMessageID']) && ! empty($segments) |
||
| 222 | && preg_match('/'.$audioFileRegex.'[. ")\]]/i', $title, $type) |
||
| 223 | ) { |
||
| 224 | $result['audioInfoExtension'] = $type[1]; |
||
| 225 | $result['audioInfoMessageID'] = (string) $segments[0]; |
||
| 226 | } |
||
| 227 | |||
| 228 | // Count book files |
||
| 229 | if (preg_match($ignoreBookRegex, $title)) { |
||
| 230 | $result['bookFileCount']++; |
||
| 231 | } |
||
| 232 | } catch (\ErrorException $e) { |
||
| 233 | Log::debug($e->getTraceAsString()); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | return $result; |
||
| 238 | } |
||
| 265 |