Completed
Pull Request — master (#1088)
by
unknown
02:31
created

Command   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 207
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 6

12 Methods

Rating   Name   Duplication   Size   Complexity  
A appendable() 0 4 1
A stubFile() 0 4 1
A replaces() 0 6 1
A getTemplateContents() 0 4 1
A getDestinationFilePath() 0 6 2
A resolveFilename() 0 6 1
A before() 0 4 1
A after() 0 4 1
A handle() 0 9 2
A getFileName() 0 10 3
A getDefaultNamespace() 0 9 3
A getGeneratorConfigKey() 0 5 2
1
<?php
2
3
namespace Nwidart\Modules\Generators;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Stub;
7
use Nwidart\Modules\Commands\GeneratorCommand;
8
use Nwidart\Modules\Console\Traits\Definitions;
9
use Nwidart\Modules\Support\Config\GenerateConfigReader;
10
11
abstract class Command extends GeneratorCommand
12
{
13
    use Definitions;
14
15
    /**
16
     * The name to be appended to the generated resources.
17
     *
18
     * @var null|string
19
     */
20
    protected $appendable;
21
22
    /**
23
     * Stub file of the resource
24
     *
25
     * @var null|string
26
     */
27
    protected $stubFile;
28
29
30
    /**
31
     * Specifies default path.
32
     *
33
     * @var null|string
34
     */
35
    protected $defaultPath;
36
37
    /**
38
     * Destination file extension
39
     *
40
     * @var string
41
     */
42
    protected $outputExtension = 'php';
43
44
    /**
45
     * Generator paths key
46
     *
47
     * @see config/modules.php
48
     * @var string
49
     */
50
    protected $generatorPathsKey = 'generator.paths';
51
52
    /**
53
     * Generator config key
54
     *
55
     * @see config/modules.php
56
     * @var string
57
     */
58
    protected $generatorConfigKey = '';
59
60
    /**
61
     * Generator config prefix for the $generatorConfigKey
62
     *
63
     * @var string
64
     */
65
    protected $generatorConfigPrefix;
66
67
    /**
68
     * @var string $configKeySeparator
69
     */
70
    protected $configKeySeparator = '-';
71
72
73
    /**
74
     * Getter for appendable
75
     *
76
     * @return void
77
     */
78
    public function appendable()
79
    {
80
        return $this->appendable;
81
    }
82
83
    /**
84
     * Getter for the stub file
85
     *
86
     * @return void
87
     */
88
    public function stubFile()
89
    {
90
        return $this->stubFile;
91
    }
92
93
    /**
94
     * Replacements for the stub file
95
     *
96
     * @return array
97
     */
98
    public function replaces()
99
    {
100
        return [
101
            //...
102
        ];
103
    }
104
105
    /**
106
     * Get stub file contents
107
     *
108
     * @return mixed
109
     */
110
    protected function getTemplateContents()
111
    {
112
        return (new Stub($this->stubFile(), $this->replaces()))->render();
113
    }
114
115
    /**
116
     * Get destination file path.
117
     *
118
     * @return mixed
119
     */
120
    public function getDestinationFilePath()
121
    {
122
        return  $this->getModulePath() . "/" .
0 ignored issues
show
Documentation Bug introduced by
The method getModulePath does not exist on object<Nwidart\Modules\Generators\Command>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
123
            GenerateConfigReader::read($this->getGeneratorConfigKey())->getPath() . "/" .
124
            $this->resolveFilename() . '.' . $this->outputExtension ?: 'php';
125
    }
126
127
    /**
128
     * Resolves the filename so that it always starts with capital letter
129
     *
130
     * @return string
131
     */
132
    private function resolveFilename()
133
    {
134
        $filename = str_replace('/', ' ', $this->getFileName());
135
        $filename = ucwords(str_replace('\\', ' ', $filename));
136
        return trim(str_replace(' ', '/', $filename));
137
    }
138
139
    /**
140
     * Method to apply necessary functionality
141
     * before console command gets executed
142
     *
143
     * @return void
144
     */
145
    public function before()
146
    {
147
        // ...
148
    }
149
150
    /**
151
     * Method to apply necessary functionality
152
     * after console command has executed.
153
     *
154
     * @return void
155
     */
156
    public function after()
157
    {
158
        // ...
159
    }
160
161
    /**
162
     * Execute the console command
163
     *
164
     * @return int
165
     */
166
    public function handle(): int
167
    {
168
        $this->before();
169
170
        if (parent::handle() != E_ERROR) {
171
            $this->after();
172
        }
173
        return 0;
174
    }
175
176
    /**
177
     * Get and resolve the filename.
178
     *
179
     * @return string
180
     */
181
    protected function getFileName(): string
182
    {
183
184
        $name = Str::studly($this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
185
        if ($this->appendable() && !Str::contains(strtolower($name), strtolower($this->appendable()))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->appendable() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
186
            $name .= Str::studly($this->appendable());
187
        }
188
189
        return Str::singular(Str::studly($name));
190
    }
191
192
    /**
193
     * Get default namespace.
194
     *
195
     * @return string
196
     */
197
    public function getDefaultNamespace(): string
198
    {
199
        return $this->getModules()->config(
200
            $this->generatorPathsKey . '.' . $this->getGeneratorConfigKey() . '.namespace'
201
        ) ?: $this->getModules()->config(
202
            $this->generatorPathsKey . '.' . $this->getGeneratorConfigKey() . '.path',
203
            $this->defaultPath ?: ''
204
        );
205
    }
206
207
    /**
208
     * Get configurator config key
209
     *
210
     * @return string
211
     */
212
    private function getGeneratorConfigKey()
213
    {
214
        return ($this->generatorConfigPrefix ? $this->generatorConfigPrefix . $this->configKeySeparator : '') .
215
            $this->generatorConfigKey;
216
    }
217
}
218