1 | <?php |
||||
2 | |||||
3 | namespace W2w\Laravel\Apie\Console; |
||||
4 | |||||
5 | use Illuminate\Console\Command; |
||||
6 | use W2w\Lib\Apie\OpenApiSchema\OpenApiSpecGenerator; |
||||
7 | |||||
8 | class DumpOpenApiSpecCommand extends Command |
||||
9 | { |
||||
10 | protected $signature = 'apie:dump-open-api {filename}'; |
||||
11 | |||||
12 | protected $description = 'Dumps openapi spec as JSON or yaml to a file.'; |
||||
13 | |||||
14 | public function handle(OpenApiSpecGenerator $generator) |
||||
15 | { |
||||
16 | $filename = $this->argument('filename'); |
||||
17 | if (preg_match('/\.y[a]{0,1}ml$/i', $filename)) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
18 | $content = $this->createYamlContent($generator); |
||||
19 | } else { |
||||
20 | $content = $this->createJsonContent($generator); |
||||
21 | } |
||||
22 | if (@file_put_contents($this->argument('filename'), $content)) { |
||||
0 ignored issues
–
show
It seems like
$this->argument('filename') can also be of type string[] ; however, parameter $filename of file_put_contents() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
23 | $this->output->writeln('Created file "' . $this->argument('filename') . '" successfully!'); |
||||
0 ignored issues
–
show
Are you sure
$this->argument('filename') of type null|string|string[] can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
24 | } else { |
||||
25 | $this->output->error('Could not write file "' . $this->argument('filename') . '"!'); |
||||
26 | return false; |
||||
27 | } |
||||
28 | return true; |
||||
29 | } |
||||
30 | |||||
31 | private function createYamlContent(OpenApiSpecGenerator $generator): string |
||||
32 | { |
||||
33 | return $generator->getOpenApiSpec()->toYaml(20, 2); |
||||
34 | } |
||||
35 | |||||
36 | private function createJsonContent(OpenApiSpecGenerator $generator): string |
||||
37 | { |
||||
38 | return json_encode($generator->getOpenApiSpec()->toArray(), JSON_UNESCAPED_SLASHES); |
||||
39 | } |
||||
40 | } |
||||
41 |