Test Setup Failed
Push — master ( e2ebce...430989 )
by Gabriel
02:55
created

InflectionPluralizeTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 3.7 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 5
loc 135
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B doPluralize() 5 24 7

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
3
namespace Nip\Inflector\Traits;
4
5
/**
6
 * Trait InflectionPluralizeTrait
7
 * @package Nip\Inflector\Traits
8
 *
9
 * @method pluralize($string)
10
 */
11
trait InflectionPluralizeTrait
12
{
13
    protected $plural = [
14
        '/(s)tatus$/i' => '\1tatuses',
15
        '/(quiz)$/i' => '\1zes',
16
        '/^(ox)$/i' => '\1\2en',
17
        '/([m|l])ouse$/i' => '\1ice',
18
        '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
19
        '/(x|ch|ss|sh)$/i' => '\1es',
20
        '/([^aeiouy]|qu)y$/i' => '\1ies',
21
        '/(hive)$/i' => '\1s',
22
        '/(chef)$/i' => '\1s',
23
        '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
24
        '/sis$/i' => 'ses',
25
        '/([ti])um$/i' => '\1a',
26
        '/(p)erson$/i' => '\1eople',
27
        '/(?<!u)(m)an$/i' => '\1en',
28
        '/(c)hild$/i' => '\1hildren',
29
        '/(buffal|tomat)o$/i' => '\1\2oes',
30
        '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i',
31
        '/us$/i' => 'uses',
32
        '/(alias)$/i' => '\1es',
33
        '/(ax|cris|test)is$/i' => '\1es',
34
        '/s$/' => 's',
35
        '/^$/' => '',
36
        '/$/' => 's',
37
    ];
38
39
    protected $uncountable = [
40
        '.*[nrlm]ese',
41
        '.*data',
42
        '.*deer',
43
        '.*fish',
44
        '.*measles',
45
        '.*ois',
46
        '.*pox',
47
        '.*sheep',
48
        'people',
49
        'feedback',
50
        'stadia',
51
        '.*?media',
52
        'chassis',
53
        'clippers',
54
        'debris',
55
        'diabetes',
56
        'equipment',
57
        'gallows',
58
        'graffiti',
59
        'headquarters',
60
        'information',
61
        'innings',
62
        'news',
63
        'nexus',
64
        'pokemon',
65
        'proceedings',
66
        'research',
67
        'sea[- ]bass',
68
        'series',
69
        'species',
70
        'weather'
71
    ];
72
73
    protected $irregular = [
74
        'atlas' => 'atlases',
75
        'beef' => 'beefs',
76
        'brief' => 'briefs',
77
        'brother' => 'brothers',
78
        'cafe' => 'cafes',
79
        'child' => 'children',
80
        'cookie' => 'cookies',
81
        'corpus' => 'corpuses',
82
        'cow' => 'cows',
83
        'criterion' => 'criteria',
84
        'ganglion' => 'ganglions',
85
        'genie' => 'genies',
86
        'genus' => 'genera',
87
        'graffito' => 'graffiti',
88
        'hoof' => 'hoofs',
89
        'loaf' => 'loaves',
90
        'man' => 'men',
91
        'money' => 'monies',
92
        'mongoose' => 'mongooses',
93
        'move' => 'moves',
94
        'mythos' => 'mythoi',
95
        'niche' => 'niches',
96
        'numen' => 'numina',
97
        'occiput' => 'occiputs',
98
        'octopus' => 'octopuses',
99
        'opus' => 'opuses',
100
        'ox' => 'oxen',
101
        'penis' => 'penises',
102
        'person' => 'people',
103
        'sex' => 'sexes',
104
        'soliloquy' => 'soliloquies',
105
        'testis' => 'testes',
106
        'trilby' => 'trilbys',
107
        'turf' => 'turfs',
108
        'potato' => 'potatoes',
109
        'hero' => 'heroes',
110
        'tooth' => 'teeth',
111
        'goose' => 'geese',
112
        'foot' => 'feet',
113
        'foe' => 'foes',
114
        'sieve' => 'sieves'
115
    ];
116
117
    /**
118
     * @param $word
119
     * @return bool|mixed
120
     */
121 1
    protected function doPluralize($word)
122
    {
123 1
        $lowerCased_word = strtolower($word);
124
125 1
        foreach ($this->uncountable as $_uncountable) {
126 1
            if (substr($lowerCased_word, (-1 * strlen($_uncountable))) == $_uncountable) {
127
                return $word;
128
            }
129
        }
130
131 1 View Code Duplication
        foreach ($this->irregular as $_plural => $_singular) {
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...
132 1
            if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) {
133 1
                return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word);
134
            }
135
        }
136
137 1
        foreach ($this->plural as $rule => $replacement) {
138 1
            if (preg_match($rule, $word)) {
139 1
                return preg_replace($rule, $replacement, $word);
140
            }
141
        }
142
143
        return false;
144
    }
145
}
146