Passed
Push — master ( 1dbc05...713241 )
by Bruno
03:36
created

GeneratorNameTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Test Coverage

Coverage 95.16%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
c 2
b 0
f 0
dl 0
loc 222
ccs 59
cts 62
cp 0.9516
rs 10
wmc 18

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getLowerFirstLetterNamePlural() 0 3 1
A templateStub() 0 4 1
A toTableName() 0 3 1
A splitClassName() 0 7 1
A getTableName() 0 3 1
A getStudlyName() 0 3 1
A getLowerName() 0 3 1
A getLowerFirstLetterName() 0 3 1
A getInflector() 0 7 2
A templateFile() 0 11 1
A setBaseName() 0 9 1
A getBasePath() 0 7 2
A compileMustacheFromFile() 0 16 2
A getBaseName() 0 3 1
A getLowerNamePlural() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Doctrine\Inflector\InflectorFactory;
6
use Illuminate\Support\Str;
7
use LightnCandy\LightnCandy;
8
use Modelarium\Exception\Exception;
9
use function Safe\date;
10
11
trait GeneratorNameTrait
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $baseName = '';
17
18
    /**
19
     * @var string
20
     */
21
    protected $studlyName = '';
22
23
    /**
24
     * @var string
25
     */
26
    protected $lowerFirstLetterName = '';
27
28
    /**
29
     * @var string
30
     */
31
    protected $lowerFirstLetterNamePlural = '';
32
33
    /**
34
     * @var string
35
     */
36
    protected $lowerName = '';
37
38
    /**
39
     * @var string
40
     */
41
    protected $lowerNamePlural = '';
42
43
    /**
44
     * @var string
45
     */
46
    protected $tableName = '';
47
48 30
    public function getInflector(): \Doctrine\Inflector\Inflector
49
    {
50 30
        static $inflector = null;
51 30
        if (!$inflector) {
52 6
            $inflector = InflectorFactory::create()->build();
53
        }
54 30
        return $inflector;
55
    }
56
57 30
    protected function setBaseName(string $name): void
58
    {
59 30
        $this->baseName = $name;
60 30
        $this->studlyName = Str::studly($this->baseName);
61 30
        $this->lowerFirstLetterName = lcfirst($this->baseName);
62 30
        $this->lowerFirstLetterNamePlural = lcfirst($this->getInflector()->pluralize($this->baseName));
63 30
        $this->lowerName = mb_strtolower($this->baseName);
64 30
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
65 30
        $this->tableName = self::toTableName($this->baseName);
66 30
    }
67
68
    /**
69
     * Generates the expected laravel table name
70
     *
71
     * @param string $name
72
     * @return string
73
     */
74 30
    public static function toTableName(string $name): string
75
    {
76 30
        return Str::snake(Str::pluralStudly($name));
77
    }
78
79
    /**
80
     * Splits a fully qualified class name into its namespace, class name and relative path
81
     *
82
     * @param string $fullclass
83
     * @return array
84
     */
85 3
    public static function splitClassName(string $fullclass): array
86
    {
87 3
        $classTokens = explode('\\', $fullclass);
88 3
        $className = array_pop($classTokens);
89 3
        $classNamespace = implode('\\', $classTokens);
90 3
        $relativePath = implode('/', $classTokens);
91 3
        return [$classNamespace, $className, $relativePath];
92
    }
93
94
    /**
95
     * Returns the base path (where composer.json is located)
96
     *
97
     * @param string $file The filename
98
     * @return string
99
     */
100 13
    public static function getBasePath(string $file = null): string
101
    {
102 13
        $basepath = dirname(\Composer\Factory::getComposerFile());
103 13
        if ($file) {
104 13
            $basepath .= '/' . $file;
105
        }
106 13
        return $basepath;
107
    }
108
109
    /**
110
     * Get the value of name
111
     *
112
     * @return  string
113
     */
114 8
    public function getBaseName()
115
    {
116 8
        return $this->baseName;
117
    }
118
119
    /**
120
     * Get the value of studlyName
121
     *
122
     * @return  string
123
     */
124 8
    public function getStudlyName()
125
    {
126 8
        return $this->studlyName;
127
    }
128
129
    /**
130
     *
131
     * @param string $path
132
     * @return callable
133
     */
134 26
    protected function compileMustacheFromFile(string $path)
135
    {
136 26
        $template = \Safe\file_get_contents($path);
137 26
        $phpStr = LightnCandy::compile(
138 26
            $template,
139
            [
0 ignored issues
show
Bug introduced by
array('flags' => LightnC...' => array('{|', '|}')) of type array<string,array<integer,string>|integer> is incompatible with the type array<string,array|integer|string> expected by parameter $options of LightnCandy\LightnCandy::compile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
            /** @scrutinizer ignore-type */ [
Loading history...
140 26
                'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
141
                'delimiters' => array('{|', '|}')
142
            ]
143
        );
144 26
        if (!$phpStr) {
145
            throw new Exception('Invalid template');
146
        }
147
        /** @var callable $renderer */
148 26
        $renderer = LightnCandy::prepare($phpStr);
0 ignored issues
show
Deprecated Code introduced by
The function LightnCandy\LightnCandy::prepare() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
        $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
149 26
        return $renderer;
150
    }
151
152
    /**
153
     * Replaces common strings from the stubs
154
     *
155
     * @param string $path The string data to apply replaces
156
     * @param array $context
157
     * @return string
158
     */
159 26
    public function templateFile(string $path, array $context = [])
160
    {
161 26
        $renderer = $this->compileMustacheFromFile($path);
162 26
        $context['StudlyName'] = $context['studlyName'] = $this->studlyName;
163 26
        $context['lowerFirstLetterName'] = $this->lowerFirstLetterName;
164 26
        $context['lowerFirstLetterNamePlural'] = $this->lowerFirstLetterNamePlural;
165 26
        $context['lowerName'] = $this->lowerName;
166 26
        $context['lowerNamePlural'] = $this->lowerNamePlural;
167 26
        $context['date'] = date("c");
168
169 26
        return $renderer($context);
170
    }
171
172
    /**
173
     * Stubs from a mustache file. Convenience wrapper for templateFile().
174
     *
175
     * @param string $stubName
176
     * @param array $context
177
     * @return string
178
     */
179 26
    public function templateStub(string $stubName, array $context = []): string
180
    {
181 26
        $stub = $this->stubDir . "/$stubName.mustache.php";
182 26
        return $this->templateFile($stub, $context);
183
    }
184
185
    /**
186
     * Get the value of lowerName
187
     *
188
     * @return  string
189
     */
190 5
    public function getLowerName(): string
191
    {
192 5
        return $this->lowerName;
193
    }
194
195
    /**
196
     * Get the value of lowerName
197
     *
198
     * @return  string
199
     */
200 6
    public function getTableName(): string
201
    {
202 6
        return $this->tableName;
203
    }
204
205
    /**
206
     * Get the value of lowerNamePlural
207
     *
208
     * @return  string
209
     */
210
    public function getLowerNamePlural(): string
211
    {
212
        return $this->lowerNamePlural;
213
    }
214
215
    /**
216
     * Get the value of lowerFirstLetterName
217
     *
218
     * @return  string
219
     */
220 5
    public function getLowerFirstLetterName()
221
    {
222 5
        return $this->lowerFirstLetterName;
223
    }
224
225
    /**
226
     * Get the value of lowerFirstLetterNamePlural
227
     *
228
     * @return  string
229
     */
230 3
    public function getLowerFirstLetterNamePlural()
231
    {
232 3
        return $this->lowerFirstLetterNamePlural;
233
    }
234
}
235