Passed
Push — master ( d845af...25b359 )
by Bruno
08:11
created

GeneratorNameTrait::getInflector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

110
            /** @scrutinizer ignore-type */ [
Loading history...
111
                'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
112
                'delimiters' => array('{|', '|}')
113
            ]
114 25
        );
115
        if (!$phpStr) {
116
            throw new Exception('Invalid template');
117
        }
118 25
        /** @var callable $renderer */
119 25
        $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

119
        $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
120
        return $renderer;
121
    }
122
123
    /**
124
     * Replaces common strings from the stubs
125
     *
126
     * @param string $path The string data to apply replaces
127
     * @param array $context
128
     * @return string
129 25
     */
130
    public function templateFile(string $path, array $context = [])
131 25
    {
132 25
        $renderer = $this->compileMustacheFromFile($path);
133 25
        $context['StudlyName'] = $context['studlyName'] = $this->studlyName;
134 25
        $context['lowerName'] = $this->lowerName;
135 25
        $context['lowerNamePlural'] = $this->lowerNamePlural;
136
        $context['date'] = date("c");
137 25
138
        return $renderer($context);
139
    }
140
141
    /**
142
     * Stubs from a mustache file. Convenience wrapper for templateFile().
143
     *
144
     * @param string $stubName
145
     * @param array $context
146
     * @return string
147 25
     */
148
    public function templateStub(string $stubName, array $context = []): string
149 25
    {
150 25
        $stub = $this->stubDir . "/$stubName.mustache.php";
151
        return $this->templateFile($stub, $context);
152
    }
153
154
    /**
155
     * Get the value of lowerName
156
     *
157
     * @return  string
158
     */
159
    public function getLowerName()
160
    {
161
        return $this->lowerName;
162
    }
163
}
164