Passed
Push — master ( 6bf22a...09030e )
by Bruno
04:28
created

GeneratorNameTrait::getBaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 $lowerName = '';
27
28
    /**
29
     * @var string
30
     */
31
    protected $lowerNamePlural = '';
32
33
    /**
34
     * @var string
35
     */
36
    protected $tableName = '';
37
38 29
    public function getInflector(): \Doctrine\Inflector\Inflector
39
    {
40 29
        static $inflector = null;
41 29
        if (!$inflector) {
42 6
            $inflector = InflectorFactory::create()->build();
43
        }
44 29
        return $inflector;
45
    }
46
47 29
    protected function setBaseName(string $name): void
48
    {
49 29
        $this->baseName = $name;
50 29
        $this->studlyName = Str::studly($this->baseName);
51 29
        $this->lowerName = mb_strtolower($this->baseName);
52 29
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
53 29
        $this->tableName = self::toTableName($this->baseName);
54 29
    }
55
56
    /**
57
     * Generates the expected laravel table name
58
     *
59
     * @param string $name
60
     * @return string
61
     */
62 29
    public static function toTableName(string $name): string
63
    {
64 29
        return Str::snake(Str::pluralStudly($name));
65
    }
66
67
    /**
68
     * Splits a fully qualified class name into its namespace, class name and relative path
69
     *
70
     * @param string $fullclass
71
     * @return array
72
     */
73 2
    public static function splitClassName(string $fullclass): array
74
    {
75 2
        $classTokens = explode('\\', $fullclass);
76 2
        $className = array_pop($classTokens);
77 2
        $classNamespace = implode('\\', $classTokens);
78 2
        $relativePath = implode('/', $classTokens);
79 2
        return [$classNamespace, $className, $relativePath];
80
    }
81
82
    /**
83
     * Returns the base path (where composer.json is located)
84
     *
85
     * @param string $file The filename
86
     * @return string
87
     */
88 12
    public static function getBasePath(string $file = null): string
89
    {
90 12
        $basepath = dirname(\Composer\Factory::getComposerFile());
91 12
        if ($file) {
92 12
            $basepath .= '/' . $file;
93
        }
94 12
        return $basepath;
95
    }
96
97
    /**
98
     * Get the value of name
99
     *
100
     * @return  string
101
     */
102
    public function getBaseName()
103
    {
104
        return $this->baseName;
105
    }
106
107
    /**
108
     * Get the value of studlyName
109
     *
110
     * @return  string
111
     */
112 8
    public function getStudlyName()
113
    {
114 8
        return $this->studlyName;
115
    }
116
117
    /**
118
     *
119
     * @param string $path
120
     * @return callable
121
     */
122 26
    protected function compileMustacheFromFile(string $path)
123
    {
124 26
        $template = \Safe\file_get_contents($path);
125 26
        $phpStr = LightnCandy::compile(
126 26
            $template,
127
            [
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

127
            /** @scrutinizer ignore-type */ [
Loading history...
128 26
                'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
129
                'delimiters' => array('{|', '|}')
130
            ]
131
        );
132 26
        if (!$phpStr) {
133
            throw new Exception('Invalid template');
134
        }
135
        /** @var callable $renderer */
136 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

136
        $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
137 26
        return $renderer;
138
    }
139
140
    /**
141
     * Replaces common strings from the stubs
142
     *
143
     * @param string $path The string data to apply replaces
144
     * @param array $context
145
     * @return string
146
     */
147 26
    public function templateFile(string $path, array $context = [])
148
    {
149 26
        $renderer = $this->compileMustacheFromFile($path);
150 26
        $context['StudlyName'] = $context['studlyName'] = $this->studlyName;
151 26
        $context['lowerName'] = $this->lowerName;
152 26
        $context['lowerNamePlural'] = $this->lowerNamePlural;
153 26
        $context['date'] = date("c");
154
155 26
        return $renderer($context);
156
    }
157
158
    /**
159
     * Stubs from a mustache file. Convenience wrapper for templateFile().
160
     *
161
     * @param string $stubName
162
     * @param array $context
163
     * @return string
164
     */
165 26
    public function templateStub(string $stubName, array $context = []): string
166
    {
167 26
        $stub = $this->stubDir . "/$stubName.mustache.php";
168 26
        return $this->templateFile($stub, $context);
169
    }
170
171
    /**
172
     * Get the value of lowerName
173
     *
174
     * @return  string
175
     */
176 8
    public function getLowerName(): string
177
    {
178 8
        return $this->lowerName;
179
    }
180
181
    /**
182
     * Get the value of lowerName
183
     *
184
     * @return  string
185
     */
186 6
    public function getTableName(): string
187
    {
188 6
        return $this->tableName;
189
    }
190
191
    /**
192
     * Get the value of lowerNamePlural
193
     *
194
     * @return  string
195
     */
196
    public function getLowerNamePlural(): string
197
    {
198
        return $this->lowerNamePlural;
199
    }
200
}
201