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 |
||
11 | abstract class FormElement implements FormElementInterface |
||
12 | { |
||
13 | use Assets, VisibleCondition; |
||
14 | |||
15 | /** |
||
16 | * @var \SleepingOwl\Admin\Contracts\TemplateInterface |
||
17 | */ |
||
18 | protected $template; |
||
19 | |||
20 | /** |
||
21 | * @var string|\Illuminate\View\View |
||
22 | */ |
||
23 | protected $view; |
||
24 | |||
25 | /** |
||
26 | * @var Model |
||
27 | */ |
||
28 | protected $model; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $validationRules = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $validationMessages = []; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $readonly = false; |
||
44 | |||
45 | public function __construct() |
||
49 | |||
50 | public function initialize() |
||
54 | |||
55 | /** |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getValidationMessages() |
||
62 | |||
63 | /** |
||
64 | * @param string $rule |
||
65 | * @param string $message |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function addValidationMessage($rule, $message) |
||
79 | |||
80 | /** |
||
81 | * @param array $validationMessages |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function setValidationMessages(array $validationMessages) |
||
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getValidationLabels() |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getValidationRules() |
||
107 | |||
108 | /** |
||
109 | * @param string $rule |
||
110 | * @param string|null $message |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function addValidationRule($rule, $message = null) |
||
124 | |||
125 | /** |
||
126 | * @param array|string $validationRules |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setValidationRules($validationRules) |
||
147 | |||
148 | /** |
||
149 | * @return string|\Illuminate\View\View |
||
150 | */ |
||
151 | View Code Duplication | public function getView() |
|
160 | |||
161 | /** |
||
162 | * @param \Illuminate\View\View|string $view |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setView($view) |
||
172 | |||
173 | /** |
||
174 | * @return Model |
||
175 | */ |
||
176 | public function getModel() |
||
180 | |||
181 | /** |
||
182 | * @param Model $model |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setModel(Model $model) |
||
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function isReadonly() |
||
204 | |||
205 | /** |
||
206 | * @param Closure|bool $readonly |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setReadonly($readonly) |
||
216 | |||
217 | /** |
||
218 | * @return mixed |
||
219 | */ |
||
220 | public function getValue() |
||
223 | |||
224 | public function save() |
||
227 | |||
228 | public function afterSave() |
||
231 | |||
232 | /** |
||
233 | * @return array |
||
234 | */ |
||
235 | public function toArray() |
||
243 | |||
244 | /** |
||
245 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
246 | */ |
||
247 | public function render() |
||
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | public function __toString() |
||
259 | } |
||
260 |
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.