Passed
Push — master ( 687fe0...cf70e7 )
by Adrien
02:51
created

Plural::make()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
ccs 9
cts 11
cp 0.8182
cc 4
nc 4
nop 1
crap 4.0961
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api;
6
7
final class Plural
8
{
9
    /**
10
     * Returns the plural form of the given name.
11
     *
12
     * This is **not** necessarily valid english grammar. Its only purpose is for internal usage, not for humans.
13
     *
14
     * This **MUST** be kept in sync with Natural's `makePlural()`.
15
     *
16
     * This is a bit performance-sensitive, so we should keep it fast and only cover cases that we actually need.
17
     */
18 148
    public static function make(string $name): string
19
    {
20
        // Words ending in a y preceded by a vowel form their plurals by adding -s:
21 148
        if (preg_match('/[aeiou]y$/', $name)) {
22 1
            return $name . 's';
23
        }
24
25 147
        $plural = $name . 's';
26 147
        $plural = preg_replace('/ys$/', 'ies', $plural);
27 147
        if ($plural === null) {
28
            throw new Exception('Error while making plural');
29
        }
30
31 147
        $plural = preg_replace('/ss$/', 'ses', $plural);
32 147
        if ($plural === null) {
33
            throw new Exception('Error while making plural');
34
        }
35
36 147
        return $plural;
37
    }
38
}
39