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:
Complex classes like ComponentMakeCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ComponentMakeCommand, and based on these observations, apply Extract Interface, too.
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(Sco-Admin)'; |
||
27 | |||
28 | /** |
||
29 | * The type of class being generated. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $type = 'Component'; |
||
34 | |||
35 | protected $displayTypes = ['table', 'image', 'tree']; |
||
36 | |||
37 | protected $columnTypeMappings = [ |
||
38 | 'smallint' => 'text', |
||
39 | 'integer' => 'text', |
||
40 | 'bigint' => 'text', |
||
41 | 'float' => 'text', |
||
42 | 'string' => 'text', |
||
43 | 'text' => 'text', |
||
44 | 'boolean' => 'mapping', |
||
45 | 'datetime' => 'datetime', |
||
46 | 'date' => 'datetime', |
||
47 | ]; |
||
48 | |||
49 | protected $elementTypeMappings = [ |
||
50 | 'smallint' => 'number', |
||
51 | 'integer' => 'number', |
||
52 | 'bigint' => 'number', |
||
53 | 'float' => 'number', |
||
54 | 'string' => 'text', |
||
55 | 'text' => 'textarea', |
||
56 | 'boolean' => 'elswitch', |
||
57 | 'datetime' => 'datetime', |
||
58 | 'date' => 'date', |
||
59 | 'time' => 'time', |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * Get the stub file for the generator. |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | protected function getStub() |
||
71 | |||
72 | /** |
||
73 | * Build the class with the given name. |
||
74 | * |
||
75 | * @param string $name |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | protected function buildClass($name) |
||
93 | |||
94 | protected function getDisplayType() |
||
107 | |||
108 | protected function buildObserverReplacements() |
||
137 | |||
138 | /** |
||
139 | * Get the fully-qualified observer class name. |
||
140 | * |
||
141 | * @param string $observer |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | protected function parseObserver($observer) |
||
164 | |||
165 | protected function buildModelReplacements() |
||
197 | |||
198 | /** |
||
199 | * Get the fully-qualified model class name. |
||
200 | * |
||
201 | * @param string $model |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | protected function parseModel($model) |
||
222 | |||
223 | View Code Duplication | protected function getViewColumns($model) |
|
237 | |||
238 | protected function buildViewColumn(Column $column) |
||
247 | |||
248 | protected function getColumnTitle(Column $column) |
||
252 | |||
253 | protected function getViewColumnType($name) |
||
257 | |||
258 | View Code Duplication | protected function getFormElements($model) |
|
275 | |||
276 | protected function buildFormElement(Column $column) |
||
285 | |||
286 | protected function getFormElementType($name) |
||
290 | |||
291 | protected function getTableColumns($class) |
||
311 | |||
312 | /** |
||
313 | * Get the default namespace for the class. |
||
314 | * |
||
315 | * @param string $rootNamespace |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | protected function getDefaultNamespace($rootNamespace) |
||
323 | |||
324 | /** |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | protected function getComponentNamespace() |
||
339 | |||
340 | /** |
||
341 | * Get the console command options. |
||
342 | * |
||
343 | * @return array |
||
344 | */ |
||
345 | protected function getOptions() |
||
369 | } |
||
370 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.