Completed
Push — master ( 7fbbb0...53f538 )
by Oleg
03:37
created

Lexer::getPluralForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Util;
5
6
use Zend\Filter\Word\CamelCaseToUnderscore;
7
8
final class Lexer
9
{
10
    /**
11
     * Convert plural into singular form
12
     *  # buses => bus
13
     *  # products => product
14
     *  # stores => store
15
     *  # categories => category
16
     *
17
     * @param string $name
18
     * @return string
19
     */
20 4
    public static function getSingularForm(string $name): string
21
    {
22 4
        if (preg_match('/.*ies$/', $name)) {
23 1
            return preg_replace('/(.*)ies$/', '$1y', $name);
24 3
        } elseif (preg_match('/.*ses$/', $name)) {
25 1
            return preg_replace('/(.*)ses$/', '$1s', $name);
26 2
        } elseif (preg_match('/.*s$/', $name)) {
27 2
            return preg_replace('/(.*)s$/', '$1', $name);
28
        }
29
30
        return $name;
31
    }
32
33
    /**
34
     * Convert singular into plural form
35
     *  # bus => buses
36
     *  # product => products
37
     *  # store => stores
38
     *  # category => categories
39
     *
40
     * @param string $name
41
     * @return string
42
     */
43 4
    public static function getPluralForm(string $name): string
44
    {
45 4
        if (preg_match('/.*s$/', $name)) {
46 1
            return preg_replace('/(.*)s$/', '$1ses', $name);
47 3
        } elseif (preg_match('/.*y$/', $name)) {
48 1
            return preg_replace('/(.*)y$/', '$1ies', $name);
49
        }
50
51 2
        return $name . 's';
52
    }
53
54
    /**
55
     * @param string $fullyQualifiedName
56
     * @return string
57
     */
58 16
    public static function getBaseName(string $fullyQualifiedName): string
59
    {
60 16
        if (($pos = strrpos($fullyQualifiedName, '\\')) !== false) {
61 14
            return ltrim(substr($fullyQualifiedName, $pos), '\\');
62
        }
63 2
        return $fullyQualifiedName;
64
    }
65
66
    /**
67
     * @param string $fullyQualifiedName
68
     * @return string
69
     */
70 11
    public static function getRefName(string $fullyQualifiedName): string
71
    {
72 11
        $baseName = self::getBaseName($fullyQualifiedName);
73 11
        return strtolower((new CamelCaseToUnderscore())->filter($baseName));
74
    }
75
}
76