Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class ComponentMakeCommand extends GeneratorCommand |
||
13 | { |
||
14 | /** |
||
15 | * The console command name. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name = 'make:component'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Create a new component class(ScoAdmin)'; |
||
27 | |||
28 | /** |
||
29 | * The type of class being generated. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $type = 'Component'; |
||
34 | |||
35 | protected $columnTypeMappings = [ |
||
36 | 'smallint' => 'text', |
||
37 | 'integer' => 'text', |
||
38 | 'bigint' => 'text', |
||
39 | 'float' => 'text', |
||
40 | 'string' => 'text', |
||
41 | 'text' => 'text', |
||
42 | 'boolean' => 'mapping', |
||
43 | 'datetime' => 'datetime', |
||
44 | 'date' => 'datetime', |
||
45 | ]; |
||
46 | |||
47 | protected $elementTypeMappings = [ |
||
48 | 'smallint' => 'number', |
||
49 | 'integer' => 'number', |
||
50 | 'bigint' => 'number', |
||
51 | 'float' => 'number', |
||
52 | 'string' => 'text', |
||
53 | 'text' => 'textarea', |
||
54 | 'boolean' => 'elswitch', |
||
55 | 'datetime' => 'datetime', |
||
56 | 'date' => 'date', |
||
57 | 'time' => 'time', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Get the stub file for the generator. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | protected function getStub() |
||
73 | |||
74 | /** |
||
75 | * Build the class with the given name. |
||
76 | * |
||
77 | * @param string $name |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function buildClass($name) |
||
95 | |||
96 | protected function buildObserverReplacements() |
||
120 | |||
121 | /** |
||
122 | * Get the fully-qualified observer class name. |
||
123 | * |
||
124 | * @param string $observer |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | protected function parseObserver($observer) |
||
129 | { |
||
130 | if (preg_match('([^A-Za-z0-9_/\\\\])', $observer)) { |
||
131 | throw new InvalidArgumentException('Observer name contains invalid characters.'); |
||
132 | } |
||
133 | |||
134 | $observer = trim(str_replace('/', '\\', $observer), '\\'); |
||
135 | |||
136 | if (! Str::startsWith( |
||
137 | $observer, |
||
138 | $rootNamespace = $this->laravel->getNamespace() |
||
139 | )) { |
||
140 | $observer = $rootNamespace . $this->getComponentNamespace() |
||
141 | . '\Observers\\' |
||
142 | . $observer . 'Observer'; |
||
143 | } |
||
144 | |||
145 | return $observer; |
||
146 | } |
||
147 | |||
148 | protected function buildModelReplacements(array $replace) |
||
149 | { |
||
150 | $modelClass = $this->parseModel($this->option('model')); |
||
151 | |||
152 | if (! class_exists($modelClass)) { |
||
153 | if ($this->confirm( |
||
154 | "A {$modelClass} model does not exist. Do you want to generate it?", |
||
155 | true |
||
156 | )) { |
||
157 | $this->call('make:model', ['name' => $modelClass]); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $columns = $this->getViewColumns($modelClass); |
||
162 | $elements = $this->getFormElements($modelClass); |
||
163 | |||
164 | return array_merge($replace, [ |
||
165 | 'DummyFullModelClass' => $modelClass, |
||
166 | 'DummyColumns' => $columns ? implode("\n", $columns) : '', |
||
167 | 'DummyElements' => $elements ? implode("\n", $elements) : '', |
||
168 | ]); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get the fully-qualified model class name. |
||
173 | * |
||
174 | * @param string $model |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function parseModel($model) |
||
179 | { |
||
180 | if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
||
181 | throw new InvalidArgumentException('Model name contains invalid characters.'); |
||
182 | } |
||
183 | |||
184 | $model = trim(str_replace('/', '\\', $model), '\\'); |
||
185 | |||
186 | if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
||
187 | $model = $rootNamespace . $model; |
||
188 | } |
||
189 | |||
190 | return $model; |
||
191 | } |
||
192 | |||
193 | View Code Duplication | protected function getViewColumns($model) |
|
194 | { |
||
195 | $columns = $this->getTableColumns($model); |
||
196 | if (! $columns) { |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | $list = []; |
||
201 | foreach ($columns as $column) { |
||
202 | $list[] = $this->buildViewColumn($column); |
||
203 | } |
||
204 | |||
205 | return $list; |
||
206 | } |
||
207 | |||
208 | protected function buildViewColumn(Column $column) |
||
217 | |||
218 | protected function getColumnTitle(Column $column) |
||
222 | |||
223 | protected function getViewColumnType($name) |
||
227 | |||
228 | View Code Duplication | protected function getFormElements($model) |
|
229 | { |
||
230 | $columns = $this->getTableColumns($model); |
||
231 | |||
245 | |||
246 | protected function buildFormElement(Column $column) |
||
255 | |||
256 | protected function getFormElementType($name) |
||
260 | |||
261 | protected function getTableColumns($class) |
||
281 | |||
282 | /** |
||
283 | * Get the default namespace for the class. |
||
284 | * |
||
285 | * @param string $rootNamespace |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | protected function getDefaultNamespace($rootNamespace) |
||
293 | |||
294 | /** |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | protected function getComponentNamespace() |
||
309 | |||
310 | /** |
||
311 | * Get the console command options. |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | protected function getOptions() |
||
344 | } |
||
345 |
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.