Lexer::getRefName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 8
    public static function getPluralForm(string $name): string
44
    {
45 8
        if (preg_match('/.*s$/', $name)) {
46 1
            return preg_replace('/(.*)s$/', '$1ses', $name);
47 7
        } elseif (preg_match('/.*y$/', $name)) {
48 5
            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 30
    public static function getBaseName(string $fullyQualifiedName): string
59
    {
60 30
        if (($pos = strrpos($fullyQualifiedName, '\\')) !== false) {
61 28
            return ltrim(substr($fullyQualifiedName, $pos), '\\');
62
        }
63 2
        return $fullyQualifiedName;
64
    }
65
66
    /**
67
     * @param string $fullyQualifiedName
68
     * @return string
69
     */
70 25
    public static function getRefName(string $fullyQualifiedName): string
71
    {
72 25
        $baseName = self::getBaseName($fullyQualifiedName);
73 25
        return strtolower((new CamelCaseToUnderscore())->filter($baseName));
74
    }
75
}
76