Passed
Push — master ( 0b2a3d...a47e04 )
by Bruno
17:31 queued 04:58
created

GeneratorNameTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 43
c 1
b 0
f 0
dl 0
loc 120
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInflector() 0 7 2
A setName() 0 6 1
A getStudlyName() 0 3 1
A splitClassName() 0 7 1
A template() 0 25 1
A getName() 0 3 1
A getBasePath() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Doctrine\Inflector\InflectorFactory;
6
use GraphQL\Type\Definition\Type;
7
use Illuminate\Support\Str;
8
use Modelarium\Exception\Exception;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\Parser;
11
12
trait GeneratorNameTrait
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $name = '';
18
19
    /**
20
     * @var string
21
     */
22
    protected $studlyName = '';
23
24
    /**
25
     * @var string
26
     */
27
    protected $lowerName = '';
28
29
    /**
30
     * @var string
31
     */
32
    protected $lowerNamePlural = '';
33
34
    public function getInflector(): \Doctrine\Inflector\Inflector
35
    {
36
        static $inflector = null;
37
        if (!$inflector) {
38
            $inflector = InflectorFactory::create()->build();
39
        }
40
        return $inflector;
41
    }
42
43
    protected function setName(string $name): void
44
    {
45
        $this->name = $name;
46
        $this->studlyName = Str::studly($this->name);
47
        $this->lowerName = mb_strtolower($this->name);
48
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
49
    }
50
51
    /**
52
     * Splits a fully qualified class name into its namespace, class name and relative path
53
     *
54
     * @param string $fullclass
55
     * @return array
56
     */
57
    protected static function splitClassName(string $fullclass): array
58
    {
59
        $classTokens = explode('\\', $fullclass);
60
        $className = array_pop($classTokens);
61
        $classNamespace = implode('\\', $classTokens);
62
        $relativePath = implode('/', $classTokens);
63
        return [$classNamespace, $className, $relativePath];
64
    }
65
66
    /**
67
     * Returns the base path (where composer.json is located)
68
     *
69
     * @param string $file The filename
70
     * @return string
71
     */
72
    public static function getBasePath(string $file = null): string
73
    {
74
        $basepath = dirname(\Composer\Factory::getComposerFile());
75
        if ($file) {
76
            $basepath .= '/' . $file;
77
        }
78
        return $basepath;
79
    }
80
81
    /**
82
     * Get the value of name
83
     *
84
     * @return  string
85
     */
86
    public function getName()
87
    {
88
        return $this->name;
89
    }
90
91
    /**
92
     * Get the value of studlyName
93
     *
94
     * @return  string
95
     */
96
    public function getStudlyName()
97
    {
98
        return $this->studlyName;
99
    }
100
101
    /**
102
     * Replaces common strings from the stubs
103
     *
104
     * @param string $str The string data to apply replaces
105
     * @return string
106
     */
107
    protected function template(string $str)
108
    {
109
        $date = date("c");
110
        return str_replace(
111
            [
112
                '{{StudlyName}}',
113
                '{{ StudlyName }}',
114
                '{{lowerName}}',
115
                '{{ lowerName }}',
116
                '{{lowerNamePlural}}',
117
                '{{ lowerNamePlural }}',
118
                '{{date}}',
119
                '{{ date }}',
120
            ],
121
            [
122
                $this->studlyName,
123
                $this->studlyName,
124
                $this->lowerName,
125
                $this->lowerName,
126
                $this->lowerNamePlural,
127
                $this->lowerNamePlural,
128
                $date,
129
                $date
130
            ],
131
            $str
132
        );
133
    }
134
}
135