Completed
Pull Request — master (#90)
by Jonathan
02:59
created

Uninflected   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 150
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isUninflected() 0 3 1
A getUninflected() 0 3 1
A getRegex() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector\Rules;
6
7
use function implode;
8
use function preg_match;
9
10
class Uninflected
11
{
12
    /**
13
     * Default words that should not be inflected.
14
     *
15
     * @var string[]
16
     */
17
    public const DEFAULT = [
18
        '.*?media',
19
        'Amoyese',
20
        'audio',
21
        'bison',
22
        'Borghese',
23
        'bream',
24
        'breeches',
25
        'britches',
26
        'buffalo',
27
        'cantus',
28
        'carp',
29
        'chassis',
30
        'clippers',
31
        'cod',
32
        'coitus',
33
        'compensation',
34
        'Congoese',
35
        'contretemps',
36
        'coreopsis',
37
        'corps',
38
        'data',
39
        'debris',
40
        'deer',
41
        'diabetes',
42
        'djinn',
43
        'education',
44
        'eland',
45
        'elk',
46
        'emoji',
47
        'equipment',
48
        'evidence',
49
        'Faroese',
50
        'feedback',
51
        'fish',
52
        'flounder',
53
        'Foochowese',
54
        'Furniture',
55
        'furniture',
56
        'gallows',
57
        'Genevese',
58
        'Genoese',
59
        'Gilbertese',
60
        'gold',
61
        'headquarters',
62
        'herpes',
63
        'hijinks',
64
        'Hottentotese',
65
        'information',
66
        'innings',
67
        'jackanapes',
68
        'jedi',
69
        'Kiplingese',
70
        'knowledge',
71
        'Kongoese',
72
        'love',
73
        'Lucchese',
74
        'Luggage',
75
        'mackerel',
76
        'Maltese',
77
        'metadata',
78
        'mews',
79
        'moose',
80
        'mumps',
81
        'Nankingese',
82
        'news',
83
        'nexus',
84
        'Niasese',
85
        'nutrition',
86
        'offspring',
87
        'Pekingese',
88
        'Piedmontese',
89
        'pincers',
90
        'Pistoiese',
91
        'plankton',
92
        'pliers',
93
        'pokemon',
94
        'police',
95
        'Portuguese',
96
        'proceedings',
97
        'rabies',
98
        'rain',
99
        'rhinoceros',
100
        'rice',
101
        'salmon',
102
        'Sarawakese',
103
        'scissors',
104
        'sea[- ]bass',
105
        'series',
106
        'Shavese',
107
        'shears',
108
        'sheep',
109
        'siemens',
110
        'species',
111
        'staff',
112
        'swine',
113
        'traffic',
114
        'trousers',
115
        'trout',
116
        'tuna',
117
        'us',
118
        'Vermontese',
119
        'Wenchowese',
120
        'wheat',
121
        'whiting',
122
        'wildebeest',
123
        'Yengeese',
124
    ];
125
126
    /** @var string[] */
127
    private $uninflected = [];
128
129
    /** @var string|null */
130
    private $regex;
131
132
    /**
133
     * @param string[] $uninflected
134
     */
135 526
    public function __construct(array $uninflected)
136
    {
137 526
        $this->uninflected = $uninflected;
138 526
    }
139
140
    /**
141
     * @return string[]
142
     */
143 526
    public function getUninflected() : array
144
    {
145 526
        return $this->uninflected;
146
    }
147
148 420
    public function isUninflected(string $word) : bool
149
    {
150 420
        return preg_match('/^(' . $this->getRegex() . ')$/i', $word, $regs) === 1;
151
    }
152
153 420
    private function getRegex() : string
154
    {
155 420
        if ($this->regex === null) {
156 420
            $this->regex = '(?:' . implode('|', $this->uninflected) . ')';
157
        }
158
159 420
        return $this->regex;
160
    }
161
}
162