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 | View Code Duplication | protected function parseObserver($observer) |
|
147 | |||
148 | protected function buildModelReplacements(array $replace) |
||
171 | |||
172 | /** |
||
173 | * Get the fully-qualified model class name. |
||
174 | * |
||
175 | * @param string $model |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | View Code Duplication | protected function parseModel($model) |
|
193 | |||
194 | View Code Duplication | protected function getViewColumns($model) |
|
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) |
|
244 | |||
245 | protected function buildFormElement(Column $column) |
||
254 | |||
255 | protected function getFormElementType($name) |
||
259 | |||
260 | protected function getTableColumns($class) |
||
280 | |||
281 | /** |
||
282 | * Get the default namespace for the class. |
||
283 | * |
||
284 | * @param string $rootNamespace |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | protected function getDefaultNamespace($rootNamespace) |
||
292 | |||
293 | protected function getComponentNamespace() |
||
304 | |||
305 | /** |
||
306 | * Get the console command options. |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | protected function getOptions() |
||
327 | } |
||
328 |
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.