Completed
Push — master ( b5bb9d...38f7c0 )
by f
02:02
created

src/Russian/functions.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
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
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
$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
 * @param int $count Количество предметов
128
 * @param string $word Название предмета
129
 * @param bool $animateness Признак одушевленности
130
 * @return string Строка в формате "ЧИСЛО [СУЩ в правильной форме]"
131
 * @throws \Exception
132
 */
133
function pluralize($count, $word, $animateness = false)
134
{
135
    // меняем местами аргументы, если они переданы в старом формате
136 15 View Code Duplication
    if (is_string($count) && is_numeric($word)) {
0 ignored issues
show
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...
137 1
        list($count, $word) = [$word, $count];
138
    }
139 15
    return $count.' '.NounPluralization::pluralize($count, $word, $animateness);
140
}
141