These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace morphos\Russian; |
||
3 | |||
4 | use morphos\S; |
||
5 | |||
6 | /** |
||
7 | * Rules are from http://morpher.ru/Russian/Noun.aspx |
||
8 | */ |
||
9 | class NounPluralization extends \morphos\NounPluralization implements Cases |
||
10 | { |
||
11 | use RussianLanguage, CasesHelper; |
||
12 | |||
13 | const ONE = 1; |
||
14 | const TWO_FOUR = 2; |
||
15 | const FIVE_OTHER = 3; |
||
16 | |||
17 | protected static $abnormalExceptions = [ |
||
18 | 'человек' => ['люди', 'человек', 'людям', 'людей', 'людьми', 'людях'], |
||
19 | ]; |
||
20 | |||
21 | protected static $neuterExceptions = [ |
||
22 | 'поле', |
||
23 | 'море', |
||
24 | ]; |
||
25 | |||
26 | protected static $genitiveExceptions = [ |
||
27 | 'письмо' => 'писем', |
||
28 | 'пятно' => 'пятен', |
||
29 | 'кресло' => 'кресел', |
||
30 | 'коромысло' => 'коромысел', |
||
31 | 'ядро' => 'ядер', |
||
32 | 'блюдце' => 'блюдец', |
||
33 | 'полотенце' => 'полотенец', |
||
34 | 'гривна' => 'гривен', |
||
35 | 'год' => 'лет', |
||
36 | ]; |
||
37 | |||
38 | protected static $immutableWords = [ |
||
39 | 'евро', |
||
40 | 'пенни', |
||
41 | ]; |
||
42 | |||
43 | protected static $runawayVowelsExceptions = [ |
||
44 | 'писе*ц', |
||
45 | 'песе*ц', |
||
46 | 'глото*к', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @return array|bool |
||
51 | */ |
||
52 | 74 | View Code Duplication | protected static function getRunAwayVowelsList() |
53 | { |
||
54 | 74 | $runawayVowelsNormalized = []; |
|
55 | 74 | foreach (static::$runawayVowelsExceptions as $word) { |
|
56 | 74 | $runawayVowelsNormalized[str_replace('*', null, $word)] = S::indexOf($word, '*') - 1; |
|
57 | } |
||
58 | 74 | return $runawayVowelsNormalized; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Склонение существительного для сочетания с числом (кол-вом предметов). |
||
63 | * |
||
64 | * @param string|int $word Название предмета |
||
65 | * @param int|string $count Количество предметов |
||
66 | * @param bool $animateness Признак одушевленности |
||
67 | * @param string $case Род существительного |
||
68 | * |
||
69 | * @return string |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | 52 | public static function pluralize($word, $count = 2, $animateness = false, $case = null) |
|
73 | { |
||
74 | // меняем местами аргументы, если они переданы в старом формате |
||
75 | 52 | View Code Duplication | if (is_string($count) && is_numeric($word)) { |
76 | 15 | list($count, $word) = [$word, $count]; |
|
77 | } |
||
78 | |||
79 | 52 | if ($case !== null) |
|
80 | 2 | $case = static::canonizeCase($case); |
|
81 | |||
82 | // для адъективных существительных правила склонения проще: |
||
83 | // только две формы |
||
84 | 52 | View Code Duplication | if (static::isAdjectiveNoun($word)) { |
85 | 3 | if (static::getNumeralForm($count) == static::ONE) |
|
86 | 2 | return $word; |
|
87 | else |
||
88 | 3 | return NounPluralization::getCase($word, |
|
89 | 3 | $case !== null |
|
90 | ? $case |
||
91 | 3 | : static::RODIT, $animateness); |
|
92 | } |
||
93 | |||
94 | 50 | if ($case === null) { |
|
95 | 48 | switch (static::getNumeralForm($count)) { |
|
96 | 48 | case static::ONE: |
|
97 | 28 | return $word; |
|
98 | 48 | case static::TWO_FOUR: |
|
99 | 34 | return NounDeclension::getCase($word, static::RODIT, $animateness); |
|
100 | 36 | case static::FIVE_OTHER: |
|
101 | 36 | return NounPluralization::getCase($word, static::RODIT, $animateness); |
|
102 | } |
||
103 | } |
||
104 | |||
105 | 2 | View Code Duplication | if (static::getNumeralForm($count) == static::ONE) |
106 | 2 | return NounDeclension::getCase($word, $case, $animateness); |
|
107 | else |
||
108 | 2 | return NounPluralization::getCase($word, $case, $animateness); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param $count |
||
113 | * @return int |
||
114 | */ |
||
115 | 64 | public static function getNumeralForm($count) |
|
116 | { |
||
117 | 64 | if ($count > 100) { |
|
118 | 41 | $count %= 100; |
|
119 | } |
||
120 | 64 | $ending = $count % 10; |
|
121 | |||
122 | 64 | if (($count > 20 && $ending == 1) || $count == 1) { |
|
123 | 36 | return static::ONE; |
|
124 | 63 | } elseif (($count > 20 && in_array($ending, range(2, 4))) || in_array($count, range(2, 4))) { |
|
125 | 44 | return static::TWO_FOUR; |
|
126 | } else { |
||
127 | 48 | return static::FIVE_OTHER; |
|
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param $word |
||
133 | * @param $case |
||
134 | * @param bool $animateness |
||
135 | * @return string |
||
136 | * @throws \Exception |
||
137 | */ |
||
138 | 40 | public static function getCase($word, $case, $animateness = false) |
|
139 | { |
||
140 | 40 | $case = static::canonizeCase($case); |
|
141 | 40 | $forms = static::getCases($word, $animateness); |
|
142 | 40 | return $forms[$case]; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param $word |
||
147 | * @param bool $animateness |
||
148 | * @return array |
||
149 | */ |
||
150 | 85 | public static function getCases($word, $animateness = false) |
|
151 | { |
||
152 | 85 | $word = S::lower($word); |
|
153 | |||
154 | 85 | if (in_array($word, static::$immutableWords, true)) { |
|
155 | return [ |
||
156 | 1 | static::IMENIT => $word, |
|
157 | 1 | static::RODIT => $word, |
|
158 | 1 | static::DAT => $word, |
|
159 | 1 | static::VINIT => $word, |
|
160 | 1 | static::TVORIT => $word, |
|
161 | 1 | static::PREDLOJ => $word, |
|
162 | ]; |
||
163 | } |
||
164 | |||
165 | 85 | if (isset(static::$abnormalExceptions[$word])) { |
|
166 | 1 | return array_combine( |
|
167 | 1 | [static::IMENIT, static::RODIT, static::DAT, static::VINIT, static::TVORIT, static::PREDLOJ], |
|
168 | 1 | static::$abnormalExceptions[$word]); |
|
169 | } |
||
170 | |||
171 | // Адъективное склонение (Сущ, образованные от прилагательных и причастий) |
||
172 | // Пример: прохожий, существительное |
||
173 | 84 | if (static::isAdjectiveNoun($word)) { |
|
174 | 11 | return static::declinateAdjective($word, $animateness); |
|
175 | } |
||
176 | |||
177 | // Субстантивное склонение (существительные) |
||
178 | 74 | return static::declinateSubstative($word, $animateness); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Склонение обычных существительных. |
||
183 | * @param $word |
||
184 | * @param $animateness |
||
185 | * @return array |
||
186 | */ |
||
187 | 74 | protected static function declinateSubstative($word, $animateness) |
|
188 | { |
||
189 | 74 | $prefix = S::slice($word, 0, -1); |
|
190 | 74 | $last = S::slice($word, -1); |
|
191 | |||
192 | 74 | $runaway_vowels_list = static::getRunAwayVowelsList(); |
|
193 | 74 | if (isset($runaway_vowels_list[$word])) { |
|
194 | 1 | $vowel_offset = $runaway_vowels_list[$word]; |
|
195 | 1 | $word = S::slice($word, 0, $vowel_offset) . S::slice($word, $vowel_offset + 1); |
|
196 | } |
||
197 | |||
198 | 74 | if (($declension = NounDeclension::getDeclension($word)) == NounDeclension::SECOND_DECLENSION) { |
|
199 | 47 | $soft_last = $last == 'й' || (in_array($last, ['ь', 'е', 'ё', 'ю', 'я'], true) |
|
200 | && (( |
||
201 | 13 | static::isConsonant(S::slice($word, -2, -1)) && !static::isHissingConsonant(S::slice($word, -2, -1))) |
|
202 | 47 | || S::slice($word, -2, -1) == 'и')); |
|
203 | 47 | $prefix = NounDeclension::getPrefixOfSecondDeclension($word, $last); |
|
204 | 33 | } elseif ($declension == NounDeclension::FIRST_DECLENSION) { |
|
205 | 29 | $soft_last = static::checkLastConsonantSoftness($word); |
|
206 | View Code Duplication | } else { |
|
0 ignored issues
–
show
|
|||
207 | 4 | $soft_last = in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true); |
|
208 | } |
||
209 | |||
210 | 74 | $forms = []; |
|
211 | |||
212 | 74 | if (in_array($last, ['ч', 'г'], false) || in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true) |
|
213 | 74 | || (static::isVowel($last) && in_array(S::slice($word, -2, -1), ['ч', 'к'], true))) { // before ч, чь, сь, ч+vowel, к+vowel |
|
214 | 32 | $forms[Cases::IMENIT] = $prefix.'и'; |
|
215 | 47 | } elseif (in_array($last, ['н', 'ц', 'р', 'т'], true)) { |
|
216 | 17 | $forms[Cases::IMENIT] = $prefix.'ы'; |
|
217 | View Code Duplication | } else { |
|
218 | 33 | $forms[Cases::IMENIT] = static::chooseVowelAfterConsonant($last, $soft_last, $prefix.'я', $prefix.'а'); |
|
219 | } |
||
220 | |||
221 | // RODIT |
||
222 | 74 | if (isset(static::$genitiveExceptions[$word])) { |
|
223 | 10 | $forms[Cases::RODIT] = static::$genitiveExceptions[$word]; |
|
224 | 66 | } elseif (in_array($last, ['о', 'е'], true)) { |
|
225 | // exceptions |
||
226 | 8 | if (in_array($word, static::$neuterExceptions, true)) { |
|
227 | 3 | $forms[Cases::RODIT] = $prefix.'ей'; |
|
228 | 5 | View Code Duplication | } elseif (S::slice($word, -2, -1) == 'и') { |
229 | 2 | $forms[Cases::RODIT] = $prefix.'й'; |
|
230 | } else { |
||
231 | 8 | $forms[Cases::RODIT] = $prefix; |
|
232 | } |
||
233 | 58 | } elseif (S::slice($word, -2) == 'ка' && S::slice($word, -3, -2) !== 'и') { // words ending with -ка: чашка, вилка, ложка, тарелка, копейка, батарейка |
|
234 | 7 | if (S::slice($word, -3, -2) == 'л') { |
|
235 | 2 | $forms[Cases::RODIT] = S::slice($word, 0, -2).'ок'; |
|
236 | 5 | View Code Duplication | } elseif (S::slice($word, -3, -2) == 'й') { |
237 | 3 | $forms[Cases::RODIT] = S::slice($word, 0, -3).'ек'; |
|
238 | } else { |
||
239 | 7 | $forms[Cases::RODIT] = S::slice($word, 0, -2).'ек'; |
|
240 | } |
||
241 | 51 | } elseif (in_array($last, ['а'], true)) { // обида, ябеда |
|
242 | 20 | $forms[Cases::RODIT] = $prefix; |
|
243 | 37 | View Code Duplication | } elseif (in_array($last, ['я'], true)) { // молния |
244 | 1 | $forms[Cases::RODIT] = $prefix.'й'; |
|
245 | 36 | } elseif (RussianLanguage::isHissingConsonant($last) || ($soft_last && $last != 'й') || in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true)) { |
|
246 | 13 | $forms[Cases::RODIT] = $prefix.'ей'; |
|
247 | 25 | View Code Duplication | } elseif ($last == 'й' || S::slice($word, -2) == 'яц') { // месяц |
248 | 7 | $forms[Cases::RODIT] = $prefix.'ев'; |
|
249 | } else { // (static::isConsonant($last) && !RussianLanguage::isHissingConsonant($last)) |
||
250 | 18 | $forms[Cases::RODIT] = $prefix.'ов'; |
|
251 | } |
||
252 | |||
253 | // DAT |
||
254 | 74 | $forms[Cases::DAT] = static::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ям', $prefix.'ам'); |
|
255 | |||
256 | // VINIT |
||
257 | 74 | $forms[Cases::VINIT] = NounDeclension::getVinitCaseByAnimateness($forms, $animateness); |
|
258 | |||
259 | // TVORIT |
||
260 | // my personal rule |
||
261 | 74 | if ($last == 'ь' && $declension == NounDeclension::THIRD_DECLENSION && !in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true)) { |
|
262 | $forms[Cases::TVORIT] = $prefix.'ми'; |
||
263 | } else { |
||
264 | 74 | $forms[Cases::TVORIT] = static::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ями', $prefix.'ами'); |
|
265 | } |
||
266 | |||
267 | // PREDLOJ |
||
268 | 74 | $forms[Cases::PREDLOJ] = static::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ях', $prefix.'ах'); |
|
269 | 74 | return $forms; |
|
270 | } |
||
271 | |||
272 | /** |
||
273 | * Склонение существительных, образованных от прилагательных и причастий. |
||
274 | * Rules are from http://rusgram.narod.ru/1216-1231.html |
||
275 | * @param $word |
||
276 | * @param $animateness |
||
277 | * @return array |
||
278 | */ |
||
279 | 11 | protected static function declinateAdjective($word, $animateness) |
|
280 | { |
||
281 | 11 | $prefix = S::slice($word, 0, -2); |
|
282 | 11 | $vowel = static::isHissingConsonant(S::slice($prefix, -1)) ? 'и' : 'ы'; |
|
283 | return [ |
||
284 | 11 | Cases::IMENIT => $prefix.$vowel.'е', |
|
285 | 11 | Cases::RODIT => $prefix.$vowel.'х', |
|
286 | 11 | Cases::DAT => $prefix.$vowel.'м', |
|
287 | 11 | Cases::VINIT => $prefix.$vowel.($animateness ? 'х' : 'е'), |
|
288 | 11 | Cases::TVORIT => $prefix.$vowel.'ми', |
|
289 | 11 | Cases::PREDLOJ => $prefix.$vowel.'х', |
|
290 | ]; |
||
291 | } |
||
292 | } |
||
293 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.