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:
Complex classes like Embeds often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Embeds, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Embeds extends Field |
||
11 | { |
||
12 | /** |
||
13 | * @var \Closure |
||
14 | */ |
||
15 | protected $builder = null; |
||
16 | protected $view = 'admin::form.embeds'; |
||
17 | |||
18 | /** |
||
19 | * Create a new HasMany field instance. |
||
20 | * |
||
21 | * @param string $column |
||
22 | * @param array $arguments |
||
23 | */ |
||
24 | View Code Duplication | public function __construct($column, $arguments = []) |
|
37 | |||
38 | /** |
||
39 | * Prepare input data for insert or update. |
||
40 | * |
||
41 | * @param array $input |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function prepare($input) |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function getValidator(array $input) |
||
56 | { |
||
57 | if (!array_key_exists($this->column, $input)) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | $input = array_only($input, $this->column); |
||
62 | |||
63 | $rules = $attributes = $messages = []; |
||
64 | $rel = $this->column; |
||
65 | |||
66 | $availInput = $input; |
||
67 | View Code Duplication | $array_key_attach_str = function(array $a, string $b, string $c = '.') { |
|
68 | return call_user_func_array( |
||
69 | 'array_merge', |
||
70 | array_map(function($u, $v) use ($b, $c) { |
||
71 | return ["{$b}{$c}{$u}" => $v]; |
||
72 | }, array_keys($a), array_values($a)) |
||
73 | ); |
||
74 | }; |
||
75 | |||
76 | View Code Duplication | $array_key_clean = function(array $a) { |
|
77 | $a = count($a) ? call_user_func_array('array_merge', array_map(function($k, $v) { |
||
78 | return [str_replace(':', '', $k) => $v]; |
||
79 | }, array_keys($a), array_values($a))) : $a; |
||
80 | |||
81 | return $a; |
||
82 | }; |
||
83 | |||
84 | View Code Duplication | $array_key_clean_undot = function(array $a) { |
|
85 | $keys = preg_grep('/[\.\:]/', array_keys($a)); |
||
86 | if ($keys) { |
||
87 | foreach ($keys as $key) { |
||
88 | array_set($a, str_replace(':', '', $key), $a[$key]); |
||
89 | unset($a[$key]); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return $a; |
||
94 | }; |
||
95 | |||
96 | |||
97 | /** @var Field $field */ |
||
98 | foreach ($this->buildEmbeddedForm()->fields() as $field) { |
||
99 | if (!$fieldRules = $field->getRules()) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | $column = $field->column(); |
||
104 | $columns = is_array($column) ? $column : [$column]; |
||
105 | if ($field instanceof Field\MultipleSelect) { |
||
106 | $availInput[$column] = array_filter($availInput[$column], 'strlen'); |
||
107 | $availInput[$column] = $availInput[$column] ? : null; |
||
108 | } |
||
109 | /* |
||
110 | * |
||
111 | * For single column field format rules to: |
||
112 | * [ |
||
113 | * 'extra.name' => 'required' |
||
114 | * 'extra.email' => 'required' |
||
115 | * ] |
||
116 | * |
||
117 | * For multiple column field with rules like 'required': |
||
118 | * 'extra' => [ |
||
119 | * 'start' => 'start_at' |
||
120 | * 'end' => 'end_at', |
||
121 | * ] |
||
122 | * |
||
123 | * format rules to: |
||
124 | * [ |
||
125 | * 'extra.start_atstart' => 'required' |
||
126 | * 'extra.end_atend' => 'required' |
||
127 | * ] |
||
128 | */ |
||
129 | $newColumn = array_map(function($k, $v) use ($rel) { |
||
130 | //Fix ResetInput Function! A Headache Implementation! |
||
131 | return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; |
||
132 | }, array_keys($columns), array_values($columns)); |
||
133 | |||
134 | $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules; |
||
135 | $rules = array_merge($rules, call_user_func_array( |
||
136 | 'array_merge', |
||
137 | array_map(function($v) use ($fieldRules) { |
||
138 | return [$v => $fieldRules]; |
||
139 | }, $newColumn) |
||
140 | )); |
||
141 | $attributes = array_merge($attributes, call_user_func_array( |
||
142 | 'array_merge', |
||
143 | View Code Duplication | array_map(function($v) use ($field) { |
|
144 | //Fix ResetInput Function! A Headache Implementation! |
||
145 | $u = $field->label(); |
||
146 | $u .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[1])[0].']' : ''; |
||
147 | |||
148 | return [$v => "{$u}"]; |
||
149 | }, $newColumn) |
||
150 | )); |
||
151 | View Code Duplication | if ($field->validationMessages) { |
|
152 | $newMessages = array_map(function($v) use ($field, $availInput, $array_key_attach_str) { |
||
153 | list($rel, $col) = explode('.', $v); |
||
154 | //Fix ResetInput Function! A Headache Implementation! |
||
155 | $col1 = explode(':', $col)[0]; |
||
156 | if (!array_key_exists($col1, $availInput[$rel])) { |
||
157 | return [null => null]; |
||
158 | } |
||
159 | $rows = $availInput[$rel][$col1]; |
||
160 | if (!is_array($rows)) { |
||
161 | return $array_key_attach_str($field->validationMessages, $v); |
||
162 | } |
||
163 | $r = []; |
||
164 | foreach (array_keys($rows) as $k) { |
||
165 | $k = "{$v}{$k}"; |
||
166 | $r = array_merge($r, $array_key_attach_str($field->validationMessages, $k)); |
||
167 | } |
||
168 | |||
169 | return $r; |
||
170 | }, $newColumn); |
||
171 | $newMessages = call_user_func_array('array_merge', $newMessages); |
||
172 | $messages = array_merge($messages, $newMessages); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if (empty($rules)) { |
||
177 | return false; |
||
178 | } |
||
179 | $newInput = call_user_func_array('array_merge', array_map(function($idx) use ($availInput) { |
||
180 | list($rel, $col) = explode('.', $idx); |
||
181 | //Fix ResetInput Function! A Headache Implementation! |
||
182 | $col1 = explode(':', $col)[0]; |
||
183 | if (!array_key_exists($col1, $availInput[$rel])) { |
||
184 | return [null => null]; |
||
185 | } |
||
186 | if (is_array($availInput[$rel][$col1])) { |
||
187 | return call_user_func_array('array_merge', array_map(function($x, $y) use ($idx) { |
||
188 | return ["{$idx}{$x}" => $y]; |
||
189 | }, array_keys($availInput[$rel][$col1]), $availInput[$rel][$col1])); |
||
190 | } |
||
191 | |||
192 | return ["{$idx}" => $availInput[$rel][$col1]]; |
||
193 | }, array_keys($rules))); |
||
194 | |||
195 | $newInput = $array_key_clean_undot($newInput); |
||
196 | $newRules = array_map(function($idx) use ($availInput, $rules) { |
||
197 | list($rel, $col) = explode('.', $idx); |
||
198 | //Fix ResetInput Function! A Headache Implementation! |
||
199 | $col1 = explode(':', $col)[0]; |
||
200 | if (!array_key_exists($col1, $availInput[$rel])) { |
||
201 | return [null => null]; |
||
202 | } |
||
203 | if (is_array($availInput[$rel][$col1])) { |
||
204 | return call_user_func_array('array_merge', array_map(function($x) use ($idx, $rules) { |
||
205 | return ["{$idx}{$x}" => $rules[$idx]]; |
||
206 | }, array_keys($availInput[$rel][$col1]))); |
||
207 | } |
||
208 | |||
209 | return ["{$idx}" => $rules[$idx]]; |
||
210 | }, array_keys($rules)); |
||
211 | $newRules = array_filter(call_user_func_array('array_merge', $newRules), 'strlen', ARRAY_FILTER_USE_KEY); |
||
212 | $newRules = $array_key_clean($newRules); |
||
213 | |||
214 | $newAttributes = array_map(function($idx) use ($availInput, $attributes) { |
||
215 | list($rel, $col) = explode('.', $idx); |
||
216 | //Fix ResetInput Function! A Headache Implementation! |
||
217 | $col1 = explode(':', $col)[0]; |
||
218 | if (!array_key_exists($col1, $availInput[$rel])) { |
||
219 | return [null => null]; |
||
220 | } |
||
221 | View Code Duplication | if (is_array($availInput[$rel][$col1])) { |
|
222 | if (array_keys($availInput[$rel][$col1])) { |
||
223 | return call_user_func_array('array_merge', array_map(function($x) use ($idx, $attributes) { |
||
224 | return ["{$idx}.{$x}" => $attributes[$idx]]; |
||
225 | }, array_keys($availInput[$rel][$col1]))); |
||
226 | } |
||
227 | |||
228 | return [null => null]; |
||
229 | } |
||
230 | |||
231 | return ["{$idx}" => $attributes[$idx]]; |
||
232 | }, array_keys($attributes)); |
||
233 | $newAttributes = array_filter(call_user_func_array('array_merge', $newAttributes), 'strlen'); |
||
234 | $newAttributes = $array_key_clean($newAttributes); |
||
235 | |||
236 | $messages = $array_key_clean($messages); |
||
237 | |||
238 | if (empty($newInput)) { |
||
239 | $newInput = $availInput; |
||
240 | } |
||
241 | |||
242 | return Validator::make($newInput, $newRules, $messages, $newAttributes); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Format validation attributes. |
||
247 | * |
||
248 | * @param array $input |
||
249 | * @param string $label |
||
250 | * @param string $column |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | View Code Duplication | protected function formatValidationAttribute($input, $label, $column) |
|
280 | |||
281 | /** |
||
282 | * Reset input key for validation. |
||
283 | * |
||
284 | * @param array $input |
||
285 | * @param array $column $column is the column name array set |
||
286 | * |
||
287 | * @return void. |
||
288 | */ |
||
289 | public function resetInputKey(array &$input, array $column) |
||
310 | |||
311 | /** |
||
312 | * Get data for Embedded form. |
||
313 | * |
||
314 | * Normally, data is obtained from the database. |
||
315 | * |
||
316 | * When the data validation errors, data is obtained from session flash. |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | protected function getEmbeddedData() |
||
336 | |||
337 | /** |
||
338 | * Build a Embedded Form and fill data. |
||
339 | * |
||
340 | * @return EmbeddedForm |
||
341 | */ |
||
342 | protected function buildEmbeddedForm() |
||
354 | |||
355 | /** |
||
356 | * Render the form. |
||
357 | * |
||
358 | * @return \Illuminate\View\View |
||
359 | */ |
||
360 | public function render() |
||
364 | } |
||
365 |
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.