Passed
Push — master ( cf70e7...e3c58b )
by Adrien
03:45 queued 10s
created

Plural::make()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
rs 9.9
cc 3
nc 3
nop 1
crap 3.004
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
27 147
        $replacements = [
28 147
            '/ys$/' => 'ies',
29 147
            '/ss$/' => 'ses',
30 147
            '/xs$/' => 'xes',
31 147
        ];
32
33 147
        $plural = preg_replace(array_keys($replacements), $replacements, $plural);
34 147
        if ($plural === null) {
35
            throw new Exception('Error while making plural');
36
        }
37
38 147
        return $plural;
39
    }
40
}
41