Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/CommandMakeCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class CommandMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument name.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'name';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'module:make-command';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Generate new Artisan command for the specified module.';
36
37 5
    public function getDefaultNamespace() : string
38
    {
39 5
        $module = $this->laravel['modules'];
40
41 5
        return $module->config('paths.generator.command.namespace') ?: $module->config('paths.generator.command.path', 'Console');
42
    }
43
44
    /**
45
     * Get the console command arguments.
46
     *
47
     * @return array
48
     */
49 120
    protected function getArguments()
50
    {
51
        return [
52 120
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
53 120
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
54
        ];
55
    }
56
57
    /**
58
     * Get the console command options.
59
     *
60
     * @return array
61
     */
62 120
    protected function getOptions()
63
    {
64
        return [
65 120
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null],
66
        ];
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72 5
    protected function getTemplateContents()
73
    {
74 5
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
75
76 5
        return (new Stub('/command.stub', [
77 5
            'COMMAND_NAME' => $this->getCommandName(),
78 5
            'NAMESPACE'    => $this->getClassNamespace($module),
79 5
            'CLASS'        => $this->getClass(),
80 5
        ]))->render();
81
    }
82
83
    /**
84
     * @return string
85
     */
86 5
    private function getCommandName()
87
    {
88 5
        return $this->option('command') ?: 'command:name';
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94 5
    protected function getDestinationFilePath()
95
    {
96 5
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
97
98 5
        $commandPath = GenerateConfigReader::read('command');
99
100 5
        return $path . $commandPath->getPath() . '/' . $this->getFileName() . '.php';
101
    }
102
103
    /**
104
     * @return string
105
     */
106 5
    private function getFileName()
107
    {
108 5
        return Str::studly($this->argument('name'));
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::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...
109
    }
110
}
111