Completed
Push — master ( dd6397...5d0bef )
by f
01:45
created

functions.php ➔ inflectName()   D

Complexity

Conditions 10
Paths 28

Size

Total Lines 37
Code Lines 29

Duplication

Lines 10
Ratio 27.03 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 28
nop 3
dl 10
loc 37
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\S;
5
6
/**
7
 * Inflects the name to all cases / one case.
8
 * @param string      $fullname Name in format: "L F" o "L M F", where L - last name, M - middl name, F - first name
9
 * @param null|string $case     Case to inflect to. If null, result will contain inflection to all cases.
10
 *                              Should be one of {@link morphos\Cases} or {@link morphos\Russian\Cases} constants.
11
 * @param null|tring  $gender   Gender of name owner. If null, auto detection will be used.
12
 *                              Should be one of {@link morphos\Gender} constants.
13
 * @return string|array         Returns string containing the inflection of name to a case, if `$case` is not null.
14
 *                              Returns an array will inflection to all cases.
15
 */
16
function inflectName($fullname, $case = null, $gender = null)
17
{
18
    if (in_array($case, array('m', 'f'))) {
19
        $gender = $case;
20
        $case = null;
21
    }
22
    if ($gender === null) $gender = detectGender($fullname);
23
    $fullname = normalizeFullName($fullname);
24
25
    $name = explode(' ', $fullname);
26
    if (count($name) < 2 || count($name) > 3) {
27
        return false;
28
    }
29
    if ($case === null) {
30
        $result = array();
0 ignored issues
show
Unused Code introduced by
$result 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...
31
        if (count($name) == 2) {
32
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
33
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
34 View Code Duplication
        } elseif (count($name) == 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...
35
            $name[0] = LastNamesInflection::getCases($name[0], $gender);
36
            $name[1] = FirstNamesInflection::getCases($name[1], $gender);
37
            $name[2] = MiddleNamesInflection::getCases($name[2], $gender);
38
        }
39
        return CasesHelper::composeCasesFromWords($name);
40
    } else {
41
        $case = CasesHelper::canonizeCase($case);
42
        if (count($name) == 2) {
43
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
44
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
45 View Code Duplication
        } elseif (count($name) == 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...
46
            $name[0] = LastNamesInflection::getCase($name[0], $case, $gender);
47
            $name[1] = FirstNamesInflection::getCase($name[1], $case, $gender);
48
            $name[2] = MiddleNamesInflection::getCase($name[2], $case, $gender);
49
        }
50
    	return implode(' ', $name);
51
    }
52
}
53
54
/**
55
 * Guesses the gender of name owner.
56
 * @param string $fullname
57
 * @return null|string     Null if not detected. One of {@link morphos\Gender} constants.
58
 */
59
function detectGender($fullname)
60
{
61
    static $first, $middle, $last;
62
    $name = explode(' ', S::lower($fullname));
63
    if (count($name) < 2 || count($name) > 3) {
64
        return false;
65
    }
66
67
    return (isset($name[2]) ? MiddleNamesInflection::detectGender($name[2]) : null) ?:
68
        LastNamesInflection::detectGender($name[0]) ?:
69
        FirstNamesInflection::detectGender($name[1]);
70
}
71
72
/**
73
 * Normalizes a full name. Swaps name parts to make "L F" or "L M F" scheme.
74
 * @param string $name Input name
75
 * @return string      Normalized name
76
 */
77
function normalizeFullName($name)
78
{
79
    $name = preg_replace('~[ ]{2,}~', null, trim($name));
80
    return $name;
81
}
82
83
/*
84
 * @param string $word
85
 * @param int $count
86
 * @param bool $animateness
87
 * @return string
88
 */
89
function pluralize($count, $word, $animateness = false)
90
{
91
    return $count.' '.NounPluralization::pluralize($word, $count, $animateness);
92
}
93