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 |
||
10 | class DataTablesHtmlCommand extends GeneratorCommand |
||
11 | { |
||
12 | /** |
||
13 | * The name and signature of the console command. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $signature = 'datatables:html |
||
18 | {name : The name of the datatable html.} |
||
19 | {--dom= : The dom of the datatable.} |
||
20 | {--buttons= : The buttons of the datatable.} |
||
21 | {--table= : Scaffold columns from the table.} |
||
22 | {--columns= : The columns of the datatable.}'; |
||
23 | |||
24 | /** |
||
25 | * The console command description. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $description = 'Create a new dataTable html class.'; |
||
30 | |||
31 | /** |
||
32 | * The type of class being generated. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $type = 'DataTableHtml'; |
||
37 | |||
38 | /** |
||
39 | * Build the class with the given name. |
||
40 | * |
||
41 | * @param string $name |
||
42 | * @return string |
||
43 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
44 | */ |
||
45 | protected function buildClass($name) |
||
55 | |||
56 | /** |
||
57 | * Replace columns. |
||
58 | * |
||
59 | * @param string $stub |
||
60 | * @return string |
||
61 | */ |
||
62 | protected function replaceTableId(&$stub) |
||
70 | |||
71 | /** |
||
72 | * Replace dom. |
||
73 | * |
||
74 | * @param string $stub |
||
75 | * @return $this |
||
76 | */ |
||
77 | protected function replaceDOM(&$stub) |
||
87 | |||
88 | /** |
||
89 | * Replace buttons. |
||
90 | * |
||
91 | * @param string $stub |
||
92 | * @return $this |
||
93 | */ |
||
94 | protected function replaceButtons(&$stub) |
||
102 | |||
103 | /** |
||
104 | * Get the columns to be used. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function getButtons() |
||
121 | |||
122 | /** |
||
123 | * Parse array from definition. |
||
124 | * |
||
125 | * @param string $definition |
||
126 | * @param int $indentation |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function parseButtons($definition, $indentation = 24) |
||
150 | |||
151 | /** |
||
152 | * Replace columns. |
||
153 | * |
||
154 | * @param string $stub |
||
155 | * @return $this |
||
156 | */ |
||
157 | protected function replaceColumns(&$stub) |
||
165 | |||
166 | /** |
||
167 | * Get the columns to be used. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function getColumns() |
||
188 | |||
189 | /** |
||
190 | * Parse array from definition. |
||
191 | * |
||
192 | * @param string $definition |
||
193 | * @param int $indentation |
||
194 | * @return string |
||
195 | */ |
||
196 | protected function parseColumns($definition, $indentation = 12) |
||
210 | |||
211 | /** |
||
212 | * Replace builder name. |
||
213 | * |
||
214 | * @param string $stub |
||
215 | * @return self |
||
216 | */ |
||
217 | View Code Duplication | protected function replaceBuilder(&$stub) |
|
226 | |||
227 | /** |
||
228 | * Parse the name and format according to the root namespace. |
||
229 | * |
||
230 | * @param string $name |
||
231 | * @return string |
||
232 | */ |
||
233 | View Code Duplication | protected function qualifyClass($name) |
|
251 | |||
252 | /** |
||
253 | * Get the default namespace for the class. |
||
254 | * |
||
255 | * @param string $rootNamespace |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function getDefaultNamespace($rootNamespace) |
||
262 | |||
263 | /** |
||
264 | * Get the stub file for the generator. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | View Code Duplication | protected function getStub() |
|
276 | |||
277 | /** |
||
278 | * Get the console command options. |
||
279 | * |
||
280 | * @return array |
||
281 | */ |
||
282 | View Code Duplication | protected function getOptions() |
|
290 | } |
||
291 |
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.