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 DataTablesMakeCommand 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 DataTablesMakeCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class DataTablesMakeCommand extends GeneratorCommand |
||
10 | { |
||
11 | /** |
||
12 | * The name and signature of the console command. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $signature = 'datatables:make |
||
17 | {name : The name of the DataTable.} |
||
18 | {--model= : The name of the model to be used.} |
||
19 | {--model-namespace= : The namespace of the model to be used.} |
||
20 | {--action= : The path of the action view.} |
||
21 | {--table= : Scaffold columns from the table.} |
||
22 | {--builder : Extract html() to a Builder class.} |
||
23 | {--dom= : The dom of the DataTable.} |
||
24 | {--buttons= : The buttons of the DataTable.} |
||
25 | {--columns= : The columns of the DataTable.}'; |
||
26 | |||
27 | /** |
||
28 | * The console command description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Create a new DataTable service class.'; |
||
33 | |||
34 | /** |
||
35 | * The type of class being generated. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $type = 'DataTable'; |
||
40 | |||
41 | public function handle() |
||
42 | { |
||
43 | parent::handle(); |
||
44 | |||
45 | if ($this->option('builder')) { |
||
46 | $this->call('datatables:html', [ |
||
47 | 'name' => $this->getNameInput(), |
||
48 | '--columns' => $this->option('columns') ?: $this->laravel['config']->get( |
||
49 | 'datatables-buttons.generator.columns', |
||
50 | 'id,add your columns,created_at,updated_at' |
||
51 | ), |
||
52 | '--buttons' => $this->option('buttons') ?: $this->laravel['config']->get( |
||
53 | 'datatables-buttons.generator.buttons', |
||
54 | 'create,export,print,reset,reload' |
||
55 | ), |
||
56 | '--dom' => $this->option('dom') ?: $this->laravel['config']->get( |
||
57 | 'datatables-buttons.generator.dom', |
||
58 | 'Bfrtip' |
||
59 | ), |
||
60 | '--table' => $this->option('table'), |
||
61 | ]); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Build the class with the given name. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * @return string |
||
70 | */ |
||
71 | protected function buildClass($name) |
||
87 | |||
88 | /** |
||
89 | * Replace the filename. |
||
90 | * |
||
91 | * @param string $stub |
||
92 | * @return $this |
||
93 | */ |
||
94 | protected function replaceFilename(&$stub) |
||
102 | |||
103 | /** |
||
104 | * Replace the action. |
||
105 | * |
||
106 | * @param string $stub |
||
107 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
108 | */ |
||
109 | protected function replaceAction(&$stub) |
||
117 | |||
118 | /** |
||
119 | * Set the action view to be used. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | protected function getAction() |
||
127 | |||
128 | /** |
||
129 | * Replace columns. |
||
130 | * |
||
131 | * @param string $stub |
||
132 | * @return $this |
||
133 | */ |
||
134 | protected function replaceTableId(&$stub) |
||
142 | |||
143 | /** |
||
144 | * Replace dom. |
||
145 | * |
||
146 | * @param string $stub |
||
147 | * @return $this |
||
148 | */ |
||
149 | protected function replaceDOM(&$stub) |
||
159 | |||
160 | /** |
||
161 | * Replace buttons. |
||
162 | * |
||
163 | * @param string $stub |
||
164 | * @return $this |
||
165 | */ |
||
166 | protected function replaceButtons(&$stub) |
||
174 | |||
175 | /** |
||
176 | * Get the columns to be used. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function getButtons() |
||
193 | |||
194 | /** |
||
195 | * Parse array from definition. |
||
196 | * |
||
197 | * @param string $definition |
||
198 | * @param int $indentation |
||
199 | * @return string |
||
200 | */ |
||
201 | protected function parseButtons($definition, $indentation = 24) |
||
222 | |||
223 | /** |
||
224 | * Replace columns. |
||
225 | * |
||
226 | * @param string $stub |
||
227 | * @return $this |
||
228 | */ |
||
229 | protected function replaceColumns(&$stub) |
||
237 | |||
238 | /** |
||
239 | * Get the columns to be used. |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function getColumns() |
||
260 | |||
261 | /** |
||
262 | * Parse array from definition. |
||
263 | * |
||
264 | * @param string $definition |
||
265 | * @param int $indentation |
||
266 | * @return string |
||
267 | */ |
||
268 | protected function parseColumns($definition, $indentation = 12) |
||
282 | |||
283 | /** |
||
284 | * Replace builder name. |
||
285 | * |
||
286 | * @param string $stub |
||
287 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
288 | */ |
||
289 | protected function replaceBuilder(&$stub) |
||
298 | |||
299 | /** |
||
300 | * Parse the name and format according to the root namespace. |
||
301 | * |
||
302 | * @param string $name |
||
303 | * @return string |
||
304 | */ |
||
305 | View Code Duplication | protected function qualifyClass($name) |
|
323 | |||
324 | /** |
||
325 | * Get the default namespace for the class. |
||
326 | * |
||
327 | * @param string $rootNamespace |
||
328 | * @return string |
||
329 | */ |
||
330 | protected function getDefaultNamespace($rootNamespace) |
||
334 | |||
335 | /** |
||
336 | * Replace model name. |
||
337 | * |
||
338 | * @param string $stub |
||
339 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
340 | */ |
||
341 | protected function replaceModel(&$stub) |
||
349 | |||
350 | /** |
||
351 | * Get model name to use. |
||
352 | */ |
||
353 | protected function getModel() |
||
368 | |||
369 | /** |
||
370 | * Replace model import. |
||
371 | * |
||
372 | * @param string $stub |
||
373 | * @return $this |
||
374 | */ |
||
375 | protected function replaceModelImport(&$stub) |
||
383 | |||
384 | /** |
||
385 | * Get the stub file for the generator. |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | protected function getStub() |
||
402 | } |
||
403 |
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.