Completed
Push — master ( 03367b...e141e7 )
by f
01:43
created

functions.php ➔ name()   C

Complexity

Conditions 15
Paths 76

Size

Total Lines 42
Code Lines 34

Duplication

Lines 10
Ratio 23.81 %

Importance

Changes 0
Metric Value
cc 15
eloc 34
nc 76
nop 3
dl 10
loc 42
rs 5.0504
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
function name($fullname, $case = null, $gender = null) {
7
    if (in_array($case, array('m', 'w'))) {
8
        $gender = $case;
9
        $case = null;
10
    }
11
    if ($gender === null) $gender = detectGender($fullname);
12
13
    $name = explode(' ', $fullname);
14
    if ($case === null) {
15
        $result = array();
16
        if (count($name) == 2) {
17
            $name[0] = LastNamesDeclension::getCases($name[0], $gender);
18
            $name[1] = FirstNamesDeclension::getCases($name[1], $gender);
19
        } else if (count($name) == 3) {
20
            $name[0] = LastNamesDeclension::getCases($name[0], $gender);
21
            $name[1] = FirstNamesDeclension::getCases($name[1], $gender);
22
            $name[2] = MiddleNamesDeclension::getCases($name[2], $gender);
23
        }
24 View Code Duplication
        foreach (array(Cases::IMENIT, Cases::RODIT, Cases::DAT, Cases::VINIT, Cases::TVORIT, Cases::PREDLOJ) as $case) {
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...
25
            foreach ($name as $partNum => $namePart) {
26
                if ($case == Cases::PREDLOJ && $partNum > 0) list(, $namePart[$case]) = explode(' ', $namePart[$case]);
27
                $result[$case][] = $namePart[$case];
28
            }
29
            $result[$case] = implode(' ', $result[$case]);
30
        }
31
        return $result;
32
    } else {
33
        $case = CasesHelper::canonizeCase($case);
34
        if (count($name) == 2) {
35
            $name[0] = LastNamesDeclension::getCase($name[0], $case, $gender);
36
            $name[1] = FirstNamesDeclension::getCase($name[1], $case, $gender);
37 View Code Duplication
            if ($case == Cases::PREDLOJ) list(, $name[1]) = explode(' ', $name[1]);
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
        } else if (count($name) == 3) {
39
            $name[0] = LastNamesDeclension::getCase($name[0], $case, $gender);
40
            $name[1] = FirstNamesDeclension::getCase($name[1], $case, $gender);
41 View Code Duplication
            if ($case == Cases::PREDLOJ) list(, $name[1]) = explode(' ', $name[1]);
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...
42
            $name[2] = MiddleNamesDeclension::getCase($name[2], $case, $gender);
43 View Code Duplication
            if ($case == Cases::PREDLOJ) list(, $name[2]) = explode(' ', $name[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...
44
        }
45
    }
46
    return implode(' ', $name);
47
}
48
49
function detectGender($fullname) {
50
    static $first, $middle, $last;
51
    $name = explode(' ', S::lower($fullname));
52
53
    return (isset($name[2]) ? MiddleNamesDeclension::detectGender($name[2]) : null) ?:
54
        FirstNamesDeclension::detectGender($name[1]) ?:
55
        LastNamesDeclension::detectGender($name[0]);
56
}
57
58
function pluralize($word, $count = 2, $animateness = false) {
59
    return Plurality::pluralize($word, $count, $animateness);
60
}
61