Completed
Push — master ( 1d3c0c...8bf0fe )
by Song
03:06
created

ActionCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceClass() 0 18 1
A generateInteractor() 0 26 3
A getStub() 0 12 3
A getDefaultNamespace() 0 14 2
A getNameInput() 0 8 1
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
8
class ActionCommand extends GeneratorCommand
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'admin:action {name}
16
        {--grid-batch}
17
        {--grid-row}
18
        {--form}
19
        {--dialog}
20
        {--name=}
21
        {--namespace=}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Make a admin action';
29
30
    /**
31
     * Replace the class name for the given stub.
32
     *
33
     * @param string $stub
34
     * @param string $name
35
     *
36
     * @return string
37
     */
38
    protected function replaceClass($stub, $name)
39
    {
40
        $stub = parent::replaceClass($stub, $name);
41
42
        return str_replace(
43
            [
44
                'DummyName',
45
                'DummySelector',
46
                'DummyInteractor'
47
            ],
48
            [
49
                $this->option('name'),
50
                Str::kebab(class_basename($this->argument('name'))),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array or null; 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...
51
                $this->generateInteractor(),
52
            ],
53
            $stub
54
        );
55
    }
56
57
    protected function generateInteractor()
58
    {
59
        if ($this->option('form')) {
60
            return <<<CODE
61
62
    public function form()
63
    {
64
        \$this->text('name')->rules('required');
65
        \$this->email('email')->rules('email');
66
        \$this->datetime('created_at');
67
    }
68
69
CODE;
70
        } elseif ($this->option('dialog')) {
71
            return <<<CODE
72
73
    public function dialog()
74
    {
75
        \$this->confirm('Confirm message...');
76
    }
77
78
CODE;
79
        }
80
81
        return '';
82
    }
83
84
    /**
85
     * Get the stub file for the generator.
86
     *
87
     * @return string
88
     */
89
    public function getStub()
90
    {
91
        if ($this->option('grid-batch')) {
92
            return __DIR__.'/stubs/grid-batch-action.stub';
93
        }
94
95
        if ($this->option('grid-row')) {
96
            return __DIR__.'/stubs/grid-row-action.stub';
97
        }
98
99
        return __DIR__.'/stubs/action.stub';
100
    }
101
102
    /**
103
     * Get the default namespace for the class.
104
     *
105
     * @param string $rootNamespace
106
     *
107
     * @return string
108
     */
109
    protected function getDefaultNamespace($rootNamespace)
110
    {
111
        if ($namespace = $this->option('namespace')) {
112
            return $namespace;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $namespace; (array|string|boolean) is incompatible with the return type of the parent method Illuminate\Console\Gener...nd::getDefaultNamespace of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
113
        }
114
115
        $segments = explode('\\', config('admin.route.namespace'));
116
117
        array_pop($segments);
118
119
        array_push($segments, 'Actions');
120
121
        return implode('\\', $segments);
122
    }
123
124
    /**
125
     * Get the desired class name from the input.
126
     *
127
     * @return string
128
     */
129
    protected function getNameInput()
130
    {
131
        $name = trim($this->argument('name'));
132
133
        $this->type = $this->qualifyClass($name);
134
135
        return $name;
136
    }
137
}