Completed
Pull Request — master (#23)
by Tom
03:43
created

MakePageCommand::replaceClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Spatie\Sheets\Facades\Sheets;
8
use Spatie\Sheets\Repositories\FilesystemRepository;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Yaml\Yaml;
11
12
class MakePageCommand extends GeneratorCommand
13
{
14
    protected $name = 'make:page';
0 ignored issues
show
introduced by
Property \App\Console\Commands\MakePageCommand::$name does not have @var annotation.
Loading history...
15
16
    protected $description = 'Create a new page for stancy package.';
0 ignored issues
show
introduced by
Property \App\Console\Commands\MakePageCommand::$description does not have @var annotation.
Loading history...
17
18
    public function handle()
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::handle() does not have return type hint nor @return annotation for its return value.
Loading history...
19
    {
20
        $this->createSheet();
21
22
        return parent::handle();
23
    }
24
25
    protected function createSheet(): void
0 ignored issues
show
introduced by
You should change the following
<fg=green>+ public function getOptions()
</><fg=green>+
</><fg=green>+ return [
</><fg=green>+ ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
</><fg=green>+ ];
</><fg=green>+
</><fg=green>+
</>
Loading history...
26
    {
27
        $name = $this->getNameInput();
28
        $collection = $this->option('collection');
29
30
        if (!$collection) {
0 ignored issues
show
introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
31
            return;
32
        }
33
34
        $repository = Sheets::collection($collection);
35
36
        if (!$repository instanceof FilesystemRepository) {
0 ignored issues
show
introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
37
            $this->warn('can not create a sheet if collection is not instance of `'.FilesystemRepository::class.'`');
38
39
            return;
40
        }
41
42
        $filename = Str::kebab(class_basename($name)).'.'.$repository->getExtension();
0 ignored issues
show
Bug introduced by
The method getExtension() does not exist on Spatie\Sheets\Repositories\FilesystemRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $filename = Str::kebab(class_basename($name)).'.'.$repository->/** @scrutinizer ignore-call */ getExtension();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        if (
45
            (! $this->hasOption('force') || ! $this->option('force'))
46
            && $repository->getFilesystem()->exists($filename)
0 ignored issues
show
Bug introduced by
The method getFilesystem() does not exist on Spatie\Sheets\Repositories\FilesystemRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            && $repository->/** @scrutinizer ignore-call */ getFilesystem()->exists($filename)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        ) {
48
            $this->error('the sheet `'.$filename.'` already exists');
49
50
            return;
51
        }
52
53
        $repository->getFilesystem()->put($filename, $this->getDefaultSheetContent($repository->getExtension()));
54
    }
55
56
    protected function getDefaultSheetContent(string $extension): string
57
    {
58
        if (!in_array($extension, ['md', 'json', 'yaml', 'yml'])) {
0 ignored issues
show
introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
59
            return '';
60
        }
61
62
        $data = [
63
            '_pageData' => '\\'.$this->qualifyClass($this->getNameInput()),
64
        ];
65
66
        if ($extension == 'json') {
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
67
            return json_encode($data);
68
        }
69
70
        if (in_array($extension, ['yaml', 'yml'])) {
71
            return Yaml::dump($data);
72
        }
73
74
        return implode(PHP_EOL, [
75
            '---',
76
            trim(Yaml::dump(array_merge($data, ['_view' => null]))),
77
            '---',
78
            '',
79
        ]);
80
    }
81
82
    protected function getStub(): string
83
    {
84
        return __DIR__.'/../../../../stancy/resources/stubs/page.stub';
85
    }
86
87
    protected function getDefaultNamespace($rootNamespace): string
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::getDefaultNamespace() does not have parameter type hint nor @param annotation for its parameter $rootNamespace.
Loading history...
88
    {
89
        return rtrim($rootNamespace, '\\').'\Pages';
90
    }
91
92
    protected function replaceClass($stub, $name): string
0 ignored issues
show
introduced by
Possible useless method overriding detected
Loading history...
introduced by
Method \App\Console\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $stub.
Loading history...
introduced by
Method \App\Console\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $name.
Loading history...
93
    {
94
        return parent::replaceClass($stub, $name);
95
    }
0 ignored issues
show
introduced by
You should change the following
<fg=red>- }
</><fg=red>-
</><fg=red>- public function getOptions()
</><fg=red>- {
</><fg=red>- return [
</><fg=red>- ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
</><fg=red>- ];
</>
Loading history...
96
97
    public function getOptions()
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::getOptions() does not have return type hint nor @return annotation for its return value.
Loading history...
98
    {
99
        return [
100
            ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
101
        ];
102
    }
103
}
104