Completed
Push — master ( dd6397...5d0bef )
by f
01:45
created

RussianLanguage::chooseEndingBySonority()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
1
<?php
2
namespace morphos\Russian;
3
4
use morphos\Gender;
5
use morphos\S;
6
7
trait RussianLanguage
8
{
9
    /**
10
     * @var array Все гласные
11
     */
12
    public static $vowels = array(
13
        'а',
14
        'е',
15
        'ё',
16
        'и',
17
        'о',
18
        'у',
19
        'ы',
20
        'э',
21
        'ю',
22
        'я',
23
    );
24
25
    /**
26
     * @var array Все согласные
27
     */
28
    public static $consonants = array(
29
        'б',
30
        'в',
31
        'г',
32
        'д',
33
        'ж',
34
        'з',
35
        'й',
36
        'к',
37
        'л',
38
        'м',
39
        'н',
40
        'п',
41
        'р',
42
        'с',
43
        'т',
44
        'ф',
45
        'х',
46
        'ц',
47
        'ч',
48
        'ш',
49
        'щ',
50
    );
51
52
    /**
53
     * @var array Пары согласных
54
     */
55
    public static $pairs = array(
56
        'б' => 'п',
57
        'в' => 'ф',
58
        'г' => 'к',
59
        'д' => 'т',
60
        'ж' => 'ш',
61
        'з' => 'с',
62
    );
63
64
    /**
65
     * @var array Звонкие согласные
66
     */
67
    public static $sonorousConsonants = ['б', 'в', 'г', 'д', 'з', 'ж', 'л', 'м', 'н', 'р'];
68
    /**
69
     * @var array Глухие согласные
70
     */
71
    public static $deafConsonants = ['п', 'ф', 'к', 'т', 'с', 'ш', 'х', 'ч', 'щ'];
72
73
    /**
74
     * Проверка гласной
75
     */
76
    public static function isVowel($char)
77
    {
78
        return in_array($char, self::$vowels);
79
    }
80
81
    /**
82
     * Проверка согласной
83
     */
84
    public static function isConsonant($char)
85
    {
86
        return in_array($char, self::$consonants);
87
    }
88
89
    /**
90
     * Проверка звонкости согласной
91
     */
92
    public static function isSonorousConsonant($char)
93
    {
94
        return in_array($char, self::$sonorousConsonants);
95
    }
96
97
    /**
98
     * Проверка глухости согласной
99
     */
100
    public static function isDeafConsonant($char)
101
    {
102
        return in_array($char, self::$deafConsonants);
103
    }
104
105
    /**
106
     * Щипящая ли согласная
107
     */
108
    public static function isHissingConsonant($consonant)
109
    {
110
        return in_array(S::lower($consonant), array('ж', 'ш', 'ч', 'щ'));
111
    }
112
113
    protected static function isVelarConsonant($consonant)
114
    {
115
        return in_array(S::lower($consonant), array('г', 'к', 'х'));
116
    }
117
118
    /**
119
     * Подсчет слогов
120
     */
121
    public static function countSyllables($string)
122
    {
123
        return S::chars_count($string, self::$vowels);
124
    }
125
126
    /**
127
     * Проверка парности согласной
128
     */
129
    public static function isPaired($consonant)
130
    {
131
        $consonant = S::lower($consonant);
132
        return array_key_exists($consonant, self::$pairs) || (array_search($consonant, self::$pairs) !== false);
133
    }
134
135
    /**
136
     * Проверка мягкости последней согласной
137
     */
138
    public static function checkLastConsonantSoftness($word)
139
    {
140
        if (($substring = S::last_position_for_one_of_chars(S::lower($word), self::$consonants)) !== false) {
141
            if (in_array(S::slice($substring, 0, 1), ['й', 'ч', 'щ'])) { // always soft consonants
142
                return true;
143
            } elseif (S::length($substring) > 1 && in_array(S::slice($substring, 1, 2), ['е', 'ё', 'и', 'ю', 'я', 'ь'])) { // consonants are soft if they are trailed with these vowels
144
                return true;
145
            }
146
        }
147
        return false;
148
    }
149
150
    /**
151
     * Выбор предлога по первой букве
152
     */
153
    public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition)
154
    {
155 View Code Duplication
        if (in_array(S::upper(S::slice($word, 0, 1)), array('А', 'О', 'И', 'У', 'Э'))) {
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...
156
            return $prepositionWithVowel;
157
        } else {
158
            return $preposition;
159
        }
160
    }
161
162
    /**
163
     * Выбор окончания в зависимости от мягкости
164
     */
165
    public static function chooseVowelAfterConsonant($last, $soft_last, $after_soft, $after_hard)
166
    {
167
        if ((RussianLanguage::isHissingConsonant($last) && !in_array($last, array('ж', 'ч'))) || /*self::isVelarConsonant($last) ||*/ $soft_last) {
168
            return $after_soft;
169
        } else {
170
            return $after_hard;
171
        }
172
    }
173
174
    /**
175
     * @param string $verb Verb to modify if gender is female
176
     * @param string $gender If not `m`, verb will be modified
177
     * @return string Correct verb
178
     */
179
    public static function verb($verb, $gender)
180
    {
181
        $verb = S::lower($verb);
182
        // возвратный глагол
183
        if (S::slice($verb, -2) == 'ся') {
184
            return ($gender == Gender::MALE ? $verb : mb_substr($verb, 0, -2).'ась');
185
        }
186
187
        // обычный глагол
188
        return ($gender == Gender::MALE ? $verb : $verb.'а');
189
    }
190
191
    /**
192
     * Add 'в' or 'во' prepositional before the word
193
     * @param string $word
194
     * @return string
195
     */
196
    public static function in($word)
197
    {
198
        $normalized = trim(S::lower($word));
199
        if (in_array(S::slice($normalized, 0, 1), ['в', 'ф']))
200
            return 'во '.$word;
201
        return 'в '.$word;
202
    }
203
204
    /**
205
     * Add 'с' or 'со' prepositional before the word
206
     * @param string $word
207
     * @return string
208
     */
209
    public static function with($word)
210
    {
211
        $normalized = trim(S::lower($word));
212
        if (in_array(S::slice($normalized, 0, 1), ['c', 'з', 'ш', 'ж']) && static::isConsonant(S::slice($normalized, 1, 2)) || S::slice($normalized, 0, 1) == 'щ')
213
            return 'со '.$word;
214
        return 'с '.$word;
215
    }
216
217
    /**
218
     * Add 'о' or 'об' or 'обо' prepositional before the word
219
     * @param string $word
220
     * @return string
221
     */
222
    public static function about($word)
223
    {
224
        $normalized = trim(S::lower($word));
225
        if (static::isVowel(S::slice($normalized, 0, 1)) && !in_array(S::slice($normalized, 0, 1), ['е', 'ё', 'ю', 'я']))
226
            return 'об '.$word;
227
228 View Code Duplication
        if (in_array(S::slice($normalized, 0, 3), ['все', 'всё', 'всю', 'что', 'мне']))
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...
229
            return 'обо '.$word;
230
231
        return 'о '.$word;
232
    }
233
234
    /**
235
     * Выбирает первое или второе окончание в зависимости от звонкости/глухости в конце слова.
236
     * @param string $word Слово (или префикс), на основе звонкости которого нужно выбрать окончание
237
     * @param string $ifSonorous Окончание, если слово оканчивается на звонкую согласную
0 ignored issues
show
Documentation introduced by
There is no parameter named $ifSonorous. Did you maybe mean $ifSononous?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
238
     * @param string $ifDead Окончание, если слово оканчивается на глухую согласную
0 ignored issues
show
Bug introduced by
There is no parameter named $ifDead. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
239
     * @return string Первое или второе окончание
240
     */
241
    public static function chooseEndingBySonority($word, $ifSononous, $ifDeaf)
242
    {
243
        $last = S::slice($word, -1);
244
        if (self::isSonorousConsonant($last))
245
            return $ifSononous;
246
        if (self::isDeafConsonant($last))
247
            return $ifDeaf;
248
249
        throw new \Exception('Not implemented');
250
    }
251
}
252