PluralForm   F
last analyzed

Complexity

Total Complexity 71

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 30.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 71
eloc 36
c 2
b 0
f 0
dl 0
loc 74
ccs 11
cts 36
cp 0.3056
rs 2.7199

1 Method

Rating   Name   Duplication   Size   Complexity  
F get() 0 72 71

How to fix   Complexity   

Complex Class

Complex classes like PluralForm 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 PluralForm, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte;
6
7
class PluralForm
8
{
9 9
    public static function get(int $number, string $lang): int
10
    {
11 9
        if ($lang === 'pt_BR') {
12
            // temporary set a locale for brazilian
13
            $lang = 'xbr';
14
        }
15
16 9
        if (strlen($lang) > 3) {
17 9
            $lang = substr($lang, 0, -strlen((string) strrchr($lang, '_')));
18
        }
19
20 9
        if (in_array($lang, ['az', 'bo', 'dz', 'id', 'ja', 'jv', 'ka', 'km', 'kn', 'ko', 'ms', 'th', 'tr', 'vi', 'zh'])) {
21
            return 0;
22
        }
23
24 9
        if (in_array($lang, ['af', 'bn', 'bg', 'ca', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fur', 'fy', 'gl', 'gu', 'ha', 'he', 'hu', 'is', 'it', 'ku', 'lb', 'ml', 'mn', 'mr', 'nah', 'nb', 'ne', 'nl', 'nn', 'no', 'oc', 'om', 'or', 'pa', 'pap', 'ps', 'pt', 'so', 'sq', 'sv', 'sw', 'ta', 'te', 'tk', 'ur', 'zu'])) {
25 9
            return ($number === 1) ? 0 : 1;
26
        }
27
28 9
        if (in_array($lang, ['am', 'bh', 'fil', 'fr', 'gun', 'hi', 'hy', 'ln', 'mg', 'nso', 'xbr', 'ti', 'wa'])) {
29
            return (($number === 0) || ($number === 1)) ? 0 : 1;
30
        }
31
32 9
        if (in_array($lang, ['be', 'bs', 'hr', 'ru', 'sh', 'sr', 'uk'])) {
33
            return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
34
        }
35
36 9
        if (in_array($lang, ['cs', 'sk'])) {
37 9
            return (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2);
38
        }
39
40
        if (in_array($lang, ['ga'])) {
41
            return (1 == $number) ? 0 : ((2 == $number) ? 1 : 2);
42
        }
43
44
        if (in_array($lang, ['lt'])) {
45
            return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
46
        }
47
48
        if (in_array($lang, ['sl'])) {
49
            return (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3));
50
        }
51
52
        if (in_array($lang, ['mk'])) {
53
            return (1 == $number % 10) ? 0 : 1;
54
        }
55
56
        if (in_array($lang, ['mt'])) {
57
            return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3));
58
        }
59
60
        if (in_array($lang, ['lv'])) {
61
            return (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2);
62
        }
63
64
        if (in_array($lang, ['pl'])) {
65
            return (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2);
66
        }
67
68
        if (in_array($lang, ['cy'])) {
69
            return (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3));
70
        }
71
72
        if (in_array($lang, ['ro'])) {
73
            return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2);
74
        }
75
76
        if (in_array($lang, ['ar'])) {
77
            return (0 == $number) ? 0 : ((1 == $number) ? 1 : ((2 == $number) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5))));
78
        }
79
80
        return 0;
81
    }
82
}
83