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

MiddleNamesInflection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 16.47 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
dl 14
loc 85
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detectGender() 0 11 3
A isMutable() 3 10 2
A getCase() 0 6 1
A getCases() 11 29 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace morphos\Russian;
3
4
use morphos\S;
5
6
/**
7
 * Rules are from http://surnameonline.ru/patronymic.html
8
 */
9
class MiddleNamesInflection extends \morphos\NamesInflection implements Cases
10
{
11
    use RussianLanguage, CasesHelper;
12
13
    /**
14
     * @param $name
15
     * @return null|string
16
     */
17 5
    public static function detectGender($name)
18
    {
19 5
        $name = S::lower($name);
20 5
        if (S::slice($name, -2) == 'ич') {
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 19 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
21 3
            return self::MALE;
22 3
        } elseif (S::slice($name, -2) == 'на') {
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 19 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
23 3
            return self::FEMALE;
24
        }
25
26
        return null;
27
    }
28
29
    /**
30
     * @param $name
31
     * @param null $gender
32
     * @return bool
33
     */
34 2
    public static function isMutable($name, $gender = null)
35
    {
36 2
        $name = S::lower($name);
37 2 View Code Duplication
        if (in_array(S::slice($name, -2), ['ич', 'на'], true)) {
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...
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 36 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38 2
            return true;
39
        }
40
41
        // it's foreign middle name, inflect it as a first name
42
        return FirstNamesInflection::isMutable($name, $gender);
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 36 can also be of type boolean; however, morphos\Russian\FirstNamesInflection::isMutable() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
    }
44
45
    /**
46
     * @param $name
47
     * @param $case
48
     * @param null $gender
49
     * @return mixed
50
     * @throws \Exception
51
     */
52 9
    public static function getCase($name, $case, $gender = null)
53
    {
54 9
        $case = self::canonizeCase($case);
55 9
        $forms = self::getCases($name, $gender);
56 9
        return $forms[$case];
57
    }
58
59
    /**
60
     * @param $name
61
     * @param null $gender
62
     * @return array
63
     */
64 21
    public static function getCases($name, $gender = null)
65
    {
66 21
        $name = S::lower($name);
67 21
        if (S::slice($name, -2) == 'ич') {
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 66 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
            // man rules
69 8
            $name = S::name($name);
70
            return array(
71 8
                Cases::IMENIT => $name,
72 8
                Cases::RODIT => $name.'а',
73 8
                Cases::DAT => $name.'у',
74 8
                Cases::VINIT => $name.'а',
75 8
                Cases::TVORIT => $name.'ем',
76 8
                Cases::PREDLOJ => $name.'е',
77
            );
78 13 View Code Duplication
        } elseif (S::slice($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...
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 66 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79 11
            $prefix = S::name(S::slice($name, 0, -1));
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 66 can also be of type boolean; however, morphos\S::slice() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
            return array(
81 11
                Cases::IMENIT => $prefix.'а',
82 11
                Cases::RODIT => $prefix.'ы',
83 11
                Cases::DAT => $prefix.'е',
84 11
                Cases::VINIT => $prefix.'у',
85 11
                Cases::TVORIT => $prefix.'ой',
86 11
                Cases::PREDLOJ => $prefix.'е',
87
            );
88
        }
89
90
        // inflect other middle names (foreign) as first names
91 2
        return FirstNamesInflection::getCases($name, $gender);
0 ignored issues
show
Bug introduced by
It seems like $name defined by \morphos\S::lower($name) on line 66 can also be of type boolean; however, morphos\Russian\FirstNamesInflection::getCases() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
92
    }
93
}
94