Completed
Push — master ( 0d6573...bd21d3 )
by Fumio
07:02
created

ControllerMakeCommand::parseModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelPlus\Extension\Generators\Commands;
4
5
use Jumilla\Generators\Laravel\OneFileGeneratorCommand as BaseCommand;
6
use Jumilla\Generators\FileGenerator;
7
use LaravelPlus\Extension\Addons\Addon;
8
use LaravelPlus\Extension\Generators\GeneratorCommandTrait;
9
use InvalidArgumentException;
10
11
class ControllerMakeCommand extends BaseCommand
12
{
13
    use GeneratorCommandTrait;
14
15
    /**
16
     * The console command singature.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'make:controller
21
        {name : The name of the class}
22
        {--a|addon= : The name of the addon}
23
        {--m|model= : Generate a resource controller for the given model}
24
        {--r|resource : Generate a resource controller class}
25
    ';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = '[+] Create a new controller class';
33
34
    /**
35
     * The type of class being generated.
36
     *
37
     * @var string
38
     */
39
    protected $type = 'Controller';
40
41
    /**
42
     * The constructor.
43
     */
44 9
    public function __construct()
45
    {
46 9
        parent::__construct();
47
48 9
        $this->setStubDirectory(__DIR__.'/../stubs');
49 9
    }
50
51
    /**
52
     * Get the default namespace for the class.
53
     *
54
     * @return string
55
     */
56 6
    protected function getDefaultNamespace()
57
    {
58 6
        return $this->getRootNamespace().'\\'.($this->onAddon() ? 'Controllers' : 'Http\\Controllers');
59
    }
60
61
    /**
62
     * Get the stub file for the generator.
63
     *
64
     * @return string
65
     */
66 6
    protected function getStub()
67
    {
68 6
        if ($this->option('model')) {
69 2
            return 'controller-model.stub';
70
        }
71 4
        return $this->option('resource') ? 'controller-resource.stub' : 'controller-plain.stub';
72
    }
73
74
    /**
75
     * Generate file.
76
     *
77
     * @param \Jumilla\Generators\FileGenerator $generator
78
     * @param string $path
79
     * @param string $fqcn
80
     *
81
     * @return bool
82
     */
83 6
    protected function generateFile(FileGenerator $generator, $path, $fqcn)
84
    {
85 6
        list($namespace, $class) = $this->splitFullQualifyClassName($fqcn);
86 6
        list($model_namespace, $model_class) = $this->splitFullQualifyClassName($this->parseModel($this->option('model', '')));
0 ignored issues
show
Unused Code introduced by
The call to ControllerMakeCommand::option() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
It seems like $this->option('model', '') targeting Illuminate\Console\Command::option() can also be of type array; however, LaravelPlus\Extension\Ge...keCommand::parseModel() 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...
87
88 6
        return $generator->file($path)->template($this->getStub(), [
89 6
            'namespace' => $namespace,
90 6
            'root_namespace' => $this->getAppNamespace(),   // use App\Http\Controllers\Controller
91 6
            'class' => $class,
92 6
            'model_namespace' => $model_namespace ? $model_namespace.'\\' : '',
93 6
            'model_class' => $model_class,
94
        ]);
95
    }
96
97
    /**
98
     * Get the fully-qualified model class name.
99
     *
100
     * @param  string  $model
101
     * @return string
102
     */
103 6
    protected function parseModel($model)
104
    {
105 6
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
106
            throw new InvalidArgumentException('Model name contains invalid characters.');
107
        }
108
109 6
        $model = trim(str_replace('/', '\\', $model), '\\');
110
111 6
        return $model;
112
    }
113
}
114