Completed
Push — master ( 6f4e22...98401f )
by f
01:50
created

GeographicalNamesDeclension::getCases()   D

Complexity

Conditions 22
Paths 26

Size

Total Lines 107
Code Lines 77

Duplication

Lines 23
Ratio 21.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 22
eloc 77
c 1
b 0
f 0
nc 26
nop 1
dl 23
loc 107
rs 4.6625

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
use morphos\S;
5
6
/**
7
 * Rules are from: https://ru.wikipedia.org/wiki/%D0%A1%D0%BA%D0%BB%D0%BE%D0%BD%D0%B5%D0%BD%D0%B8%D0%B5_%D0%B3%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B8%D1%85_%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D0%B9_%D0%B2_%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%BC_%D1%8F%D0%B7%D1%8B%D0%BA%D0%B5
8
 */
9
class GeographicalNamesDeclension extends \morphos\GeneralDeclension implements Cases {
10
    use RussianLanguage, CasesHelper;
11
12
    static protected $abbreviations = array(
13
        'сша',
14
        'оаэ',
15
        'ссср',
16
        'юар',
17
    );
18
19
    static public function isMutable($name) {
20
        $name = S::lower($name);
21
        // // ends with 'ы' or 'и': plural form
22
        // if (in_array(S::slice($name, -1), array('и', 'ы')))
23
        //     return false;
24
        if (in_array($name, self::$abbreviations))
25
            return false;
26
        // ends with 'е' or 'о', but not with 'ово/ёво/ево/ино/ыно'
27
        if (in_array(S::slice($name, -1), array('е', 'о')) && !in_array(S::slice($name, -3, -1), array('ов', 'ёв', 'ев', 'ин', 'ын')))
28
            return false;
29
        return true;
30
    }
31
32
    static public function getCases($name) {
33
        $name = S::lower($name);
34
35
        // check for name of two words
36
        if (strpos($name, ' ') !== false) {
37
            $parts = explode(' ', $name);
38
            $cases = array();
0 ignored issues
show
Unused Code introduced by
$cases is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
            $result = array();
40
            foreach ($parts as $i => $part) {
41
                $result[$i] = static::getCases($part);
42
                if ($i > 0)
43
                    $result[$i][self::PREDLOJ] = substr(strstr($result[$i][self::PREDLOJ], ' '), 1);
44
            }
45
46
            $cases = array();
47
            foreach (array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT, self::PREDLOJ) as $case) {
48
                foreach ($parts as $i => $part) {
49
                    $cases[$case][] = $result[$i][$case];
50
                }
51
                $cases[$case] = implode(' ', $cases[$case]);
52
            }
53
            return $cases;
54
        }
55
56
        if (!in_array($name, self::$abbreviations)) {
57
            if (S::slice($name, -2) == 'ий') {
58
                // Нижний, Русский
59
                $prefix = S::name(S::slice($name, 0, -2));
60
                return array(
61
                    self::IMENIT => $prefix.'ий',
62
                    self::RODIT => $prefix.(self::isVelarConsonant(S::slice($name, -3, -2)) ? 'ого' : 'его'),
63
                    self::DAT => $prefix.(self::isVelarConsonant(S::slice($name, -3, -2)) ? 'ому' : 'ему'),
64
                    self::VINIT => $prefix.'ий',
65
                    self::TVORIT => $prefix.'им',
66
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ем',
67
                );
68
            } else if (S::slice($name, -1) == 'а') {
69
                // Москва, Рига
70
                $prefix = S::name(S::slice($name, 0, -1));
71
                return array(
72
                    self::IMENIT => $prefix.'а',
73
                    self::RODIT => $prefix.(self::isVelarConsonant(S::slice($name, -2, -1)) ? 'и' : 'ы'),
74
                    self::DAT => $prefix.'е',
75
                    self::VINIT => $prefix.'у',
76
                    self::TVORIT => $prefix.'ой',
77
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е',
78
                );
79
            } else if (S::slice($name, -1) == 'я') {
80
                // Азия
81
                $prefix = S::name(S::slice($name, 0, -1));
82
                return array(
83
                    self::IMENIT => S::name($name),
84
                    self::RODIT => $prefix.'и',
85
                    self::DAT => $prefix.'и',
86
                    self::VINIT => $prefix.'ю',
87
                    self::TVORIT => $prefix.'ей',
88
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'и',
89
                );
90 View Code Duplication
            } else if (S::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...
91
                // Ишимбай
92
                $prefix = S::name(S::slice($name, 0, -1));
93
                return array(
94
                    self::IMENIT => $prefix.'й',
95
                    self::RODIT => $prefix.'я',
96
                    self::DAT => $prefix.'ю',
97
                    self::VINIT => $prefix.'й',
98
                    self::TVORIT => $prefix.'ем',
99
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е',
100
                );
101
            } else if (self::isConsonant(S::slice($name, -1))) {
102
                $prefix = S::name($name);
103
                // Париж, Валаам, Киев
104
                return array(
105
                    self::IMENIT => $prefix,
106
                    self::RODIT => $prefix.'а',
107
                    self::DAT => $prefix.'у',
108
                    self::VINIT => $prefix,
109
                    self::TVORIT => $prefix.(self::isVelarConsonant(S::slice($name, -2, -1)) ? 'ем' : 'ом'),
110
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е',
111
                );
112
            }
113
114
            $suffixes = array('ов', 'ёв', 'ев', 'ин', 'ын');
115
            if ((in_array(S::slice($name, -1), array('е', 'о')) && in_array(S::slice($name, -3, -1), $suffixes)) || in_array(S::slice($name, -2), $suffixes)) {
116
                // ово, ёво, ...
117
                if (in_array(S::slice($name, -3, -1), $suffixes)) {
118
                    $prefix = S::name(S::slice($name, 0, -1));
119
                }
120
                // ов, её, ...
121
                else if (in_array(S::slice($name, -2), $suffixes)) {
122
                    $prefix = S::name($name);
123
                }
124
                return array(
125
                    self::IMENIT => S::name($name),
126
                    self::RODIT => $prefix.'а',
127
                    self::DAT => $prefix.'у',
128
                    self::VINIT => S::name($name),
129
                    self::TVORIT => $prefix.'ем',
130
                    self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е',
131
                );
132
            }
133
        }
134
135
        // if no rules matches or name is immutable
136
        $name = in_array($name, self::$abbreviations) ? S::upper($name) : S::name($name);
137
        return array_fill_keys(array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT), $name) + array(self::PREDLOJ => self::choosePrepositionByFirstLetter($name, 'об', 'о').' '.$name);
138
    }
139
140
    static public function getCase($name, $case) {
141
        $case = self::canonizeCase($case);
142
        $forms = self::getCases($name);
143
        return $forms[$case];
144
    }
145
}