DumpOpenApiSpecCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 31
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createJsonContent() 0 3 1
A createYamlContent() 0 3 1
A handle() 0 15 3
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
It seems like $filename can also be of type string[]; however, parameter $subject of preg_match() 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 ignore-type  annotation

17
        if (preg_match('/\.y[a]{0,1}ml$/i', /** @scrutinizer ignore-type */ $filename)) {
Loading history...
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
Bug introduced by
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 ignore-type  annotation

22
        if (@file_put_contents(/** @scrutinizer ignore-type */ $this->argument('filename'), $content)) {
Loading history...
23
            $this->output->writeln('Created file "' . $this->argument('filename') . '" successfully!');
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

23
            $this->output->writeln('Created file "' . /** @scrutinizer ignore-type */ $this->argument('filename') . '" successfully!');
Loading history...
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