Passed
Push — master ( a41166...4c99b3 )
by Bruno
03:13
created

GeneratorNameTrait::getBasePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
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 28
    public function getInflector(): \Doctrine\Inflector\Inflector
34
    {
35 28
        static $inflector = null;
36 28
        if (!$inflector) {
37 6
            $inflector = InflectorFactory::create()->build();
38
        }
39 28
        return $inflector;
40
    }
41
42 28
    protected function setBaseName(string $name): void
43
    {
44 28
        $this->baseName = $name;
45 28
        $this->studlyName = Str::studly($this->baseName);
46 28
        $this->lowerName = mb_strtolower($this->baseName);
47 28
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
48 28
    }
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
     */
56 2
    public static function splitClassName(string $fullclass): array
57
    {
58 2
        $classTokens = explode('\\', $fullclass);
59 2
        $className = array_pop($classTokens);
60 2
        $classNamespace = implode('\\', $classTokens);
61 2
        $relativePath = implode('/', $classTokens);
62 2
        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
     */
71 12
    public static function getBasePath(string $file = null): string
72
    {
73 12
        $basepath = dirname(\Composer\Factory::getComposerFile());
74 12
        if ($file) {
75 12
            $basepath .= '/' . $file;
76
        }
77 12
        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
     */
95 8
    public function getStudlyName()
96
    {
97 8
        return $this->studlyName;
98
    }
99
100
    /**
101
     *
102
     * @param string $path
103
     * @return callable
104
     */
105 25
    protected function compileMustacheFromFile(string $path)
106
    {
107 25
        $template = \Safe\file_get_contents($path);
108 25
        $phpStr = LightnCandy::compile(
109 25
            $template,
110
            [
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 25
                'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
112
                'delimiters' => array('{|', '|}')
113
            ]
114
        );
115 25
        if (!$phpStr) {
116
            throw new Exception('Invalid template');
117
        }
118
        /** @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 25
        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
     */
130 25
    public function templateFile(string $path, array $context = [])
131
    {
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 25
        $context['date'] = date("c");
137
138 25
        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
     */
148 25
    public function templateStub(string $stubName, array $context = []): string
149
    {
150 25
        $stub = $this->stubDir . "/$stubName.mustache.php";
151 25
        return $this->templateFile($stub, $context);
152
    }
153
154
    /**
155
     * Get the value of lowerName
156
     *
157
     * @return  string
158
     */
159 1
    public function getLowerName(): string
160
    {
161 1
        return $this->lowerName;
162
    }
163
164
    /**
165
     * Get the value of lowerNamePlural
166
     *
167
     * @return  string
168
     */
169 1
    public function getLowerNamePlural(): string
170
    {
171 1
        return $this->lowerNamePlural;
172
    }
173
}
174