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 |
||
16 | class Admin |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public static $script = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | public static $css = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | public static $js = []; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected static $initialized = false; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected static $bootstrapped = false; |
||
42 | |||
43 | /** |
||
44 | * Initialize. |
||
45 | */ |
||
46 | public static function init() |
||
54 | |||
55 | /** |
||
56 | * Bootstrap. |
||
57 | */ |
||
58 | public static function bootstrap() |
||
68 | |||
69 | /** |
||
70 | * @param $model |
||
71 | * @param Closure $callable |
||
72 | * |
||
73 | * @return Grid |
||
74 | */ |
||
75 | public function grid($model, Closure $callable) |
||
79 | |||
80 | /** |
||
81 | * @param $model |
||
82 | * @param Closure $callable |
||
83 | * |
||
84 | * @return Form |
||
85 | */ |
||
86 | public function form($model, Closure $callable) |
||
93 | |||
94 | /** |
||
95 | * Build a tree. |
||
96 | * |
||
97 | * @param $model |
||
98 | * |
||
99 | * @return Tree |
||
100 | */ |
||
101 | public function tree($model) |
||
105 | |||
106 | /** |
||
107 | * @param Closure $callable |
||
108 | * |
||
109 | * @return Content |
||
110 | */ |
||
111 | public function content(Closure $callable) |
||
120 | |||
121 | /** |
||
122 | * @param $model |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function getModel($model) |
||
138 | |||
139 | /** |
||
140 | * Get namespace of controllers. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function controllerNamespace() |
||
150 | |||
151 | /** |
||
152 | * Add css or get all css. |
||
153 | * |
||
154 | * @param null $css |
||
155 | * |
||
156 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
||
157 | */ |
||
158 | View Code Duplication | public static function css($css = null) |
|
172 | |||
173 | /** |
||
174 | * Add js or get all js. |
||
175 | * |
||
176 | * @param null $js |
||
177 | * |
||
178 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
||
179 | */ |
||
180 | View Code Duplication | public static function js($js = null) |
|
194 | |||
195 | /** |
||
196 | * @param string $script |
||
197 | * |
||
198 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void |
||
199 | */ |
||
200 | public static function script($script = '') |
||
210 | |||
211 | /** |
||
212 | * Admin url. |
||
213 | * |
||
214 | * @param $url |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public static function url($url) |
||
228 | |||
229 | /** |
||
230 | * Left sider-bar menu. |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function menu() |
||
238 | |||
239 | /** |
||
240 | * Get admin title. |
||
241 | * |
||
242 | * @return Config |
||
243 | */ |
||
244 | public function title() |
||
248 | |||
249 | /** |
||
250 | * @return mixed |
||
251 | */ |
||
252 | public function user() |
||
256 | } |
||
257 |
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.