Completed
Push — master ( 3a7b5a...01b6b1 )
by f
01:20
created

functions.php ➔ inflectName()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 38
Code Lines 25

Duplication

Lines 9
Ratio 23.68 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 10
nop 3
dl 9
loc 38
rs 6.7272
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                 Returns string containing the inflection of name to a case.
15
 */
16
function inflectName($fullName, $case = null, $gender = null)
17
{
18
    if ($case === null) {
19
        return getNameCases($fullName);
20
    }
21
22
    if (in_array($case, [Gender::MALE, Gender::FEMALE], true)) {
23
        return getNameCases($fullName, $case);
24
    }
25
26
    $fullName = normalizeFullName($fullName);
27
    if ($gender === null) $gender = detectGender($fullName);
28
29
    $name = explode(' ', $fullName);
30
    $case = CasesHelper::canonizeCase($case);
31
32
    switch (count($name)) {
33
        case 1:
34
            $name[0] = FirstNamesInflection::getCase($name[0], $case, $gender);
35
            break;
36
37 View Code Duplication
        case 2:
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...
38
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
39
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
40
            break;
41
42 View Code Duplication
        case 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...
43
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
44
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
45
            $name[2] = MiddleNamesInflection::getCase($name[2], $case, $gender);
46
            break;
47
48
        default:
49
            return false;
50
    }
51
52
    return implode(' ', $name);
53
}
54
55
/**
56
 * Inflects the name to all cases.
57
 * @param string      $fullName Name in "F", "L F" or "L M F" format, where L - last name, M - middle name, F - first name
58
 * @param null|string $gender   Gender of name owner. If null, auto detection will be used.
59
 *                              Should be one of [[morphos\Gender]] constants.
60
 * @return array                Returns an array with name inflected to all cases.
61
 */
62
function getNameCases($fullName, $gender = null)
63
{
64
    $fullName = normalizeFullName($fullName);
65
    if ($gender === null) $gender = detectGender($fullName);
66
67
    $name = explode(' ', $fullName);
68
69
    switch (count($name)) {
70
        case 1:
71
            $name[0] = FirstNamesInflection::getCases($name[0], $gender);
72
            break;
73
74 View Code Duplication
        case 2:
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...
75
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
76
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
77
            break;
78
79 View Code Duplication
        case 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...
80
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
81
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
82
            $name[2] = MiddleNamesInflection::getCases($name[2], $gender);
83
            break;
84
85
        default:
86
            return false;
87
    }
88
89
    return CasesHelper::composeCasesFromWords($name);
90
}
91
92
/**
93
 * Guesses the gender of name owner.
94
 * @param string $fullName
95
 * @return null|string     Null if not detected. One of [[morphos\Gender]] constants.
96
 */
97
function detectGender($fullName)
98
{
99
    $gender = null;
100
    $name = explode(' ', S::lower($fullName));
101
    $nameCount = count($name);
102
    if (!in_array($nameCount, [2, 3], true)) {
103
        return false;
104
    }
105
    if ($nameCount === 3) {
106
        $gender = detectGender(implode(' ', [$name[1], $name[2]]));
107
    }
108
109
    if (!$gender) {
110
        $gender = (isset($name[2]) ? MiddleNamesInflection::detectGender($name[2]) : null) ?:
111
            LastNamesInflection::detectGender($name[0]) ?:
112
                FirstNamesInflection::detectGender($name[1]);
113
    }
114
115
    return $gender;
116
}
117
118
/**
119
 * Normalizes a full name. Swaps name parts to make "L F" or "L M F" scheme.
120
 * @param string $name Input name
121
 * @return string      Normalized name
122
 */
123
function normalizeFullName($name)
124
{
125
    $name = preg_replace('~[ ]{2,}~', null, trim($name));
126
    return $name;
127
}
128
129
/**
130
 * Генерация строки с числом и существительным, в правильной форме для сочетания с числом (кол-вом предметов).
131
 * @param int $count Количество предметов
132
 * @param string $word Название предмета
133
 * @param bool $animateness Признак одушевленности
134
 * @return string Строка в формате "ЧИСЛО [СУЩ в правильно форме]"
135
 */
136
function pluralize($count, $word, $animateness = false)
137
{
138
    // меняем местами аргументы, если они переданы в старом формате
139 View Code Duplication
    if (is_string($count) && is_numeric($word)) {
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...
140
        list($count, $word) = [$word, $count];
141
    }
142
    return $count.' '.NounPluralization::pluralize($count, $word, $animateness);
143
}
144