| Total Complexity | 47 |
| Total Lines | 96 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConsoleCategorizer 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 ConsoleCategorizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ConsoleCategorizer extends AbstractCategorizer |
||
| 7 | { |
||
| 8 | protected int $priority = 35; |
||
| 9 | public function getName(): string { return 'Console'; } |
||
| 10 | public function shouldSkip(ReleaseContext $context): bool { |
||
| 11 | if ($context->hasAdultMarkers()) return true; |
||
| 12 | // Skip TV shows (season patterns) |
||
| 13 | if (preg_match('/[._ -]S\d{1,3}[._ -]?(E\d|Complete|Full|1080|720|480|2160|WEB|HDTV|BluRay)/i', $context->releaseName)) return true; |
||
| 14 | return false; |
||
| 15 | } |
||
| 16 | public function categorize(ReleaseContext $context): CategorizationResult |
||
| 17 | { |
||
| 18 | $name = $context->releaseName; |
||
| 19 | if ($result = $this->checkPS4($name)) return $result; |
||
| 20 | if ($result = $this->checkPS3($name)) return $result; |
||
| 21 | if ($result = $this->checkPSVita($name)) return $result; |
||
| 22 | if ($result = $this->checkPSP($name)) return $result; |
||
| 23 | if ($result = $this->checkXboxOne($name)) return $result; |
||
| 24 | if ($result = $this->checkXbox360($name)) return $result; |
||
| 25 | if ($result = $this->checkXbox($name)) return $result; |
||
| 26 | if ($result = $this->checkWiiU($name)) return $result; |
||
| 27 | if ($result = $this->checkWii($name)) return $result; |
||
| 28 | if ($result = $this->check3DS($name)) return $result; |
||
| 29 | if ($result = $this->checkNDS($name)) return $result; |
||
| 30 | if ($result = $this->checkOther($name)) return $result; |
||
| 31 | return $this->noMatch(); |
||
| 32 | } |
||
| 33 | protected function checkPS4(string $name): ?CategorizationResult |
||
| 34 | { |
||
| 35 | if (preg_match('/^PS4[_\.\-]/i', $name) || preg_match('/CUSA\d{5}/i', $name) || |
||
| 36 | preg_match('/\.PS4-DUPLEX$/i', $name) || preg_match('/\bPS4\b|PlayStation\s*4/i', $name)) { |
||
| 37 | return $this->matched(Category::GAME_PS4, 0.9, 'ps4'); |
||
| 38 | } |
||
| 39 | return null; |
||
| 40 | } |
||
| 41 | protected function checkPS3(string $name): ?CategorizationResult |
||
| 42 | { |
||
| 43 | if (preg_match('/\bPS3\b|PlayStation\s*3/i', $name)) return $this->matched(Category::GAME_PS3, 0.9, 'ps3'); |
||
| 44 | return null; |
||
| 45 | } |
||
| 46 | protected function checkPSVita(string $name): ?CategorizationResult |
||
| 47 | { |
||
| 48 | if (preg_match('/\bPS\s?Vita\b|PSV(ita)?\b/i', $name)) return $this->matched(Category::GAME_PSVITA, 0.9, 'psvita'); |
||
| 49 | return null; |
||
| 50 | } |
||
| 51 | protected function checkPSP(string $name): ?CategorizationResult |
||
| 52 | { |
||
| 53 | if (preg_match('/\bPSP\b|PlayStation\s*Portable/i', $name)) return $this->matched(Category::GAME_PSP, 0.9, 'psp'); |
||
| 54 | return null; |
||
| 55 | } |
||
| 56 | protected function checkXboxOne(string $name): ?CategorizationResult |
||
| 62 | } |
||
| 63 | protected function checkXbox360(string $name): ?CategorizationResult |
||
| 67 | } |
||
| 68 | protected function checkXbox(string $name): ?CategorizationResult |
||
| 69 | { |
||
| 70 | if (preg_match('/\bXBOX\b/i', $name) && !preg_match('/\b(XBOX\s?360|XBOX\s?ONE|Series)\b/i', $name)) { |
||
| 71 | return $this->matched(Category::GAME_XBOX, 0.85, 'xbox'); |
||
| 72 | } |
||
| 73 | return null; |
||
| 74 | } |
||
| 75 | protected function checkWiiU(string $name): ?CategorizationResult |
||
| 79 | } |
||
| 80 | protected function checkWii(string $name): ?CategorizationResult |
||
| 81 | { |
||
| 82 | if (preg_match('/\bWii\b/i', $name) && !preg_match('/WiiU/i', $name)) return $this->matched(Category::GAME_WII, 0.85, 'wii'); |
||
| 83 | return null; |
||
| 84 | } |
||
| 85 | protected function check3DS(string $name): ?CategorizationResult |
||
| 86 | { |
||
| 87 | if (preg_match('/\b3DS\b|Nintendo\s*3DS/i', $name)) return $this->matched(Category::GAME_3DS, 0.9, '3ds'); |
||
| 88 | return null; |
||
| 89 | } |
||
| 90 | protected function checkNDS(string $name): ?CategorizationResult |
||
| 91 | { |
||
| 92 | if (preg_match('/\bNDS\b|Nintendo\s*DS/i', $name)) return $this->matched(Category::GAME_NDS, 0.9, 'nds'); |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | protected function checkOther(string $name): ?CategorizationResult |
||
| 102 | } |
||
| 103 | } |
||
| 104 |