Complex classes like FormField 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 FormField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class FormField |
||
17 | { |
||
18 | /** |
||
19 | * Name of the field |
||
20 | * |
||
21 | * @var |
||
22 | */ |
||
23 | protected $name; |
||
24 | |||
25 | /** |
||
26 | * Type of the field |
||
27 | * |
||
28 | * @var |
||
29 | */ |
||
30 | protected $type; |
||
31 | |||
32 | /** |
||
33 | * All options for the field |
||
34 | * |
||
35 | * @var |
||
36 | */ |
||
37 | protected $options = []; |
||
38 | |||
39 | /** |
||
40 | * Is field rendered |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $rendered = false; |
||
45 | |||
46 | /** |
||
47 | * @var Form |
||
48 | */ |
||
49 | protected $parent; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $template; |
||
55 | |||
56 | /** |
||
57 | * @var FormHelper |
||
58 | */ |
||
59 | protected $formHelper; |
||
60 | |||
61 | /** |
||
62 | * Name of the property for value setting |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $valueProperty = 'value'; |
||
67 | |||
68 | /** |
||
69 | * Name of the property for default value |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $defaultValueProperty = 'default_value'; |
||
74 | |||
75 | /** |
||
76 | * Is default value set? |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $hasDefault = false; |
||
80 | |||
81 | /** |
||
82 | * @var \Closure|null |
||
83 | */ |
||
84 | protected $valueClosure = null; |
||
85 | |||
86 | /** |
||
87 | * @param $name |
||
88 | * @param $type |
||
89 | * @param Form $parent |
||
90 | * @param array $options |
||
91 | */ |
||
92 | 65 | public function __construct($name, $type, Form $parent, array $options = []) |
|
102 | |||
103 | 65 | protected function setupValue() |
|
118 | |||
119 | /** |
||
120 | * Get the template, can be config variable or view path |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | abstract protected function getTemplate(); |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | 26 | protected function getViewTemplate() |
|
133 | |||
134 | /** |
||
135 | * @param array $options |
||
136 | * @param bool $showLabel |
||
137 | * @param bool $showField |
||
138 | * @param bool $showError |
||
139 | * @return string |
||
140 | */ |
||
141 | 26 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
177 | |||
178 | /** |
||
179 | * Get the attribute value from the model by name |
||
180 | * |
||
181 | * @param mixed $model |
||
182 | * @param string $name |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 58 | protected function getModelValueAttribute($model, $name) |
|
196 | |||
197 | /** |
||
198 | * Transform array like syntax to dot syntax |
||
199 | * |
||
200 | * @param $key |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 65 | protected function transformKey($key) |
|
207 | |||
208 | /** |
||
209 | * Prepare options for rendering |
||
210 | * |
||
211 | * @param array $options |
||
212 | * @return array |
||
213 | */ |
||
214 | 65 | protected function prepareOptions(array $options = []) |
|
261 | |||
262 | /** |
||
263 | * Get name of the field |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | 26 | public function getName() |
|
271 | |||
272 | /** |
||
273 | * Set name of the field |
||
274 | * |
||
275 | * @param string $name |
||
276 | * @return $this |
||
277 | */ |
||
278 | 11 | public function setName($name) |
|
284 | |||
285 | /** |
||
286 | * Get dot notation key for fields |
||
287 | * |
||
288 | * @return string |
||
289 | **/ |
||
290 | 37 | public function getNameKey() |
|
294 | |||
295 | /** |
||
296 | * Get field options |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 9 | public function getOptions() |
|
304 | |||
305 | /** |
||
306 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
307 | * |
||
308 | * @param $option |
||
309 | * @param mixed $default |
||
310 | * |
||
311 | * @return mixed |
||
312 | */ |
||
313 | 65 | public function getOption($option, $default = null) |
|
317 | |||
318 | /** |
||
319 | * Set field options |
||
320 | * |
||
321 | * @param array $options |
||
322 | * @return $this |
||
323 | */ |
||
324 | 11 | public function setOptions($options) |
|
330 | |||
331 | /** |
||
332 | * Set single option on the field |
||
333 | * |
||
334 | * @param string $name |
||
335 | * @param mixed $value |
||
336 | * @return $this |
||
337 | */ |
||
338 | 65 | public function setOption($name, $value) |
|
344 | |||
345 | /** |
||
346 | * Get the type of the field |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | 35 | public function getType() |
|
354 | |||
355 | /** |
||
356 | * Set type of the field |
||
357 | * |
||
358 | * @param mixed $type |
||
359 | * @return $this |
||
360 | */ |
||
361 | 1 | public function setType($type) |
|
369 | |||
370 | /** |
||
371 | * @return Form |
||
372 | */ |
||
373 | 65 | public function getParent() |
|
377 | |||
378 | /** |
||
379 | * Check if the field is rendered |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | 4 | public function isRendered() |
|
387 | |||
388 | /** |
||
389 | * Default options for field |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | 47 | protected function getDefaults() |
|
397 | |||
398 | /** |
||
399 | * Defaults used across all fields |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | 65 | private function allDefaults() |
|
420 | |||
421 | /** |
||
422 | * Get real name of the field without form namespace |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 65 | public function getRealName() |
|
430 | |||
431 | /** |
||
432 | * @param $value |
||
433 | * @return $this |
||
434 | */ |
||
435 | 59 | public function setValue($value) |
|
455 | |||
456 | /** |
||
457 | * Set the template property on the object |
||
458 | */ |
||
459 | 65 | private function setTemplate() |
|
463 | |||
464 | /** |
||
465 | * Add error class to wrapper if validation errors exist |
||
466 | */ |
||
467 | 65 | protected function addErrorClass() |
|
481 | |||
482 | |||
483 | /** |
||
484 | * Merge all defaults with field specific defaults and set template if passed |
||
485 | * |
||
486 | * @param array $options |
||
487 | */ |
||
488 | 65 | protected function setDefaultOptions(array $options = []) |
|
493 | |||
494 | /** |
||
495 | * Check if fields needs label |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | 26 | protected function needsLabel() |
|
510 | |||
511 | /** |
||
512 | * Disable field |
||
513 | * |
||
514 | * @return $this |
||
515 | */ |
||
516 | 1 | public function disable() |
|
522 | |||
523 | /** |
||
524 | * Enable field |
||
525 | * |
||
526 | * @return $this |
||
527 | */ |
||
528 | 1 | public function enable() |
|
534 | |||
535 | /** |
||
536 | * Get validation rules for a field if any with label for attributes |
||
537 | * |
||
538 | * @return array|null |
||
539 | */ |
||
540 | 3 | public function getValidationRules() |
|
554 | |||
555 | /** |
||
556 | * Get value property |
||
557 | * |
||
558 | * @param mixed|null $default |
||
559 | * @return mixed |
||
560 | */ |
||
561 | 29 | public function getValue($default = null) |
|
565 | |||
566 | /** |
||
567 | * Get default value property |
||
568 | * |
||
569 | * @param mixed|null $default |
||
570 | * @return mixed |
||
571 | */ |
||
572 | 26 | public function getDefaultValue($default = null) |
|
576 | |||
577 | /** |
||
578 | * Check if provided value is valid for this type |
||
579 | * |
||
580 | * @return bool |
||
581 | */ |
||
582 | 62 | protected function isValidValue($value) |
|
586 | } |
||
587 |