This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Jumilla\Generators; |
||
4 | |||
5 | use stdClass; |
||
6 | use InvalidArgumentException; |
||
7 | use League\Flysystem\FilesystemInterface; |
||
8 | |||
9 | class FileGenerator |
||
10 | { |
||
11 | /** |
||
12 | * Output directory filesystem. |
||
13 | * |
||
14 | * @var \League\Flysystem\FilesystemInterface |
||
15 | */ |
||
16 | protected $outbox; |
||
17 | |||
18 | /** |
||
19 | * Stub directory filessytem. |
||
20 | * |
||
21 | * @var \League\Flysystem\FilesystemInterface |
||
22 | */ |
||
23 | protected $stubbox; |
||
24 | |||
25 | /** |
||
26 | * Context of generate. |
||
27 | * |
||
28 | * @var \stdClass |
||
29 | */ |
||
30 | protected $context; |
||
31 | |||
32 | /** |
||
33 | * Create file generator. |
||
34 | * |
||
35 | * @param string $outbox_root_path |
||
36 | * @param string $stubbox_root_path |
||
37 | * |
||
38 | * @return static |
||
39 | */ |
||
40 | 32 | public static function make($outbox_root_path, $stubbox_root_path) |
|
41 | { |
||
42 | 32 | if (!is_dir($outbox_root_path)) { |
|
43 | 32 | mkdir($outbox_root_path, 0755, true); |
|
44 | 32 | } |
|
45 | |||
46 | 32 | $outbox = FilesystemFactory::local($outbox_root_path); |
|
47 | |||
48 | 32 | $stubbox = FilesystemFactory::local($stubbox_root_path); |
|
49 | |||
50 | $context = (object) [ |
||
51 | 32 | 'outbox_root' => $outbox_root_path, |
|
52 | 32 | 'stubbox_root' => $stubbox_root_path, |
|
53 | 32 | 'directory' => null, |
|
54 | 32 | 'file' => null, |
|
55 | 32 | ]; |
|
56 | |||
57 | 32 | return new static($outbox, $stubbox, $context); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * the Constructor. |
||
62 | * |
||
63 | * @param \Legue\Flysystem\FilesystemInterface $outbox |
||
64 | * @param \Legue\Flysystem\FilesystemInterface $stubbox |
||
65 | * @param \stdClass $context |
||
66 | */ |
||
67 | 33 | public function __construct(FilesystemInterface $outbox, FilesystemInterface $stubbox, stdClass $context) |
|
68 | { |
||
69 | 33 | $this->outbox = $outbox; |
|
70 | 33 | $this->stubbox = $stubbox; |
|
71 | 33 | $this->context = $context; |
|
72 | 33 | } |
|
73 | |||
74 | /** |
||
75 | * Get directory walker. |
||
76 | * |
||
77 | * @param string $path |
||
78 | * @param callable $callable |
||
79 | * |
||
80 | * @return static |
||
81 | */ |
||
82 | 10 | public function directory($path, callable $callable = null) |
|
83 | { |
||
84 | 10 | $directory_path = $this->makePath($path); |
|
85 | |||
86 | 10 | $this->outbox->createDir($directory_path); |
|
87 | |||
88 | 10 | $context = clone($this->context); |
|
89 | 10 | $context->directory = $directory_path; |
|
90 | |||
91 | 10 | $sub = new static($this->outbox, $this->stubbox, $context); |
|
92 | |||
93 | 10 | if ($callable) { |
|
94 | 4 | call_user_func($callable, $sub); |
|
95 | 4 | } |
|
96 | |||
97 | 10 | return $sub; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Generate sources. |
||
102 | * |
||
103 | * @param string $path |
||
104 | */ |
||
105 | 2 | View Code Duplication | public function sourceDirectory($path) |
0 ignored issues
–
show
|
|||
106 | { |
||
107 | 2 | foreach ($this->allFiles($this->stubbox, $this->makePath($path)) as $stubbox_path) { |
|
108 | 2 | if ($this->context->directory) { |
|
109 | 1 | $outbox_path = substr($stubbox_path, strlen($this->context->directory) + 1); |
|
110 | 1 | } else { |
|
111 | 1 | $outbox_path = $stubbox_path; |
|
112 | } |
||
113 | 2 | $this->directory(dirname($outbox_path))->file(basename($outbox_path))->source($stubbox_path); |
|
114 | 2 | } |
|
115 | 2 | } |
|
116 | |||
117 | /** |
||
118 | * Generate sources from templates. |
||
119 | * |
||
120 | * @param string $path |
||
121 | * @param array $arguments |
||
122 | */ |
||
123 | 2 | View Code Duplication | public function templateDirectory($path, array $arguments = []) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
124 | { |
||
125 | 2 | foreach ($this->allFiles($this->stubbox, $this->makePath($path)) as $stubbox_path) { |
|
126 | 2 | if ($this->context->directory) { |
|
127 | 1 | $outbox_path = substr($stubbox_path, strlen($this->context->directory) + 1); |
|
128 | 1 | } else { |
|
129 | 1 | $outbox_path = $stubbox_path; |
|
130 | } |
||
131 | 2 | $this->directory(dirname($outbox_path))->file(basename($outbox_path))->template($stubbox_path, $arguments); |
|
132 | 2 | } |
|
133 | 2 | } |
|
134 | |||
135 | /** |
||
136 | * Generate blank directory with '.gitkeep'. |
||
137 | * |
||
138 | * @param string $path |
||
139 | * @param string $file |
||
140 | */ |
||
141 | 2 | public function keepDirectory($path, $file = '.gitkeep') |
|
142 | { |
||
143 | 2 | $this->directory($path)->gitKeepFile($file); |
|
144 | 2 | } |
|
145 | |||
146 | /** |
||
147 | * Set file path. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @return $this |
||
151 | */ |
||
152 | 27 | public function file($path) |
|
153 | { |
||
154 | 27 | $this->context->file = $this->makePath($path); |
|
155 | |||
156 | 27 | return $this; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Check file existing. |
||
161 | * |
||
162 | * @param string $path |
||
163 | * @return bool |
||
164 | */ |
||
165 | 2 | public function exists($path) |
|
166 | { |
||
167 | 2 | return $this->outbox->has($this->makePath($path)); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Generate blank file. |
||
172 | */ |
||
173 | 7 | public function blank() |
|
174 | { |
||
175 | 7 | $this->outbox->put($this->context->file, ''); |
|
176 | 7 | } |
|
177 | |||
178 | /** |
||
179 | * Generate text file. |
||
180 | * |
||
181 | * @param string $content |
||
182 | * @param array $arguments |
||
183 | */ |
||
184 | 7 | public function text($content, array $arguments = []) |
|
185 | { |
||
186 | 7 | $this->outbox->put($this->context->file, $arguments ? $this->generate($content, $arguments) : $content); |
|
187 | 7 | } |
|
188 | |||
189 | /** |
||
190 | * Generate json file. |
||
191 | * |
||
192 | * @param array $data |
||
193 | */ |
||
194 | 1 | public function json(array $data) |
|
195 | { |
||
196 | 1 | $this->outbox->put($this->context->file, json_encode($data, JSON_PRETTY_PRINT)); |
|
197 | 1 | } |
|
198 | |||
199 | /** |
||
200 | * Generate source file from stub. |
||
201 | * |
||
202 | * @param string $stub_path |
||
203 | */ |
||
204 | 5 | public function source($stub_path) |
|
205 | { |
||
206 | 5 | $this->outbox->put($this->context->file, $this->read($stub_path)); |
|
207 | 4 | } |
|
208 | |||
209 | /** |
||
210 | * Generate source file from template. |
||
211 | * |
||
212 | * @param string $stub_path |
||
213 | * @param array $arguments |
||
214 | */ |
||
215 | 5 | public function template($stub_path, array $arguments = []) |
|
216 | { |
||
217 | 5 | $this->outbox->put($this->context->file, $this->generate($this->read($stub_path), $arguments)); |
|
218 | 4 | } |
|
219 | |||
220 | /** |
||
221 | * Generate '.gitkeep' file. |
||
222 | * |
||
223 | * @param string $path |
||
224 | */ |
||
225 | 4 | public function gitKeepFile($path = '.gitkeep') |
|
226 | { |
||
227 | 4 | $this->file($path)->blank(); |
|
228 | 4 | } |
|
229 | |||
230 | /** |
||
231 | * Generate PHP blank file. |
||
232 | * |
||
233 | * @param string $path |
||
234 | */ |
||
235 | 1 | public function phpBlankFile($path) |
|
236 | { |
||
237 | 1 | $this->file($path)->text('<?php'.PHP_EOL.PHP_EOL); |
|
238 | 1 | } |
|
239 | |||
240 | /** |
||
241 | * Generate PHP config file. |
||
242 | * |
||
243 | * @param string $path |
||
244 | * @param string $namespace |
||
245 | */ |
||
246 | 2 | public function phpConfigFile($path, array $config = [], $namespace = null) |
|
247 | { |
||
248 | 2 | $this->file($path)->text(Php\ConfigGenerator::generateText($config, $namespace)); |
|
249 | 2 | } |
|
250 | |||
251 | /** |
||
252 | * Generate PHP source file. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * @param string $source |
||
256 | * @param string $namespace |
||
257 | */ |
||
258 | 2 | public function phpSourceFile($path, $source, $namespace = '') |
|
259 | { |
||
260 | 2 | if ($namespace) { |
|
261 | 1 | $namespace = "namespace {$namespace};".PHP_EOL.PHP_EOL; |
|
262 | 1 | } |
|
263 | |||
264 | 2 | $this->file($path)->text('<?php'.PHP_EOL.PHP_EOL.$namespace.$source.PHP_EOL); |
|
265 | 2 | } |
|
266 | |||
267 | /** |
||
268 | * Generate source file, same name. |
||
269 | * |
||
270 | * @param string $path |
||
271 | */ |
||
272 | 1 | public function sourceFile($path) |
|
273 | { |
||
274 | 1 | $this->file($path)->source($this->makePath($path)); |
|
275 | 1 | } |
|
276 | |||
277 | /** |
||
278 | * Generate template file, same name. |
||
279 | * |
||
280 | * @param string $path |
||
281 | * @param array $arguments |
||
282 | */ |
||
283 | 1 | public function templateFile($path, array $arguments = []) |
|
284 | { |
||
285 | 1 | $this->file($path)->template($this->makePath($path), $arguments); |
|
286 | 1 | } |
|
287 | |||
288 | /** |
||
289 | * Create relative path in box. |
||
290 | * |
||
291 | * @param $path |
||
292 | * @return string |
||
293 | */ |
||
294 | 31 | protected function makePath($path) |
|
295 | { |
||
296 | 31 | return $this->context->directory ? $this->context->directory.'/'.$path : $path; |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * Get all files in directory |
||
301 | * |
||
302 | * @param FilesystemInterface $filesystem |
||
303 | * @param string $path |
||
304 | * @return array |
||
305 | */ |
||
306 | 4 | protected function allFiles(FilesystemInterface $filesystem, $path) |
|
307 | { |
||
308 | 4 | $files = []; |
|
309 | |||
310 | 4 | foreach ($filesystem->listContents($path, true) as $file) { |
|
311 | 4 | if ($file['type'] == 'file') { |
|
312 | 4 | $files[] = $file['path']; |
|
313 | 4 | } |
|
314 | 4 | } |
|
315 | |||
316 | 4 | return $files; |
|
317 | } |
||
318 | |||
319 | /** |
||
320 | * Generate template |
||
321 | * |
||
322 | * @param string $content |
||
323 | * @param array $arguments |
||
324 | * @return mixed |
||
325 | */ |
||
326 | 5 | protected function generate($content, array $arguments) |
|
327 | { |
||
328 | 5 | foreach ($arguments as $name => $value) { |
|
329 | 5 | if (is_array($value)) { |
|
330 | 1 | $value = implode(', ', $value); |
|
331 | 1 | } |
|
332 | 5 | $content = preg_replace('/\{\s*\$'.$name.'\s*\}/', $value, $content); |
|
333 | 5 | } |
|
334 | |||
335 | 5 | return $content; |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * Read stub file |
||
340 | * |
||
341 | * @param string $content |
||
0 ignored issues
–
show
There is no parameter named
$content . Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not. ![]() |
|||
342 | * @param array $arguments |
||
0 ignored issues
–
show
There is no parameter named
$arguments . Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not. ![]() |
|||
343 | * @return mixed |
||
344 | */ |
||
345 | 10 | protected function read($stub_path) |
|
346 | { |
||
347 | 10 | $content = $this->stubbox->read($stub_path); |
|
348 | |||
349 | 8 | if ($content === false) { |
|
350 | throw new InvalidArgumentException("File '$stub_path' is not found."); |
||
351 | } |
||
352 | |||
353 | 8 | return $content; |
|
354 | } |
||
355 | } |
||
356 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.