Completed
Push — master ( 4b1809...5e3fcd )
by f
01:31
created

functions.php ➔ pluralize()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 23

Duplication

Lines 3
Ratio 13.04 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 4
dl 3
loc 23
ccs 10
cts 11
cp 0.9091
crap 6.027
rs 8.9297
c 0
b 0
f 0
1
<?php
2
namespace morphos\Russian;
3
4
use morphos\Gender;
5
use morphos\S;
6
7
/**
8
 * Inflects the name in one case.
9
 * @param string $fullName        Name in "F", "L F" or "L M F" format, where L - last name, M - middle name, F - first name
10
 * @param string $case            Case to inflect to.
11
 *                                Should be one of [[morphos\Cases]] or [[morphos\Russian\Cases]] constants.
12
 * @param null|string $gender     Gender of name owner. If null, auto detection will be used.
13
 *                                Should be one of [[morphos\Gender]] constants.
14
 * @return string|false           Returns string containing the inflection of name to a case.
15
 * @throws \Exception
16
 */
17
function inflectName($fullName, $case = null, $gender = null)
18
{
19 18
    if (in_array($case, [Gender::MALE, Gender::FEMALE, null], true)) {
20 10
        return $case === null ? getNameCases($fullName) : getNameCases($fullName, $case);
21
    }
22
23 8
    $fullName = normalizeFullName($fullName);
24 8
    $case = CasesHelper::canonizeCase($case);
25 8
    if ($gender === null) $gender = detectGender($fullName);
26
27 8
    $name = explode(' ', $fullName);
28
29 8
    switch (count($name)) {
30 8
        case 1:
31 1
            $name[0] = FirstNamesInflection::getCase($name[0], $case, $gender);
32 1
            break;
33
34 7 View Code Duplication
        case 2:
0 ignored issues
show
Duplication introduced by wapmorgan
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...
35 1
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
36 1
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
37 1
            break;
38
39 6 View Code Duplication
        case 3:
0 ignored issues
show
Duplication introduced by wapmorgan
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...
40 5
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
41 5
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
42 5
            $name[2] = MiddleNamesInflection::getCase($name[2], $case, $gender);
43 5
            break;
44
45
        default:
46 1
            return false;
47
    }
48
49 7
    return implode(' ', $name);
50
}
51
52
/**
53
 * Inflects the name to all cases.
54
 * @param string      $fullName Name in "F", "L F" or "L M F" format, where L - last name, M - middle name, F - first name
55
 * @param null|string $gender   Gender of name owner. If null, auto detection will be used.
56
 *                              Should be one of [[morphos\Gender]] constants.
57
 * @return array|false          Returns an array with name inflected to all cases.
58
 */
59
function getNameCases($fullName, $gender = null)
60
{
61 11
    $fullName = normalizeFullName($fullName);
62 11
    if ($gender === null) $gender = detectGender($fullName);
63
64 11
    $name = explode(' ', $fullName);
65
66 11
    switch (count($name)) {
67 11
        case 1:
68 1
            $name[0] = FirstNamesInflection::getCases($name[0], $gender);
69 1
            break;
70
71 10 View Code Duplication
        case 2:
0 ignored issues
show
Duplication introduced by wapmorgan
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...
72 1
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
73 1
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
74 1
            break;
75
76 9 View Code Duplication
        case 3:
0 ignored issues
show
Duplication introduced by wapmorgan
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...
77 8
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
78 8
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
79 8
            $name[2] = MiddleNamesInflection::getCases($name[2], $gender);
80 8
            break;
81
82
        default:
83 1
            return false;
84
    }
85
86 10
    return CasesHelper::composeCasesFromWords($name);
87
}
88
89
/**
90
 * Guesses the gender of name owner.
91
 * @param string $fullName Name in "F", "L F" or "L M F" format, where L - last name, M - middle name, F - first name
92
 * @return null|string     Null if not detected. One of [[morphos\Gender]] constants.
93
 */
94
function detectGender($fullName)
95
{
96 2
    $gender = null;
0 ignored issues
show
Unused Code introduced by Evgeniy Prokhorov
$gender 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...
97 2
    $name = explode(' ', S::lower($fullName));
98 2
    $nameCount = count($name);
99 2
    if ($nameCount > 3) {
100 1
        return null;
101
    }
102
103 1
    if ($nameCount === 1)
104 1
        return FirstNamesInflection::detectGender($name[0]);
105 1
    else if ($nameCount === 2)
106 1
        return LastNamesInflection::detectGender($name[0])
107 1
            ?: FirstNamesInflection::detectGender($name[1]);
108
    else
109 1
        return MiddleNamesInflection::detectGender($name[2])
110
            ?: (LastNamesInflection::detectGender($name[0])
111 1
                ?: FirstNamesInflection::detectGender($name[1]));
112
}
113
114
/**
115
 * Normalizes a full name. Swaps name parts to make "L F" or "L M F" scheme.
116
 * @param string $name Input name
117
 * @return string      Normalized name
118
 */
119
function normalizeFullName($name)
120
{
121 18
    $name = preg_replace('~[ ]{2,}~', null, trim($name));
122 18
    return $name;
123
}
124
125
/**
126
 * Генерация строки с числом и существительным, в правильной форме для сочетания с числом (кол-вом предметов).
127
 *
128
 * @param int    $count       Количество предметов
129
 * @param string $word        Название предмета. Может включать в себя несколько прилагательных перед существительным.
130
 *                            Например: сообщение, новое сообщение, небольшая лампа.
131
 * @param bool   $animateness Признак одушевленности
132
 * @param string $case        Род существительного (по умолчанию именительный)
133
 *
134
 * @return string Строка в формате "ЧИСЛО [СУЩ в правильной форме]"
135
 * @throws \Exception
136
 */
137
function pluralize($count, $word, $animateness = false, $case = null)
138
{
139
    // меняем местами аргументы, если они переданы в старом формате
140 15 View Code Duplication
    if (is_string($count) && is_numeric($word)) {
0 ignored issues
show
Duplication introduced by wapmorgan
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...
141 1
        list($count, $word) = [$word, $count];
142
    }
143
144 15
    if (strpos($word, ' ') !== false) {
145 1
        $words = explode(' ', $word);
146 1
        $noun = array_pop($words);
147
148 1
        foreach ($words as $i => $word) {
149 1
            if (in_array($word, RussianLanguage::$unions, true))
150
                $words[$i] = $word;
151
            else
152 1
                $words[$i] = AdjectivePluralization::pluralize($word, $count, $animateness, $case);
0 ignored issues
show
Bug introduced by wapmorgan
It seems like $case defined by parameter $case on line 137 can also be of type string; however, morphos\Russian\Adjectiv...ralization::pluralize() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
153
        }
154
155 1
        return $count.' '.implode(' ', $words).' '.NounPluralization::pluralize($count, $noun, $animateness, $case);
156
    }
157
158 15
    return $count.' '.NounPluralization::pluralize($word, $count, $animateness, $case);
159
}
160