Passed
Push — main ( 82f9e4...c1b717 )
by ikechukwu
11:44
created

MakeInterfaceCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveStubPath() 0 5 2
A alreadyExists() 0 4 2
A buildClass() 0 18 2
A getStub() 0 7 2
A getDefaultNamespace() 0 3 1
A getOptions() 0 5 1
1
<?php
2
3
namespace Ikechukwukalu\Makeservice\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
#[AsCommand(name: 'make:interface')]
11
class MakeInterfaceCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'make:interface';
19
20
    /**
21
     * The name of the console command.
22
     *
23
     * This name is used to identify the command during lazy loading.
24
     *
25
     * @var string|null
26
     *
27
     * @deprecated
28
     */
29
    protected static $defaultName = 'make:interface';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new interface class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Interface';
44
45
    /**
46
     * Build the class with the given name.
47
     *
48
     * @param  string  $name
49
     * @return string
50
     */
51
    protected function buildClass($name)
52
    {
53
        $model = $this->option('model');
54
55
        if (! Str::startsWith($model, [
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $haystack of Illuminate\Support\Str::startsWith() 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

55
        if (! Str::startsWith(/** @scrutinizer ignore-type */ $model, [
Loading history...
56
            $this->laravel->getNamespace(),
57
            'Illuminate',
58
            '\\',
59
        ])) {
60
            $model = $this->laravel->getNamespace().'Models\\'.str_replace('/', '\\', $model);
61
        }
62
63
        $stub = str_replace(
64
            ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name)
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $class of class_basename() does only seem to accept object|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

64
            ['DummyModel', '{{ model }}'], class_basename(/** @scrutinizer ignore-type */ $model), parent::buildClass($name)
Loading history...
65
        );
66
67
        return str_replace(
68
            ['DummyFullModel', '{{ modelNamespace }}'], trim($model, '\\'), $stub
69
        );
70
    }
71
72
    /**
73
     * Determine if the class already exists.
74
     *
75
     * @param  string  $rawName
76
     * @return bool
77
     */
78
    protected function alreadyExists($rawName)
79
    {
80
        return class_exists($rawName) ||
81
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
82
    }
83
84
    /**
85
     * Get the stub file for the generator.
86
     *
87
     * @return string
88
     */
89
    protected function getStub()
90
    {
91
        if ($this->option('model')) {
92
            return __DIR__.'/stubs/interface.stub';
93
        }
94
95
        return __DIR__.'/stubs/interface-duck.stub';
96
    }
97
98
    /**
99
     * Resolve the fully-qualified path to the stub.
100
     *
101
     * @param  string  $stub
102
     * @return string
103
     */
104
    protected function resolveStubPath($stub)
105
    {
106
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
107
                        ? $customPath
108
                        : __DIR__.$stub;
109
    }
110
111
    /**
112
     * Get the default namespace for the class.
113
     *
114
     * @param  string  $rootNamespace
115
     * @return string
116
     */
117
    protected function getDefaultNamespace($rootNamespace)
118
    {
119
        return $rootNamespace.'\Interfaces';
120
    }
121
122
    /**
123
     * Get the console command options.
124
     *
125
     * @return array
126
     */
127
    protected function getOptions()
128
    {
129
        return [
130
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the interface already exists'],
131
            ['model', 'm', InputOption::VALUE_REQUIRED, 'Create a model namespace for this interface'],
132
        ];
133
    }
134
}
135