| Total Complexity | 58 |
| Total Lines | 210 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TvCategorizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TvCategorizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class TvCategorizer extends AbstractCategorizer |
||
| 13 | { |
||
| 14 | protected int $priority = 20; |
||
| 15 | |||
| 16 | public function getName(): string |
||
| 17 | { |
||
| 18 | return 'TV'; |
||
| 19 | } |
||
| 20 | |||
| 21 | public function shouldSkip(ReleaseContext $context): bool |
||
| 22 | { |
||
| 23 | return $context->hasAdultMarkers(); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function categorize(ReleaseContext $context): CategorizationResult |
||
| 27 | { |
||
| 28 | $name = $context->releaseName; |
||
| 29 | |||
| 30 | if (!$this->looksLikeTV($name)) { |
||
| 31 | return $this->noMatch(); |
||
| 32 | } |
||
| 33 | |||
| 34 | if ($result = $this->checkAnime($name)) { |
||
| 35 | return $result; |
||
| 36 | } |
||
| 37 | if ($result = $this->checkSport($name)) { |
||
| 38 | return $result; |
||
| 39 | } |
||
| 40 | if ($result = $this->checkDocumentary($name)) { |
||
| 41 | return $result; |
||
| 42 | } |
||
| 43 | if ($result = $this->checkForeign($context)) { |
||
| 44 | return $result; |
||
| 45 | } |
||
| 46 | if ($result = $this->checkX265($name)) { |
||
| 47 | return $result; |
||
| 48 | } |
||
| 49 | if ($context->catWebDL && ($result = $this->checkWebDL($name))) { |
||
| 50 | return $result; |
||
| 51 | } |
||
| 52 | if ($result = $this->checkUHD($name)) { |
||
| 53 | return $result; |
||
| 54 | } |
||
| 55 | if ($result = $this->checkHD($name, $context->catWebDL)) { |
||
| 56 | return $result; |
||
| 57 | } |
||
| 58 | if ($result = $this->checkSD($name)) { |
||
| 59 | return $result; |
||
| 60 | } |
||
| 61 | if ($result = $this->checkOther($name)) { |
||
| 62 | return $result; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $this->noMatch(); |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function looksLikeTV(string $name): bool |
||
| 69 | { |
||
| 70 | // Season + Episode pattern: S01E01, S01.E01, S1D1, etc. |
||
| 71 | if (preg_match('/[._ -]s\d{1,3}[._ -]?(e|d(isc)?)\d{1,3}([._ -]|$)/i', $name)) { |
||
| 72 | return true; |
||
| 73 | } |
||
| 74 | // Episode-only pattern: .E01., .E02., E01.1080p (common in anime) |
||
| 75 | if (preg_match('/[._ -]E\d{1,4}[._ -]/i', $name)) { |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | // Season pack with Complete/Full: S01.Complete, S01.Full |
||
| 79 | if (preg_match('/[._ -]S\d{1,3}[._ -]?(Complete|COMPLETE|Full|FULL)/i', $name)) { |
||
| 80 | return true; |
||
| 81 | } |
||
| 82 | // Season pack with resolution/quality: S01.1080p, S01.720p, S01.2160p, S02.WEB-DL |
||
| 83 | if (preg_match('/[._ -]S\d{1,3}[._ -](480p|720p|1080[pi]|2160p|4K|UHD|WEB|HDTV|BluRay|NF|AMZN|DSNP|ATVP|HMAX)/i', $name)) { |
||
| 84 | return true; |
||
| 85 | } |
||
| 86 | // Episode pattern: Episode 01, Ep.01, Ep 1 |
||
| 87 | if (preg_match('/\b(Episode|Ep)[._ -]?\d{1,4}\b/i', $name)) { |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | // TV source markers |
||
| 91 | if (preg_match('/\b(HDTV|PDTV|DSR|TVRip|SATRip|DTHRip)\b/i', $name)) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | // Daily show pattern: Show.Name.2024.01.15 or Show.Name.2024-01-15 |
||
| 95 | if (preg_match('/[._ -](19|20)\d{2}[._ -]\d{2}[._ -]\d{2}[._ -]/i', $name)) { |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | // Known anime release groups (should be treated as TV) |
||
| 99 | if (preg_match('/[.\-_ ](URANiME|ANiHLS|HaiKU|ANiURL|SkyAnime|Erai-raws|LostYears|Vodes|SubsPlease|Judas|Ember|EMBER|YuiSubs|ASW|Tsundere-Raws|Anime-Raws)[.\-_ ]?/i', $name)) { |
||
| 100 | return true; |
||
| 101 | } |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function checkAnime(string $name): ?CategorizationResult |
||
| 106 | { |
||
| 107 | if (preg_match('/[._ -]Anime[._ -]/i', $name)) { |
||
| 108 | return $this->matched(Category::TV_ANIME, 0.95, 'anime_pattern'); |
||
| 109 | } |
||
| 110 | // Known anime release groups - now matches anywhere in the name, not just at the end |
||
| 111 | if (preg_match('/[.\-_ ](URANiME|ANiHLS|HaiKU|ANiURL|SkyAnime|Erai-raws|LostYears|Vodes|SubsPlease|Judas|Ember|EMBER|YuiSubs|ASW|Tsundere-Raws|Anime-Raws)[.\-_ ]?/i', $name)) { |
||
| 112 | return $this->matched(Category::TV_ANIME, 0.95, 'anime_group'); |
||
| 113 | } |
||
| 114 | // Anime hash pattern: [GroupName] Title - 01 [ABCD1234] |
||
| 115 | if (preg_match('/^\[.+\].*\d{2,3}.*\[[a-fA-F0-9]{8}\]/i', $name)) { |
||
| 116 | return $this->matched(Category::TV_ANIME, 0.9, 'anime_hash'); |
||
| 117 | } |
||
| 118 | // Episode pattern with known anime indicators |
||
| 119 | if (preg_match('/[._ -]E\d{1,4}[._ -]/i', $name) && |
||
| 120 | preg_match('/\b(BluRay|BD|BDRip)\b/i', $name) && |
||
| 121 | !preg_match('/\bS\d{1,3}\b/i', $name)) { |
||
| 122 | // Episode-only pattern with BluRay but no season - likely anime |
||
| 123 | return $this->matched(Category::TV_ANIME, 0.8, 'anime_episode_bluray'); |
||
| 124 | } |
||
| 125 | return null; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function checkSport(string $name): ?CategorizationResult |
||
| 129 | { |
||
| 130 | if (preg_match('/\b(NFL|NBA|NHL|MLB|MLS|UFC|WWE|Boxing|F1|Formula[._ -]?1|NASCAR|PGA|Tennis|Golf|Soccer|Football|Cricket|Rugby)\b/i', $name) && |
||
| 131 | preg_match('/\d{4}|\b(Season|Week|Round|Match|Game)\b/i', $name)) { |
||
| 132 | return $this->matched(Category::TV_SPORT, 0.85, 'sport'); |
||
| 133 | } |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | protected function checkDocumentary(string $name): ?CategorizationResult |
||
| 138 | { |
||
| 139 | if (preg_match('/\b(Documentary|Docu[._ -]?Series|DOCU)\b/i', $name)) { |
||
| 140 | return $this->matched(Category::TV_DOCU, 0.85, 'documentary'); |
||
| 141 | } |
||
| 142 | return null; |
||
| 143 | } |
||
| 144 | |||
| 145 | protected function checkForeign(ReleaseContext $context): ?CategorizationResult |
||
| 146 | { |
||
| 147 | if (!$context->categorizeForeign) { |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | if (preg_match('/(danish|flemish|Deutsch|dutch|french|german|hebrew|nl[._ -]?sub|dub(bed|s)?|\.NL|norwegian|swedish|swesub|spanish|Staffel)[._ -]|\(german\)|Multisub/i', $context->releaseName)) { |
||
| 151 | return $this->matched(Category::TV_FOREIGN, 0.8, 'foreign_language'); |
||
| 152 | } |
||
| 153 | return null; |
||
| 154 | } |
||
| 155 | |||
| 156 | protected function checkX265(string $name): ?CategorizationResult |
||
| 157 | { |
||
| 158 | if (preg_match('/(S\d+).*(x265).*(rmteam|MeGusta|HETeam|PSA|ONLY|H4S5S|TrollHD|ImE)/i', $name)) { |
||
| 159 | return $this->matched(Category::TV_X265, 0.9, 'x265_group'); |
||
| 160 | } |
||
| 161 | return null; |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function checkWebDL(string $name): ?CategorizationResult |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function checkUHD(string $name): ?CategorizationResult |
||
| 187 | } |
||
| 188 | |||
| 189 | protected function checkHD(string $name, bool $catWebDL): ?CategorizationResult |
||
| 190 | { |
||
| 191 | if (preg_match('/1080([ip])|720p|bluray/i', $name)) { |
||
| 192 | return $this->matched(Category::TV_HD, 0.85, 'hd_resolution'); |
||
| 193 | } |
||
| 194 | if (!$catWebDL && preg_match('/web[._ -]dl|web-?rip/i', $name)) { |
||
| 195 | return $this->matched(Category::TV_HD, 0.8, 'hd_webdl_fallback'); |
||
| 196 | } |
||
| 197 | return null; |
||
| 198 | } |
||
| 199 | |||
| 200 | protected function checkSD(string $name): ?CategorizationResult |
||
| 209 | } |
||
| 210 | |||
| 211 | protected function checkOther(string $name): ?CategorizationResult |
||
| 212 | { |
||
| 222 | } |
||
| 223 | } |
||
| 224 |