Completed
Push — master ( bb59c7...b639e0 )
by Woody
01:20
created

functions.php ➔ singular()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 1
dl 0
loc 24
ccs 0
cts 9
cp 0
crap 42
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace RoundingWell\Schematic;
4
5
/**
6
 * @param string $word
7
 * @return string
8
 */
9
function singular($word)
10
{
11
    if (substr($word, -1) !== 's') {
12
        return $word;
13
    }
14
15
    if (in_array(substr($word, -2), ['us', 'ss'])) {
16
        // Not a pluralized word
17
        return $word;
18
    }
19
20
    if (preg_match('/[ssz]es$/', $word) || preg_match('/[^aeioudgkprt]hes$/', $word)) {
21
        // Remove "es"
22
        return substr($word, 0, -2);
23
    }
24
25
    if (preg_match('/[^aeiou]ies$/', $word)) {
26
        // Replace "ies" with "y"
27
        return substr($word, 0, -3) . 'y';
28
    }
29
30
    // Remove "s"
31
    return substr($word, 0, -1);
32
}
33