Completed
Push — 2.1.x ( 7ab7e8 )
by f
23:20
created

LastNamesDeclension::getForms()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 94
Code Lines 76

Duplication

Lines 44
Ratio 46.81 %

Importance

Changes 0
Metric Value
cc 10
eloc 76
nc 14
nop 2
dl 44
loc 94
rs 5.017
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://gramma.ru/SPR/?id=2.8
6
 */
7
class LastNamesDeclension extends \morphos\NamesDeclension implements Cases {
8
    use RussianLanguage;
9
10
    public function hasForms($name, $gender) {
11
        $name = lower($name);
12
        if ($gender == self::MAN) {
13
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын')) || in_array(slice($name, -4), array('ский', 'ской', 'цкий', 'цкой')))
14
                return true;
15
            if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants))
16
                return true;
17
18
            if (slice($name, -1) == 'ь')
19
                return true;
20
        } else {
21
            if (in_array(slice($name, -3), array('ова', 'ева', 'ина', 'ына')) || in_array(slice($name, -4), array('ская')))
22
                return true;
23
        }
24
25
        if (in_array(slice($name, -1), array('а', 'я')))
26
            return true;
27
28
        return false;
29
    }
30
31
    public function getForms($name, $gender) {
32
        $name = lower($name);
33
        if ($gender == self::MAN) {
34
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын'))) {
35
                $prefix = name($name);
36
                return array(
37
                    self::IMENIT_1 => $prefix,
38
                    self::RODIT_2 => $prefix.'а',
39
                    self::DAT_3 => $prefix.'у',
40
                    self::VINIT_4 => $prefix.'а',
41
                    self::TVORIT_5 => $prefix.'ым',
42
                    self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
43
                );
44
            }
45
46 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...
47
                $prefix = name(slice($name, 0, -2));
48
                return array(
49
                    self::IMENIT_1 => name($name),
50
                    self::RODIT_2 => $prefix.'ого',
51
                    self::DAT_3 => $prefix.'ому',
52
                    self::VINIT_4 => $prefix.'ого',
53
                    self::TVORIT_5 => $prefix.'им',
54
                    self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ом'
55
                );
56
            }
57
        } else {
58 View Code Duplication
            if (in_array(slice($name, -3), 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...
59
                $prefix = name(slice($name, 0, -1));
60
                return array(
61
                    self::IMENIT_1 => name($name),
62
                    self::RODIT_2 => $prefix.'ой',
63
                    self::DAT_3 => $prefix.'ой',
64
                    self::VINIT_4 => $prefix.'у',
65
                    self::TVORIT_5 => $prefix.'ой',
66
                    self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
67
                );
68
            }
69
70 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...
71
                $prefix = name(slice($name, 0, -2));
72
                return array(
73
                    self::IMENIT_1 => name($name),
74
                    self::RODIT_2 => $prefix.'ой',
75
                    self::DAT_3 => $prefix.'ой',
76
                    self::VINIT_4 => $prefix.'ую',
77
                    self::TVORIT_5 => $prefix.'ой',
78
                    self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
79
                );
80
            }
81
        }
82
83
        if (slice($name, -1) == 'я') {
84
            $prefix = name(slice($name, 0, -1));
85
            return array(
86
                self::IMENIT_1 => name($name),
87
                self::RODIT_2 => $prefix.'и',
88
                self::DAT_3 => $prefix.'е',
89
                self::VINIT_4 => $prefix.'ю',
90
                self::TVORIT_5 => $prefix.'ей',
91
                self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
92
            );
93
        } else if (slice($name, -1) == 'а') {
94
            $prefix = name(slice($name, 0, -1));
95
            return array(
96
                self::IMENIT_1 => name($name),
97
                self::RODIT_2 => $prefix.'ы',
98
                self::DAT_3 => $prefix.'е',
99
                self::VINIT_4 => $prefix.'у',
100
                self::TVORIT_5 => $prefix.'ой',
101
                self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
102
            );
103
        } else if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants)) {
104
            $prefix = name($name);
105
            return array(
106
                self::IMENIT_1 => name($name),
107
                self::RODIT_2 => $prefix.'а',
108
                self::DAT_3 => $prefix.'у',
109
                self::VINIT_4 => $prefix.'а',
110
                self::TVORIT_5 => $prefix.'ом',
111
                self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
112
            );
113 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...
114
            $prefix = name(slice($name, 0, -1));
115
            return array(
116
                self::IMENIT_1 => name($name),
117
                self::RODIT_2 => $prefix.'я',
118
                self::DAT_3 => $prefix.'ю',
119
                self::VINIT_4 => $prefix.'я',
120
                self::TVORIT_5 => $prefix.'ем',
121
                self::PREDLOJ_6 => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
122
            );
123
        }
124
    }
125
126 View Code Duplication
    public function getForm($name, $form, $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...
127
        $forms = $this->getForms($name, $gender);
128
        if ($forms !== false)
129
            if (isset($forms[$form]))
130
                return $forms[$form];
131
            else
132
                return false;
133
        else
134
            return false;
135
    }
136
}
137