Completed
Push — master ( bc5cbf...29e0b0 )
by f
02:20
created

Plurality::getCases()   F

Complexity

Conditions 31
Paths 397

Size

Total Lines 80
Code Lines 56

Duplication

Lines 12
Ratio 15 %

Importance

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

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