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 |
||
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 | {--dom= : The dom of the datatable.} |
||
22 | {--buttons= : The buttons of the datatable.} |
||
23 | {--columns= : The columns of the datatable.}'; |
||
24 | |||
25 | /** |
||
26 | * The console command description. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description = 'Create a new dataTable service class.'; |
||
31 | |||
32 | /** |
||
33 | * The type of class being generated. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $type = 'DataTable'; |
||
38 | |||
39 | public function handle() |
||
59 | |||
60 | /** |
||
61 | * Build the class with the given name. |
||
62 | * |
||
63 | * @param string $name |
||
64 | * @return string |
||
65 | */ |
||
66 | protected function buildClass($name) |
||
76 | |||
77 | /** |
||
78 | * Replace the filename. |
||
79 | * |
||
80 | * @param string $stub |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function replaceFilename(&$stub) |
||
91 | |||
92 | /** |
||
93 | * Replace the action. |
||
94 | * |
||
95 | * @param string $stub |
||
96 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
97 | */ |
||
98 | protected function replaceAction(&$stub) |
||
106 | |||
107 | /** |
||
108 | * Set the action view to be used. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | protected function getAction() |
||
116 | |||
117 | /** |
||
118 | * Replace builder name. |
||
119 | * |
||
120 | * @param string $stub |
||
121 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
122 | */ |
||
123 | View Code Duplication | protected function replaceBuilder(&$stub) |
|
132 | |||
133 | /** |
||
134 | * Parse the name and format according to the root namespace. |
||
135 | * |
||
136 | * @param string $name |
||
137 | * @return string |
||
138 | */ |
||
139 | View Code Duplication | protected function qualifyClass($name) |
|
157 | |||
158 | /** |
||
159 | * Get the default namespace for the class. |
||
160 | * |
||
161 | * @param string $rootNamespace |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function getDefaultNamespace($rootNamespace) |
||
168 | |||
169 | /** |
||
170 | * Replace model name. |
||
171 | * |
||
172 | * @param string $stub |
||
173 | * @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
||
174 | */ |
||
175 | protected function replaceModel(&$stub) |
||
183 | |||
184 | /** |
||
185 | * Get model name to use. |
||
186 | */ |
||
187 | protected function getModel() |
||
198 | |||
199 | /** |
||
200 | * Replace model import. |
||
201 | * |
||
202 | * @param string $stub |
||
203 | * @return $this |
||
204 | */ |
||
205 | protected function replaceModelImport(&$stub) |
||
213 | |||
214 | /** |
||
215 | * Get the stub file for the generator. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | View Code Duplication | protected function getStub() |
|
227 | |||
228 | /** |
||
229 | * Get the console command options. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | View Code Duplication | protected function getOptions() |
|
241 | } |
||
242 |
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.