Completed
Push — master ( 0a1b5a...13fc78 )
by Edson
01:55
created

Inflect   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 6
eloc 51
dl 0
loc 72
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A pluralize() 0 24 6
1
<?php
2
3
namespace Bonfim\ActiveRecord;
4
5
// original source: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
6
7
class Inflect
8
{
9
    private static $plural = array(
10
        '/(quiz)$/i'               => "$1zes",
11
        '/^(ox)$/i'                => "$1en",
12
        '/([m|l])ouse$/i'          => "$1ice",
13
        '/(matr|vert|ind)ix|ex$/i' => "$1ices",
14
        '/(x|ch|ss|sh)$/i'         => "$1es",
15
        '/([^aeiouy]|qu)y$/i'      => "$1ies",
16
        '/(hive)$/i'               => "$1s",
17
        '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
18
        '/(shea|lea|loa|thie)f$/i' => "$1ves",
19
        '/sis$/i'                  => "ses",
20
        '/([ti])um$/i'             => "$1a",
21
        '/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
22
        '/(bu)s$/i'                => "$1ses",
23
        '/(alias)$/i'              => "$1es",
24
        '/(octop)us$/i'            => "$1i",
25
        '/(ax|test)is$/i'          => "$1es",
26
        '/(us)$/i'                 => "$1es",
27
        '/s$/i'                    => "s",
28
        '/$/'                      => "s"
29
    );
30
31
    private static $irregular = array(
32
        'move'   => 'moves',
33
        'foot'   => 'feet',
34
        'goose'  => 'geese',
35
        'sex'    => 'sexes',
36
        'child'  => 'children',
37
        'man'    => 'men',
38
        'tooth'  => 'teeth',
39
        'person' => 'people',
40
        'valve'  => 'valves'
41
    );
42
43
    private static $uncountable = array(
44
        'sheep',
45
        'fish',
46
        'deer',
47
        'series',
48
        'species',
49
        'money',
50
        'rice',
51
        'information',
52
        'equipment'
53
    );
54
55 30
    public static function pluralize($string)
56
    {
57
        // save some time in the case that singular and plural are the same
58 30
        if (in_array(strtolower($string), self::$uncountable)) {
59
            return $string;
60
        }
61
62
        // check for irregular singular forms
63 30
        foreach (self::$irregular as $pattern => $result) {
64 30
            $pattern = '/' . $pattern . '$/i';
65
66 30
            if (preg_match($pattern, $string)) {
67 20
                return preg_replace($pattern, $result, $string);
68
            }
69
        }
70
71
        // check for matches using regular expressions
72 30
        foreach (self::$plural as $pattern => $result) {
73 30
            if (preg_match($pattern, $string)) {
74 30
                return preg_replace($pattern, $result, $string);
75
            }
76
        }
77
78
        return $string;
79
    }
80
}
81