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

LastNamesDeclension::isMutable()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 2
Ratio 9.52 %

Importance

Changes 0
Metric Value
nc 7
dl 2
loc 21
c 0
b 0
f 0
cc 8
eloc 15
nop 2
rs 7.1428
1
<?php
2
namespace morphos\Russian;
3
4
/**
5
 * Rules are from http://gramma.ru/SPR/?id=2.8
6
 */
7
class LastNamesDeclension extends \morphos\NamesDeclension implements Cases {
8
    use RussianLanguage, CasesHelper;
9
10
    static protected $menPostfixes = array('ов', 'ев' ,'ин' ,'ын', 'ой', 'ий');
11
    static protected $womenPostfixes = array('ва', 'на', 'ая', 'яя');
12
13
    public function isMutable($name, $gender) {
14
        $name = lower($name);
15
16 View Code Duplication
        if (in_array(slice($name, -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...
17
            return true;
18
19
        if ($gender == self::MAN) {
20
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын', 'ий', 'ой')))
21
                return true;
22
            if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants))
23
                return true;
24
25
            if (slice($name, -1) == 'ь')
26
                return true;
27
        } else {
28
            if (in_array(slice($name, -2), array('ва', 'на')) || in_array(slice($name, -4), array('ская')))
29
                return true;
30
        }
31
32
        return false;
33
    }
34
35
    public function detectGender($name) {
36
        $name = lower($name);
37
        if (in_array(slice($name, -2), self::$menPostfixes))
38
            return self::MAN;
39
        if (in_array(slice($name, -2), self::$womenPostfixes))
40
            return self::WOMAN;
41
42
        return null;
43
    }
44
45
    public function getCases($name, $gender) {
46
        $name = lower($name);
47
        if ($gender == self::MAN) {
48
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын'))) {
49
                $prefix = name($name);
50
                return array(
51
                    self::IMENIT => $prefix,
52
                    self::RODIT => $prefix.'а',
53
                    self::DAT => $prefix.'у',
54
                    self::VINIT => $prefix.'а',
55
                    self::TVORIT => $prefix.'ым',
56
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
57
                );
58
            }
59
            else if (in_array(slice($name, -4), array('ский', 'ской', 'цкий', 'цкой'))) {
60
                $prefix = name(slice($name, 0, -2));
61
                return array(
62
                    self::IMENIT => name($name),
63
                    self::RODIT => $prefix.'ого',
64
                    self::DAT => $prefix.'ому',
65
                    self::VINIT => $prefix.'ого',
66
                    self::TVORIT => $prefix.'им',
67
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ом'
68
                );
69
            }
70
        } else {
71
            if (in_array(slice($name, -3), array('ова', 'ева', 'ина', 'ына'))) {
72
                $prefix = name(slice($name, 0, -1));
73
                return array(
74
                    self::IMENIT => name($name),
75
                    self::RODIT => $prefix.'ой',
76
                    self::DAT => $prefix.'ой',
77
                    self::VINIT => $prefix.'у',
78
                    self::TVORIT => $prefix.'ой',
79
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
80
                );
81
            }
82
83 View Code Duplication
            if (in_array(slice($name, -4), 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...
84
                $prefix = name(slice($name, 0, -2));
85
                return array(
86
                    self::IMENIT => name($name),
87
                    self::RODIT => $prefix.'ой',
88
                    self::DAT => $prefix.'ой',
89
                    self::VINIT => $prefix.'ую',
90
                    self::TVORIT => $prefix.'ой',
91
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
92
                );
93
            }
94
        }
95
96
        if (slice($name, -1) == 'я') {
97
            $prefix = name(slice($name, 0, -1));
98
            return array(
99
                self::IMENIT => name($name),
100
                self::RODIT => $prefix.'и',
101
                self::DAT => $prefix.'е',
102
                self::VINIT => $prefix.'ю',
103
                self::TVORIT => $prefix.'ей',
104
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
105
            );
106
        } else if (slice($name, -1) == 'а') {
107
            $prefix = name(slice($name, 0, -1));
108
            return array(
109
                self::IMENIT => name($name),
110
                self::RODIT => $prefix.'ы',
111
                self::DAT => $prefix.'е',
112
                self::VINIT => $prefix.'у',
113
                self::TVORIT => $prefix.'ой',
114
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
115
            );
116
        } else if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants)) {
117
            $prefix = name($name);
118
            return array(
119
                self::IMENIT => name($name),
120
                self::RODIT => $prefix.'а',
121
                self::DAT => $prefix.'у',
122
                self::VINIT => $prefix.'а',
123
                self::TVORIT => $prefix.'ом',
124
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
125
            );
126 View Code Duplication
        } else if (slice($name, -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...
127
            $prefix = name(slice($name, 0, -1));
128
            return array(
129
                self::IMENIT => name($name),
130
                self::RODIT => $prefix.'я',
131
                self::DAT => $prefix.'ю',
132
                self::VINIT => $prefix.'я',
133
                self::TVORIT => $prefix.'ем',
134
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
135
            );
136
        }
137
138
        $name = name($name);
139
        return array_fill_keys(array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT), $name) + array(self::PREDLOJ => $this->choosePrepositionByFirstLetter($name, 'об', 'о').' '.$name);
140
    }
141
142 View Code Duplication
    public function getCase($name, $case, $gender) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
143
        $case = self::canonizeCase($case);
144
        $forms = $this->getCases($name, $gender);
145
        if ($forms !== false)
146
            if (isset($forms[$case]))
147
                return $forms[$case];
148
            else
149
                return $name;
150
        else
151
            return $name;
152
    }
153
}
154