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 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'))), |
||
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
|
|||
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 | } |
||
138 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.