Complex classes like FormHelper 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 FormHelper, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Kris\LaravelFormBuilder; |
||
9 | class FormHelper |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var View |
||
14 | */ |
||
15 | protected $view; |
||
16 | |||
17 | /** |
||
18 | * @var Translator |
||
19 | */ |
||
20 | protected $translator; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $config; |
||
26 | |||
27 | /** |
||
28 | * @var FormBuilder |
||
29 | */ |
||
30 | protected $formBuilder; |
||
31 | |||
32 | /** |
||
33 | * All available field types |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $availableFieldTypes = [ |
||
38 | 'text' => 'InputType', |
||
39 | 'email' => 'InputType', |
||
40 | 'url' => 'InputType', |
||
41 | 'tel' => 'InputType', |
||
42 | 'search' => 'InputType', |
||
43 | 'password' => 'InputType', |
||
44 | 'hidden' => 'InputType', |
||
45 | 'number' => 'InputType', |
||
46 | 'date' => 'InputType', |
||
47 | 'file' => 'InputType', |
||
48 | 'image' => 'InputType', |
||
49 | 'color' => 'InputType', |
||
50 | 'datetime-local' => 'InputType', |
||
51 | 'month' => 'InputType', |
||
52 | 'range' => 'InputType', |
||
53 | 'time' => 'InputType', |
||
54 | 'week' => 'InputType', |
||
55 | 'select' => 'SelectType', |
||
56 | 'textarea' => 'TextareaType', |
||
57 | 'button' => 'ButtonType', |
||
58 | 'submit' => 'ButtonType', |
||
59 | 'reset' => 'ButtonType', |
||
60 | 'radio' => 'CheckableType', |
||
61 | 'checkbox' => 'CheckableType', |
||
62 | 'choice' => 'ChoiceType', |
||
63 | 'form' => 'ChildFormType', |
||
64 | 'entity' => 'EntityType', |
||
65 | 'collection' => 'CollectionType', |
||
66 | 'repeated' => 'RepeatedType', |
||
67 | 'static' => 'StaticType' |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * Custom types |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $customTypes = []; |
||
76 | |||
77 | /** |
||
78 | * @param View $view |
||
79 | * @param Translator $translator |
||
80 | * @param array $config |
||
81 | */ |
||
82 | 88 | public function __construct(View $view, Translator $translator, array $config = []) |
|
83 | { |
||
84 | 88 | $this->view = $view; |
|
85 | 88 | $this->translator = $translator; |
|
86 | 88 | $this->config = $config; |
|
87 | 88 | $this->loadCustomTypes(); |
|
88 | 88 | } |
|
89 | |||
90 | /** |
||
91 | * @param string $key |
||
92 | * @param string $default |
||
93 | * @return mixed |
||
94 | */ |
||
95 | 88 | public function getConfig($key, $default = null) |
|
99 | |||
100 | /** |
||
101 | * @return View |
||
102 | */ |
||
103 | 29 | public function getView() |
|
107 | |||
108 | /** |
||
109 | * Merge options array |
||
110 | * |
||
111 | * @param array $first |
||
112 | * @param array $second |
||
113 | * @return array |
||
114 | */ |
||
115 | public function mergeOptions(array $first, array $second) |
||
144 | |||
145 | /** |
||
146 | * Get proper class for field type |
||
147 | * |
||
148 | * @param $type |
||
149 | * @return string |
||
150 | */ |
||
151 | 59 | public function getFieldType($type) |
|
177 | |||
178 | /** |
||
179 | * Convert array of attributes to html attributes |
||
180 | * |
||
181 | * @param $options |
||
182 | * @return string |
||
183 | */ |
||
184 | 70 | public function prepareAttributes($options) |
|
202 | |||
203 | /** |
||
204 | * Add custom field |
||
205 | * |
||
206 | * @param $name |
||
207 | * @param $class |
||
208 | */ |
||
209 | 3 | public function addCustomField($name, $class) |
|
217 | |||
218 | /** |
||
219 | * Load custom field types from config file |
||
220 | */ |
||
221 | 88 | private function loadCustomTypes() |
|
231 | |||
232 | 5 | public function convertModelToArray($model) |
|
248 | |||
249 | /** |
||
250 | * Format the label to the proper format |
||
251 | * |
||
252 | * @param $name |
||
253 | * @return string |
||
254 | */ |
||
255 | 69 | public function formatLabel($name) |
|
271 | |||
272 | /** |
||
273 | * @param FormField[] $fields |
||
274 | * @return array |
||
275 | */ |
||
276 | 3 | public function mergeFieldsRules($fields) |
|
293 | |||
294 | /** |
||
295 | * @param string $string |
||
296 | * @return string |
||
297 | */ |
||
298 | 69 | public function transformToDotSyntax($string) |
|
302 | |||
303 | /** |
||
304 | * @return Translator |
||
305 | */ |
||
306 | 3 | public function getTranslator() |
|
310 | } |
||
311 |