Completed
Push — master ( 9187f2...dc6751 )
by Nicolas
13:53
created

src/Commands/ControllerMakeCommand.php (2 issues)

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 ControllerMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument being used.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'controller';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'module:make-controller';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Generate new restful controller for the specified module.';
36
37
    /**
38
     * Get controller name.
39
     *
40
     * @return string
41
     */
42 117
    public function getDestinationFilePath()
43
    {
44 117
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
45
46 117
        $controllerPath = GenerateConfigReader::read('controller');
47
48 117
        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
49
    }
50
51
    /**
52
     * @return string
53
     */
54 117
    protected function getTemplateContents()
55
    {
56 117
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
57
58 117
        return (new Stub($this->getStubName(), [
59 117
            'MODULENAME'        => $module->getStudlyName(),
60 117
            'CONTROLLERNAME'    => $this->getControllerName(),
61 117
            'NAMESPACE'         => $module->getStudlyName(),
62 117
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
63 117
            'CLASS'             => $this->getControllerNameWithoutNamespace(),
64 117
            'LOWER_NAME'        => $module->getLowerName(),
65 117
            'MODULE'            => $this->getModuleName(),
66 117
            'NAME'              => $this->getModuleName(),
67 117
            'STUDLY_NAME'       => $module->getStudlyName(),
68 117
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
69 117
        ]))->render();
70
    }
71
72
    /**
73
     * Get the console command arguments.
74
     *
75
     * @return array
76
     */
77 127
    protected function getArguments()
78
    {
79
        return [
80 127
            ['controller', InputArgument::REQUIRED, 'The name of the controller class.'],
81
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
82
        ];
83
    }
84
85
    /**
86
     * @return array
87
     */
88 127 View Code Duplication
    protected function getOptions()
89
    {
90
        return [
91 127
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
92
            ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'],
93
        ];
94
    }
95
96
    /**
97
     * @return array|string
98
     */
99 117
    protected function getControllerName()
100
    {
101 117
        $controller = Str::studly($this->argument('controller'));
0 ignored issues
show
It seems like $this->argument('controller') 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...
102
103 117
        if (Str::contains(strtolower($controller), 'controller') === false) {
104 2
            $controller .= 'Controller';
105
        }
106
107 117
        return $controller;
108
    }
109
110
    /**
111
     * @return array|string
112
     */
113 117
    private function getControllerNameWithoutNamespace()
114
    {
115 117
        return class_basename($this->getControllerName());
0 ignored issues
show
It seems like $this->getControllerName() targeting Nwidart\Modules\Commands...nd::getControllerName() can also be of type array; however, class_basename() does only seem to accept string|object, 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...
116
    }
117
118 117
    public function getDefaultNamespace() : string
119
    {
120 117
        $module = $this->laravel['modules'];
121
122 117
        return $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Http/Controllers');
123
    }
124
125
    /**
126
     * Get the stub file name based on the options
127
     * @return string
128
     */
129 117
    private function getStubName()
130
    {
131 117
        if ($this->option('plain') === true) {
132 1
            $stub = '/controller-plain.stub';
133 117
        } elseif ($this->option('api') === true) {
134 1
            $stub = '/controller-api.stub';
135
        } else {
136 117
            $stub = '/controller.stub';
137
        }
138
139 117
        return $stub;
140
    }
141
}
142