Lexer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 66
rs 10
c 0
b 0
f 0
ccs 20
cts 21
cp 0.9524
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRefName() 0 4 1
A getSingularForm() 0 11 4
A getPluralForm() 0 9 3
A getBaseName() 0 6 2
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