Completed
Push — master ( ddf1df...c8a008 )
by f
01:49
created

src/Russian/Plurality.php (2 issues)

Upgrade to new PHP Analysis Engine

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
/**
5
 * Rules are from http://morpher.ru/Russian/Noun.aspx
6
 */
7
class Plurality extends \morphos\Plurality implements Cases {
8
	use RussianLanguage;
9
10
	protected $neuterExceptions = array(
11
		'поле',
12
		'море',
13
	);
14
15
	static public function pluralize($word, $count, $animateness = false) {
16
		static $dec, $plu;
17
		if ($dec === null) $dec = new GeneralDeclension();
18
		if ($plu === null) $plu = new self();
19
20
		$ending = $count % 10;
21
		if (($count > 20 && $ending == 1) || $count == 1) {
22
			return $word;
23
		} else if (($count > 20 && in_array($ending, range(2, 4))) || in_array($count, range(2, 4))) {
24
			return $dec->getForm($word, self::RODIT_2, $animateness);
25
		} else {
26
			$forms = $plu->getForms($word, $animateness);
27
			return $forms[self::RODIT_2];
28
		}
29
	}
30
31
	public function getForm($word, $form, $animateness = false) {
32
		$forms = $this->getForms($word, $animateness);
33
		return $forms[$form];
34
	}
35
36
	public function getForms($word, $animateness = false) {
37
		$word = lower($word);
38
		$prefix = slice($word, 0, -1);
39
		$last = slice($word, -1);
40
41
		if (($declension = GeneralDeclension::getDeclension($word)) == GeneralDeclension::FIRST_DECLENSION) {
42
			$soft_last = $last == 'й' || (in_array($last, ['ь', 'е', 'ё', 'ю', 'я']) && self::isConsonant(slice($word, -2, -1)));
43
			$prefix = GeneralDeclension::getPrefixOfFirstDeclension($word, $last);
44
		} else if ($declension == GeneralDeclension::SECOND_DECLENSION) {
45
			 $soft_last = $this->checkLastConsonantSoftness($word);
46
		} else {
47
			$soft_last = false;
48
		}
49
50
		$forms = array();
51
52 View Code Duplication
		if ($last == 'ч' || slice($word, -2) == 'чь')
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
53
			$forms[Cases::IMENIT_1] = $prefix.'и';
54
		else
55
			$forms[Cases::IMENIT_1] = $this->chooseVowelAfterConsonant($last, $soft_last, $prefix.'я', $prefix.'а');
56
57
58
		// RODIT_2
59
		if (in_array($last, array('о', 'е'))) {
60
			// exceptions
61
			if (in_array($word, $this->neuterExceptions))
62
				$forms[Cases::RODIT_2] = $prefix.'ей';
63
			else
64
				$forms[Cases::RODIT_2] = $prefix;
65
		}
66
		else if (slice($word, -2) == 'ка') {
67
			// чашка, вилка, ложка, тарелка
68
			if (slice($word, -3, -2) == 'л') $forms[Cases::RODIT_2] = slice($word, 0, -2).'ок';
69
			else $forms[Cases::RODIT_2] = slice($word, 0, -2).'ек';
70
		}
71
		else if (in_array($last, array('а'))) // обида, ябеда
72
			$forms[Cases::RODIT_2] = $prefix;
73
		else if (in_array($last, array('я'))) // молния
74
			$forms[Cases::RODIT_2] = $prefix.'й';
75
		else if (RussianLanguage::isHissingConsonant($last) || ($soft_last && $last != 'й') || slice($word, -2) == 'чь')
76
			$forms[Cases::RODIT_2] = $prefix.'ей';
77
		else if ($last == 'й')
78
			$forms[Cases::RODIT_2] = $prefix.'ев';
79
		else // (self::isConsonant($last) && !RussianLanguage::isHissingConsonant($last))
80
			$forms[Cases::RODIT_2] = $prefix.'ов';
81
82
		// DAT_3
83
		$forms[Cases::DAT_3] = $this->chooseVowelAfterConsonant($last, $soft_last, $prefix.'ям', $prefix.'ам');
84
85
		// VINIT_4
86
		$forms[Cases::VINIT_4] = GeneralDeclension::getVinitCaseByAnimateness($forms, $animateness);
87
88
		// TVORIT_5
89
		// my personal rule
90 View Code Duplication
		if ($last == 'ь' && $declension == GeneralDeclension::THIRD_DECLENSION && slice($word, -2) != 'чь') {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
91
			$forms[Cases::TVORIT_5] = $prefix.'ми';
92
		} else {
93
			$forms[Cases::TVORIT_5] = $this->chooseVowelAfterConsonant($last, $soft_last, $prefix.'ями', $prefix.'ами');
94
		}
95
96
		// PREDLOJ_6
97
		$forms[Cases::PREDLOJ_6] = $this->chooseVowelAfterConsonant($last, $soft_last, $prefix.'ях', $prefix.'ах');
98
		$forms[Cases::PREDLOJ_6] = $this->choosePrepositionByFirstLetter($forms[Cases::PREDLOJ_6], 'об', 'о').' '.$forms[Cases::PREDLOJ_6];
99
		return $forms;
100
	}
101
}
102