| Conditions | 8 |
| Paths | 16 |
| Total Lines | 63 |
| Code Lines | 34 |
| 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 |
||
| 60 | public function categorize( |
||
| 61 | int|string $groupId, |
||
| 62 | string $releaseName, |
||
| 63 | ?string $poster = '', |
||
| 64 | bool $debug = false |
||
| 65 | ): array { |
||
| 66 | $groupName = UsenetGroup::whereId($groupId)->value('name') ?? ''; |
||
| 67 | |||
| 68 | $context = new ReleaseContext( |
||
| 69 | releaseName: $releaseName, |
||
| 70 | groupId: $groupId, |
||
| 71 | groupName: $groupName, |
||
| 72 | poster: $poster ?? '', |
||
| 73 | categorizeForeign: $this->categorizeForeign, |
||
| 74 | catWebDL: $this->catWebDL, |
||
| 75 | ); |
||
| 76 | |||
| 77 | $bestResult = CategorizationResult::noMatch(); |
||
| 78 | $allResults = []; |
||
| 79 | |||
| 80 | foreach ($this->categorizers as $categorizer) { |
||
| 81 | // Skip if categorizer determines it shouldn't process this release |
||
| 82 | if ($categorizer->shouldSkip($context)) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | $result = $categorizer->categorize($context); |
||
| 87 | |||
| 88 | if ($debug) { |
||
| 89 | $allResults[$categorizer->getName()] = [ |
||
| 90 | 'category_id' => $result->categoryId, |
||
| 91 | 'confidence' => $result->confidence, |
||
| 92 | 'matched_by' => $result->matchedBy, |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | |||
| 96 | // If this result is better than our current best, use it |
||
| 97 | if ($result->isSuccessful() && $result->shouldOverride($bestResult)) { |
||
| 98 | $bestResult = $result; |
||
| 99 | |||
| 100 | // If we have a very high confidence match, we can stop early |
||
| 101 | if ($result->confidence >= 0.95) { |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // Build the return array |
||
| 108 | $returnValue = ['categories_id' => $bestResult->categoryId]; |
||
| 109 | |||
| 110 | if ($debug) { |
||
| 111 | $returnValue['debug'] = [ |
||
| 112 | 'final_category' => $bestResult->categoryId, |
||
| 113 | 'final_confidence' => $bestResult->confidence, |
||
| 114 | 'matched_by' => $bestResult->matchedBy, |
||
| 115 | 'release_name' => $releaseName, |
||
| 116 | 'group_name' => $groupName, |
||
| 117 | 'all_results' => $allResults, |
||
| 118 | 'categorizer_details' => $bestResult->debug, |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $returnValue; |
||
| 123 | } |
||
| 154 |