Passed
Push — master ( 03c468...179943 )
by Darko
11:56
created

ConsoleCategorizer   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 47
eloc 49
c 1
b 0
f 0
dl 0
loc 96
rs 8.64

15 Methods

Rating   Name   Duplication   Size   Complexity  
A checkXboxOne() 0 6 2
A checkXbox360() 0 4 2
A getName() 0 1 1
A checkPS4() 0 7 5
C categorize() 0 16 13
A checkPS3() 0 4 2
A checkPSP() 0 4 2
A shouldSkip() 0 5 3
A checkWiiU() 0 4 2
A checkPSVita() 0 4 2
A checkXbox() 0 6 3
A checkOther() 0 7 3
A check3DS() 0 4 2
A checkNDS() 0 4 2
A checkWii() 0 4 3

How to fix   Complexity   

Complex Class

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
2
namespace App\Services\Categorization\Categorizers;
3
use App\Models\Category;
4
use App\Services\Categorization\CategorizationResult;
5
use App\Services\Categorization\ReleaseContext;
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
57
    {
58
        if (preg_match('/\b(XboxOne|XBOX\s*One|XBONE|XB1|Xbox\s*Series[._ -]?[SX]|XSX|XSS)\b/i', $name)) {
59
            return $this->matched(Category::GAME_XBOXONE, 0.9, 'xboxone');
60
        }
61
        return null;
62
    }
63
    protected function checkXbox360(string $name): ?CategorizationResult
64
    {
65
        if (preg_match('/\b(Xbox360|XBOX360|X360)\b/i', $name)) return $this->matched(Category::GAME_XBOX360, 0.9, 'xbox360');
66
        return null;
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
76
    {
77
        if (preg_match('/\bWii\s*U\b|WiiU/i', $name)) return $this->matched(Category::GAME_WIIU, 0.9, 'wiiu');
78
        return null;
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
96
    {
97
        if (preg_match('/\b(PS[12X]|PS2|SNES|NES|SEGA|GB[AC]?|Dreamcast|Saturn|Atari|N64)\b/i', $name) &&
98
            preg_match('/\b(EUR|JP|JPN|NTSC|PAL|USA|ROM)\b/i', $name)) {
99
            return $this->matched(Category::GAME_OTHER, 0.8, 'retro_console');
100
        }
101
        return null;
102
    }
103
}
104