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 NestedForm 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 NestedForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class NestedForm |
||
56 | { |
||
57 | const DEFAULT_KEY_NAME = '__LA_KEY__'; |
||
58 | |||
59 | const REMOVE_FLAG_NAME = '_remove_'; |
||
60 | |||
61 | const REMOVE_FLAG_CLASS = 'fom-removed'; |
||
62 | |||
63 | /** |
||
64 | * @var mixed |
||
65 | */ |
||
66 | protected $key; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $relationName; |
||
72 | |||
73 | /** |
||
74 | * NestedForm key. |
||
75 | * |
||
76 | * @var Model |
||
77 | */ |
||
78 | protected $model; |
||
79 | |||
80 | /** |
||
81 | * Fields in form. |
||
82 | * |
||
83 | * @var Collection |
||
84 | */ |
||
85 | protected $fields; |
||
86 | |||
87 | /** |
||
88 | * Original data for this field. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $original = []; |
||
93 | |||
94 | /** |
||
95 | * @var \Encore\Admin\Form|\Encore\Admin\Widgets\Form |
||
96 | */ |
||
97 | protected $form; |
||
98 | |||
99 | /** |
||
100 | * Create a new NestedForm instance. |
||
101 | * |
||
102 | * NestedForm constructor. |
||
103 | * |
||
104 | * @param string $relation |
||
105 | * @param Model $model |
||
106 | */ |
||
107 | public function __construct($relation, $model = null) |
||
115 | |||
116 | /** |
||
117 | * Get current model. |
||
118 | * |
||
119 | * @return Model|null |
||
120 | */ |
||
121 | public function model() |
||
125 | |||
126 | /** |
||
127 | * Get the value of the model's primary key. |
||
128 | * |
||
129 | * @return mixed|null |
||
130 | */ |
||
131 | public function getKey() |
||
147 | |||
148 | /** |
||
149 | * Set key for current form. |
||
150 | * |
||
151 | * @param mixed $key |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setKey($key) |
||
161 | |||
162 | /** |
||
163 | * Set Form. |
||
164 | * |
||
165 | * @param Form $form |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setForm(Form $form = null) |
||
175 | |||
176 | /** |
||
177 | * Set Widget/Form. |
||
178 | * |
||
179 | * @param WidgetForm $form |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function setWidgetForm(WidgetForm $form = null) |
||
189 | |||
190 | /** |
||
191 | * Get form. |
||
192 | * |
||
193 | * @return Form |
||
194 | */ |
||
195 | public function getForm() |
||
199 | |||
200 | /** |
||
201 | * Set original values for fields. |
||
202 | * |
||
203 | * @param array $data |
||
204 | * @param string $relatedKeyName |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setOriginal($data, $relatedKeyName = null) |
||
227 | |||
228 | /** |
||
229 | * Prepare for insert or update. |
||
230 | * |
||
231 | * @param array $input |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | public function prepare($input) |
||
244 | |||
245 | /** |
||
246 | * Set original data for each field. |
||
247 | * |
||
248 | * @param string $key |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | View Code Duplication | protected function setFieldOriginalValue($key) |
|
263 | |||
264 | /** |
||
265 | * Do prepare work before store and update. |
||
266 | * |
||
267 | * @param array $record |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | protected function prepareRecord($record) |
||
308 | |||
309 | /** |
||
310 | * Fetch value in input data by column name. |
||
311 | * |
||
312 | * @param array $data |
||
313 | * @param string|array $columns |
||
314 | * |
||
315 | * @return array|mixed |
||
316 | */ |
||
317 | View Code Duplication | protected function fetchColumnValue($data, $columns) |
|
335 | |||
336 | /** |
||
337 | * @param Field $field |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function pushField(Field $field) |
||
347 | |||
348 | /** |
||
349 | * Get fields of this form. |
||
350 | * |
||
351 | * @return Collection |
||
352 | */ |
||
353 | public function fields() |
||
357 | |||
358 | /** |
||
359 | * Fill data to all fields in form. |
||
360 | * |
||
361 | * @param array $data |
||
362 | * |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function fill(array $data) |
||
374 | |||
375 | /** |
||
376 | * Get the html and script of template. |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | public function getTemplateHtmlAndScript() |
||
401 | |||
402 | /** |
||
403 | * Set `errorKey` `elementName` `elementClass` for fields inside hasmany fields. |
||
404 | * |
||
405 | * @param Field $field |
||
406 | * |
||
407 | * @return Field |
||
408 | */ |
||
409 | protected function formatField(Field $field) |
||
433 | |||
434 | /** |
||
435 | * Add nested-form fields dynamically. |
||
436 | * |
||
437 | * @param string $method |
||
438 | * @param array $arguments |
||
439 | * |
||
440 | * @return mixed |
||
441 | */ |
||
442 | View Code Duplication | public function __call($method, $arguments) |
|
465 | } |
||
466 |