Completed
Push — master ( 81ce9d...1672d9 )
by Oleg
03:54
created

Lexer::getPluralForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.6666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Util;
5
6
class Lexer
7
{
8
    /**
9
     * Convert plural into singular form
10
     *  # buses => bus
11
     *  # products => product
12
     *  # stores => store
13
     *  # categories => category
14
     *
15
     * @param string $name
16
     * @return string
17
     */
18 6
    public static function getSingularForm(string $name): string
19
    {
20 6
        if (preg_match('/.*ies$/', $name)) {
21 3
            return preg_replace('/(.*)ies$/', '$1y', $name);
22 4
        } elseif (preg_match('/.*ses$/', $name)) {
23 1
            return preg_replace('/(.*)ses$/', '$1s', $name);
24 3
        } elseif (preg_match('/.*s$/', $name)) {
25
            return preg_replace('/(.*)s$/', '$1', $name);
26
        }
27
28 3
        return $name;
29
    }
30
31
    /**
32
     * Convert singular into plural form
33
     *  # bus => buses
34
     *  # product => products
35
     *  # store => stores
36
     *  # category => categories
37
     *
38
     * @param string $name
39
     * @return string
40
     */
41 8
    public static function getPluralForm(string $name): string
42
    {
43 8
        if (preg_match('/.*s$/', $name)) {
44
            return preg_replace('/(.*)s$/', '$1ses', $name);
45 8
        } elseif (preg_match('/.*y$/', $name)) {
46 4
            return preg_replace('/(.*)y$/', '$1ies', $name);
47
        }
48
49 4
        return $name . 's';
50
    }
51
}
52