| Conditions | 25 |
| Paths | 247 |
| Total Lines | 83 |
| Code Lines | 40 |
| 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 |
||
| 124 | public function parseNzb(string $guid, int $relID, int $groupID, bool $nfoCheck = false): bool|array |
||
| 125 | { |
||
| 126 | $nzbFile = $this->loadNzb($guid); |
||
| 127 | if ($nzbFile === false) { |
||
| 128 | return false; |
||
| 129 | } |
||
| 130 | |||
| 131 | $messageID = $hiddenID = ''; |
||
| 132 | $actualParts = $artificialParts = 0; |
||
| 133 | // Initialize foundPAR2 based on settings; if lookupPar2 is false, we don't need to find one. |
||
| 134 | $foundPAR2 = $this->lookupPar2 === false; |
||
| 135 | // Initialize NFO flags based on whether we are checking for NFOs. |
||
| 136 | $foundNFO = $hiddenNFO = $nfoCheck === false; |
||
| 137 | $nfoMessageId = null; // Store potential NFO message ID here |
||
| 138 | |||
| 139 | foreach ($nzbFile->file as $nzbContents) { |
||
| 140 | $segmentCountInFile = 0; |
||
| 141 | $firstSegmentId = null; // Initialize here for each file |
||
| 142 | foreach ($nzbContents->segments->segment as $segment) { |
||
| 143 | $actualParts++; |
||
| 144 | $segmentCountInFile++; |
||
| 145 | // Store the first segment ID of the current file, potentially useful for NFO/PAR2 |
||
| 146 | if ($segmentCountInFile === 1) { |
||
| 147 | $firstSegmentId = (string) $segment; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $subject = (string) $nzbContents->attributes()->subject; |
||
| 152 | |||
| 153 | // Calculate artificial parts from subject |
||
| 154 | $artificialParts += $this->parserService->extractPartsTotal($subject); |
||
| 155 | |||
| 156 | // --- NFO Detection --- |
||
| 157 | // Check for explicit NFO files first (with enhanced patterns) |
||
| 158 | if ($nfoCheck && ! $foundNFO && isset($firstSegmentId)) { |
||
| 159 | $nfoDetection = $this->parserService->detectNfoFile($subject); |
||
| 160 | if ($nfoDetection !== false) { |
||
| 161 | $nfoMessageId = ['hidden' => $nfoDetection['hidden'], 'id' => $firstSegmentId, 'priority' => $nfoDetection['priority']]; |
||
| 162 | $foundNFO = true; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Check for potential "hidden" NFOs with improved detection |
||
| 167 | // Only consider this if an explicit NFO wasn't found yet |
||
| 168 | if ($nfoCheck && ! $foundNFO && ! $hiddenNFO && isset($firstSegmentId)) { |
||
| 169 | $hiddenNfoDetection = $this->parserService->detectHiddenNfoFile($subject, $segmentCountInFile); |
||
| 170 | if ($hiddenNfoDetection !== false) { |
||
| 171 | $nfoMessageId = ['hidden' => $hiddenNfoDetection['hidden'], 'id' => $firstSegmentId, 'priority' => $hiddenNfoDetection['priority']]; |
||
| 172 | $hiddenNFO = true; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // --- PAR2 Detection --- |
||
| 177 | // Look specifically for the .par2 index file (often small, but not always 1/1) |
||
| 178 | if ($this->lookupPar2 && ! $foundPAR2 && isset($firstSegmentId) && $this->parserService->detectPar2IndexFile($subject)) { |
||
| 179 | // Attempt to parse the PAR2 file using its first segment ID |
||
| 180 | if ($this->postProcessService->parsePAR2($firstSegmentId, $relID, $groupID, $this->nntp, 1) === true) { |
||
| 181 | Release::query()->where('id', $relID)->update(['proc_par2' => 1]); |
||
| 182 | $foundPAR2 = true; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } // End foreach $nzbFile->file |
||
| 186 | |||
| 187 | // Calculate completion |
||
| 188 | $completion = $this->calculateCompletion($actualParts, $artificialParts); |
||
| 189 | |||
| 190 | Release::query()->where('id', $relID)->update(['completion' => $completion]); |
||
| 191 | |||
| 192 | // If NFO check was requested, return the found message ID (prioritizing explicit) |
||
| 193 | if ($nfoCheck && $nfoMessageId !== null && isset($nfoMessageId['id']) && \strlen($nfoMessageId['id']) > 1) { |
||
| 194 | return $nfoMessageId; |
||
| 195 | } |
||
| 196 | |||
| 197 | // If NFO check was requested but nothing suitable was found |
||
| 198 | if ($nfoCheck && $nfoMessageId === null) { |
||
| 199 | // Update status to indicate no NFO was found in the NZB structure |
||
| 200 | Release::query()->where('id', $relID)->update(['nfostatus' => Nfo::NFO_NONFO]); |
||
| 201 | |||
| 202 | return false; |
||
| 203 | } |
||
| 204 | |||
| 205 | // If NFO check was not requested, the function's primary goal might be just completion/PAR2 update. |
||
| 206 | return false; |
||
| 207 | } |
||
| 321 |