| Conditions | 10 |
| Paths | 17 |
| Total Lines | 60 |
| Code Lines | 28 |
| 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:
| 1 | <?php |
||
| 44 | public function parse( |
||
| 45 | array $headers, |
||
| 46 | string $groupName, |
||
| 47 | bool $partRepair = false, |
||
| 48 | ?array $missingParts = null |
||
| 49 | ): array { |
||
| 50 | $parsed = []; |
||
| 51 | $headersRepaired = []; |
||
| 52 | |||
| 53 | foreach ($headers as $header) { |
||
| 54 | // Check if we got the article |
||
| 55 | if (! isset($header['Number'])) { |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | |||
| 59 | // For part repair, only process missing parts |
||
| 60 | if ($partRepair && $missingParts !== null) { |
||
| 61 | if (! \in_array($header['Number'], $missingParts, false)) { |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | $headersRepaired[] = $header['Number']; |
||
| 65 | } |
||
| 66 | |||
| 67 | // Parse subject to get base name and part/total like "(12/45)" |
||
| 68 | if (! preg_match('/^\s*(?!"Usenet Index Post)(.+)\s+\((\d+)\/(\d+)\)/', $header['Subject'], $matches)) { |
||
| 69 | $this->notYEnc++; |
||
| 70 | |||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Normalize to include yEnc if missing |
||
| 75 | if (stripos($header['Subject'], 'yEnc') === false) { |
||
| 76 | $matches[1] .= ' yEnc'; |
||
| 77 | } |
||
| 78 | |||
| 79 | $header['matches'] = $matches; |
||
| 80 | |||
| 81 | // Filter subject based on black/white list |
||
| 82 | if ($this->blacklistService->isBlackListed($header, $groupName)) { |
||
| 83 | $this->blacklisted++; |
||
| 84 | |||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Ensure Bytes is set |
||
| 89 | if (empty($header['Bytes'])) { |
||
| 90 | $header['Bytes'] = $header[':bytes'] ?? 0; |
||
| 91 | } |
||
| 92 | |||
| 93 | $parsed[] = [ |
||
| 94 | 'header' => $header, |
||
| 95 | 'repaired' => $partRepair, |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | |||
| 99 | return [ |
||
| 100 | 'headers' => array_column($parsed, 'header'), |
||
| 101 | 'repaired' => $headersRepaired, |
||
| 102 | 'notYEnc' => $this->notYEnc, |
||
| 103 | 'blacklisted' => $this->blacklisted, |
||
| 104 | ]; |
||
| 168 |