Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
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 111
    public function getDestinationFilePath()
43
    {
44 111
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
45
46 111
        $controllerPath = GenerateConfigReader::read('controller');
47
48 111
        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
49
    }
50
51
    /**
52
     * @return string
53
     */
54 111
    protected function getTemplateContents()
55
    {
56 111
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
57
58 111
        return (new Stub($this->getStubName(), [
59 111
            'MODULENAME'        => $module->getStudlyName(),
60 111
            'CONTROLLERNAME'    => $this->getControllerName(),
61 111
            'NAMESPACE'         => $module->getStudlyName(),
62 111
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
63 111
            'CLASS'             => $this->getControllerNameWithoutNamespace(),
64 111
            'LOWER_NAME'        => $module->getLowerName(),
65 111
            'MODULE'            => $this->getModuleName(),
66 111
            'NAME'              => $this->getModuleName(),
67 111
            'STUDLY_NAME'       => $module->getStudlyName(),
68 111
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
69 111
        ]))->render();
70
    }
71
72
    /**
73
     * Get the console command arguments.
74
     *
75
     * @return array
76
     */
77 120
    protected function getArguments()
78
    {
79
        return [
80 120
            ['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 120 View Code Duplication
    protected function getOptions()
89
    {
90
        return [
91 120
            ['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 111
    protected function getControllerName()
100
    {
101 111
        $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 111
        if (Str::contains(strtolower($controller), 'controller') === false) {
104 2
            $controller .= 'Controller';
105
        }
106
107 111
        return $controller;
108
    }
109
110
    /**
111
     * @return array|string
112
     */
113 111
    private function getControllerNameWithoutNamespace()
114
    {
115 111
        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 111
    public function getDefaultNamespace() : string
119
    {
120 111
        $module = $this->laravel['modules'];
121
122 111
        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 111
    private function getStubName()
130
    {
131 111
        if ($this->option('plain') === true) {
132 1
            $stub = '/controller-plain.stub';
133 111
        } elseif ($this->option('api') === true) {
134 1
            $stub = '/controller-api.stub';
135
        } else {
136 111
            $stub = '/controller.stub';
137
        }
138
139 111
        return $stub;
140
    }
141
}
142