Completed
Push — master ( cc2a70...2edf1d )
by f
01:40
created

Plurality::getForms()   F

Complexity

Conditions 31
Paths 397

Size

Total Lines 80
Code Lines 56

Duplication

Lines 12
Ratio 15 %

Importance

Changes 0
Metric Value
cc 31
eloc 56
nc 397
nop 2
dl 12
loc 80
rs 3.8384
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	const ONE = 1;
11
	const TWO_FOUR = 2;
12
	const FIVE_OTHER = 3;
13
14
	protected $neuterExceptions = array(
15
		'поле',
16
		'море',
17
	);
18
19
	static protected $immutableWords = array(
20
		'евро',
21
	);
22
23
	static public function pluralize($word, $count, $animateness = false) {
24
		static $dec, $plu;
25
		if ($dec === null) $dec = new GeneralDeclension();
26
		if ($plu === null) $plu = new self();
27
28
		switch (self::getNumeralForm($count)) {
29
			case self::ONE:
30
				return $word;
31
			case self::TWO_FOUR:
32
				return $dec->getForm($word, self::RODIT_2, $animateness);
33
			case self::FIVE_OTHER:
34
				$forms = $plu->getForms($word, $animateness);
35
				return $forms[self::RODIT_2];
36
		}
37
	}
38
39
	static public function getNumeralForm($count) {
40
		$ending = $count % 10;
41
		if (($count > 20 && $ending == 1) || $count == 1)
42
			return self::ONE;
43
		else if (($count > 20 && in_array($ending, range(2, 4))) || in_array($count, range(2, 4)))
44
			return self::TWO_FOUR;
45
		else
46
			return self::FIVE_OTHER;
47
	}
48
49
	public function getForm($word, $form, $animateness = false) {
50
		$forms = $this->getForms($word, $animateness);
51
		return $forms[$form];
52
	}
53
54
	public function getForms($word, $animateness = false) {
55
		$word = lower($word);
56
		$prefix = slice($word, 0, -1);
57
		$last = slice($word, -1);
58
59
		if (in_array($word, self::$immutableWords)) {
60
			return array(
61
				self::IMENIT => $word,
62
				self::RODIT => $word,
63
				self::DAT => $word,
64
				self::VINIT => $word,
65
				self::TVORIT => $word,
66
				self::PREDLOJ => $this->choosePrepositionByFirstLetter($word, 'об', 'о').' '.$word,
67
			);
68
		}
69
70
		if (($declension = GeneralDeclension::getDeclension($word)) == GeneralDeclension::FIRST_DECLENSION) {
71
			$soft_last = $last == 'й' || (in_array($last, ['ь', 'е', 'ё', 'ю', 'я']) && (self::isConsonant(slice($word, -2, -1)) || slice($word, -2, -1) == 'и'));
72
			$prefix = GeneralDeclension::getPrefixOfFirstDeclension($word, $last);
73
		} else if ($declension == GeneralDeclension::SECOND_DECLENSION) {
74
			 $soft_last = $this->checkLastConsonantSoftness($word);
75
		} else {
76
			$soft_last = false;
77
		}
78
79
		$forms = array();
80
81
		if ($last == 'ч' || slice($word, -2) == 'чь' || ($this->isVowel($last) && in_array(slice($word, -2, -1), array('ч', 'к')))) // before ч, чь, ч+vowel, к+vowel
82
			$forms[Cases::IMENIT_1] = $prefix.'и';
83 View Code Duplication
		else if ($last == 'н')
0 ignored issues
show
Duplication introduced by
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...
84
			$forms[Cases::IMENIT_1] = $prefix.'ы';
85
		else
86
			$forms[Cases::IMENIT_1] = $this->chooseVowelAfterConsonant($last, $soft_last, $prefix.'я', $prefix.'а');
87
88
89
		// RODIT_2
90
		if (in_array($last, array('о', 'е'))) {
91
			// exceptions
92
			if (in_array($word, $this->neuterExceptions))
93
				$forms[Cases::RODIT_2] = $prefix.'ей';
94 View Code Duplication
			else if (slice($word, -2, -1) == 'и')
0 ignored issues
show
Duplication introduced by
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...
95
				$forms[Cases::RODIT_2] = $prefix.'й';
96
			else
97
				$forms[Cases::RODIT_2] = $prefix;
98
		}
99
		else if (slice($word, -2) == 'ка') { // words ending with -ка: чашка, вилка, ложка, тарелка, копейка, батарейка
100
			if (slice($word, -3, -2) == 'л') $forms[Cases::RODIT_2] = slice($word, 0, -2).'ок';
101
			else if (slice($word, -3, -2) == 'й') $forms[Cases::RODIT_2] = slice($word, 0, -3).'ек';
102
			else $forms[Cases::RODIT_2] = slice($word, 0, -2).'ек';
103
		}
104
		else if (in_array($last, array('а'))) // обида, ябеда
105
			$forms[Cases::RODIT_2] = $prefix;
106
		else if (in_array($last, array('я'))) // молния
107
			$forms[Cases::RODIT_2] = $prefix.'й';
108
		else if (RussianLanguage::isHissingConsonant($last) || ($soft_last && $last != 'й') || slice($word, -2) == 'чь')
109
			$forms[Cases::RODIT_2] = $prefix.'ей';
110 View Code Duplication
		else if ($last == 'й')
0 ignored issues
show
Duplication introduced by
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...
111
			$forms[Cases::RODIT_2] = $prefix.'ев';
112
		else // (self::isConsonant($last) && !RussianLanguage::isHissingConsonant($last))
113
			$forms[Cases::RODIT_2] = $prefix.'ов';
114
115
		// DAT_3
116
		$forms[Cases::DAT_3] = $this->chooseVowelAfterConsonant($last, $soft_last && slice($word, -2, -1) != 'ч', $prefix.'ям', $prefix.'ам');
117
118
		// VINIT_4
119
		$forms[Cases::VINIT_4] = GeneralDeclension::getVinitCaseByAnimateness($forms, $animateness);
120
121
		// TVORIT_5
122
		// my personal rule
123
		if ($last == 'ь' && $declension == GeneralDeclension::THIRD_DECLENSION && slice($word, -2) != 'чь') {
124
			$forms[Cases::TVORIT_5] = $prefix.'ми';
125
		} else {
126
			$forms[Cases::TVORIT_5] = $this->chooseVowelAfterConsonant($last, $soft_last && slice($word, -2, -1) != 'ч', $prefix.'ями', $prefix.'ами');
127
		}
128
129
		// PREDLOJ_6
130
		$forms[Cases::PREDLOJ_6] = $this->chooseVowelAfterConsonant($last, $soft_last && slice($word, -2, -1) != 'ч', $prefix.'ях', $prefix.'ах');
131
		$forms[Cases::PREDLOJ_6] = $this->choosePrepositionByFirstLetter($forms[Cases::PREDLOJ_6], 'об', 'о').' '.$forms[Cases::PREDLOJ_6];
132
		return $forms;
133
	}
134
}
135