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 Embeds extends Field |
||
11 | { |
||
12 | /** |
||
13 | * @var \Closure |
||
14 | */ |
||
15 | protected $builder = null; |
||
16 | |||
17 | /** |
||
18 | * Create a new HasMany field instance. |
||
19 | * |
||
20 | * @param string $column |
||
21 | * @param array $arguments |
||
22 | */ |
||
23 | View Code Duplication | public function __construct($column, $arguments = []) |
|
36 | |||
37 | /** |
||
38 | * Prepare input data for insert or update. |
||
39 | * |
||
40 | * @param array $input |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | public function prepare($input) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function getValidator(array $input) |
||
133 | |||
134 | /** |
||
135 | * Format validation attributes. |
||
136 | * |
||
137 | * @param array $input |
||
138 | * @param string $label |
||
139 | * @param string $column |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | View Code Duplication | protected function formatValidationAttribute($input, $label, $column) |
|
169 | |||
170 | /** |
||
171 | * Reset input key for validation. |
||
172 | * |
||
173 | * @param array $input |
||
174 | * @param array $column $column is the column name array set |
||
175 | * |
||
176 | * @return void. |
||
177 | */ |
||
178 | public function resetInputKey(array &$input, array $column) |
||
199 | |||
200 | /** |
||
201 | * Get data for Embedded form. |
||
202 | * |
||
203 | * Normally, data is obtained from the database. |
||
204 | * |
||
205 | * When the data validation errors, data is obtained from session flash. |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function getEmbeddedData() |
||
225 | |||
226 | /** |
||
227 | * Build a Embedded Form and fill data. |
||
228 | * |
||
229 | * @return EmbeddedForm |
||
230 | */ |
||
231 | protected function buildEmbeddedForm() |
||
243 | |||
244 | /** |
||
245 | * Determine the column name to use with the embedded form |
||
246 | * |
||
247 | * @return array|string |
||
248 | */ |
||
249 | protected function getEmbeddedColumnName() |
||
257 | |||
258 | /** |
||
259 | * Check if the field is in a nested form |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | protected function isNested() |
||
267 | |||
268 | /** |
||
269 | * Render the form. |
||
270 | * |
||
271 | * @return \Illuminate\View\View |
||
272 | */ |
||
273 | public function render() |
||
277 | } |
||
278 |
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.