Completed
Push — master ( e602f5...349893 )
by f
01:55
created

functions.php ➔ detectGender()   D

Complexity

Conditions 10
Paths 19

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 15.8818

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 19
nop 1
dl 0
loc 26
ccs 11
cts 18
cp 0.6111
crap 15.8818
rs 4.8196
c 0
b 0
f 0

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