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 |
||
16 | class FormHelper |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var View |
||
21 | */ |
||
22 | protected $view; |
||
23 | |||
24 | /** |
||
25 | * @var TranslatorInterface |
||
26 | */ |
||
27 | protected $translator; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $config; |
||
33 | |||
34 | /** |
||
35 | * @var FormBuilder |
||
36 | */ |
||
37 | protected $formBuilder; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected static $reservedFieldNames = [ |
||
43 | 'save' |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * All available field types |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $availableFieldTypes = [ |
||
52 | 'text' => 'InputType', |
||
53 | 'email' => 'InputType', |
||
54 | 'url' => 'InputType', |
||
55 | 'tel' => 'InputType', |
||
56 | 'search' => 'InputType', |
||
57 | 'password' => 'InputType', |
||
58 | 'hidden' => 'InputType', |
||
59 | 'number' => 'InputType', |
||
60 | 'date' => 'InputType', |
||
61 | 'file' => 'InputType', |
||
62 | 'image' => 'InputType', |
||
63 | 'color' => 'InputType', |
||
64 | 'datetime-local' => 'InputType', |
||
65 | 'month' => 'InputType', |
||
66 | 'range' => 'InputType', |
||
67 | 'time' => 'InputType', |
||
68 | 'week' => 'InputType', |
||
69 | 'select' => 'SelectType', |
||
70 | 'textarea' => 'TextareaType', |
||
71 | 'button' => 'ButtonType', |
||
72 | 'buttongroup' => 'ButtonGroupType', |
||
73 | 'submit' => 'ButtonType', |
||
74 | 'reset' => 'ButtonType', |
||
75 | 'radio' => 'CheckableType', |
||
76 | 'checkbox' => 'CheckableType', |
||
77 | 'choice' => 'ChoiceType', |
||
78 | 'form' => 'ChildFormType', |
||
79 | 'entity' => 'EntityType', |
||
80 | 'collection' => 'CollectionType', |
||
81 | 'repeated' => 'RepeatedType', |
||
82 | 'static' => 'StaticType' |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Custom types |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private $customTypes = []; |
||
91 | |||
92 | /** |
||
93 | * @param View $view |
||
94 | * @param Translator $translator |
||
95 | * @param array $config |
||
96 | */ |
||
97 | 131 | public function __construct(View $view, Translator $translator, array $config = []) |
|
104 | |||
105 | /** |
||
106 | * @param string $key |
||
107 | * @param string $default |
||
108 | * @param array $customConfig |
||
109 | * @return mixed |
||
110 | */ |
||
111 | 131 | public function getConfig($key = null, $default = null, $customConfig = []) |
|
121 | |||
122 | /** |
||
123 | * @return View |
||
124 | */ |
||
125 | 38 | public function getView() |
|
129 | |||
130 | /** |
||
131 | * Merge options array. |
||
132 | * |
||
133 | * @param array $targetOptions |
||
134 | * @param array $sourceOptions |
||
135 | * @return array |
||
136 | */ |
||
137 | 131 | public function mergeOptions(array $targetOptions, array $sourceOptions) |
|
141 | |||
142 | |||
143 | /** |
||
144 | * Get proper class for field type. |
||
145 | * |
||
146 | * @param $type |
||
147 | * @return string |
||
148 | */ |
||
149 | 88 | public function getFieldType($type) |
|
150 | { |
||
151 | 88 | $types = array_keys(static::$availableFieldTypes); |
|
152 | |||
153 | 88 | if (!$type || trim($type) == '') { |
|
154 | 1 | throw new \InvalidArgumentException('Field type must be provided.'); |
|
155 | } |
||
156 | |||
157 | 87 | if ($this->hasCustomField($type)) { |
|
158 | 3 | return $this->customTypes[$type]; |
|
159 | } |
||
160 | |||
161 | 84 | if (!in_array($type, $types)) { |
|
162 | 2 | throw new \InvalidArgumentException( |
|
163 | sprintf( |
||
164 | 2 | 'Unsupported field type [%s]. Available types are: %s', |
|
165 | $type, |
||
166 | 2 | join(', ', array_merge($types, array_keys($this->customTypes))) |
|
167 | ) |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | 82 | $namespace = __NAMESPACE__.'\\Fields\\'; |
|
172 | |||
173 | 82 | return $namespace . static::$availableFieldTypes[$type]; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Convert array of attributes to html attributes. |
||
178 | * |
||
179 | * @param $options |
||
180 | * @return string |
||
181 | */ |
||
182 | 104 | public function prepareAttributes($options) |
|
199 | |||
200 | /** |
||
201 | * Add custom field. |
||
202 | * |
||
203 | * @param $name |
||
204 | * @param $class |
||
205 | */ |
||
206 | 4 | public function addCustomField($name, $class) |
|
214 | |||
215 | /** |
||
216 | * Load custom field types from config file. |
||
217 | */ |
||
218 | 131 | private function loadCustomTypes() |
|
228 | |||
229 | /** |
||
230 | * Check if custom field with provided name exists |
||
231 | * @param string $name |
||
232 | * @return boolean |
||
233 | */ |
||
234 | 88 | public function hasCustomField($name) |
|
238 | |||
239 | /** |
||
240 | * @param object $model |
||
241 | * @return object|null |
||
242 | */ |
||
243 | 4 | public function convertModelToArray($model) |
|
259 | |||
260 | /** |
||
261 | * Format the label to the proper format. |
||
262 | * |
||
263 | * @param $name |
||
264 | * @return string |
||
265 | */ |
||
266 | 102 | public function formatLabel($name) |
|
282 | |||
283 | /** |
||
284 | * @param FormField $field |
||
285 | * @return RulesParser |
||
286 | */ |
||
287 | 103 | public function createRulesParser(FormField $field) |
|
291 | |||
292 | /** |
||
293 | * @param FormField $field |
||
294 | * @return array |
||
295 | */ |
||
296 | 10 | public function getFieldValidationRules(FormField $field) |
|
309 | |||
310 | /** |
||
311 | * @param FormField[] $fields |
||
312 | * @return array |
||
313 | */ |
||
314 | 10 | public function mergeFieldsRules($fields) |
|
324 | |||
325 | /** |
||
326 | * @param array $fields |
||
327 | * @return array |
||
328 | */ |
||
329 | 3 | public function mergeAttributes(array $fields) |
|
338 | |||
339 | /** |
||
340 | * Alter a form's values recursively according to its fields. |
||
341 | * |
||
342 | * @param Form $form |
||
343 | * @param array $values |
||
344 | * @return void |
||
345 | */ |
||
346 | 3 | public function alterFieldValues(Form $form, array &$values) |
|
362 | |||
363 | /** |
||
364 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | 10 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
|
386 | |||
387 | /** |
||
388 | * Add unprefixed messages with prefix to a MessageBag. |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | 1 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
404 | |||
405 | /** |
||
406 | * @param string $string |
||
407 | * @return string |
||
408 | */ |
||
409 | 103 | public function transformToDotSyntax($string) |
|
413 | |||
414 | /** |
||
415 | * @param string $string |
||
416 | * @return string |
||
417 | */ |
||
418 | 7 | public function transformToBracketSyntax($string) |
|
428 | |||
429 | /** |
||
430 | * @return TranslatorInterface |
||
431 | */ |
||
432 | 3 | public function getTranslator() |
|
436 | |||
437 | /** |
||
438 | * Check if field name is valid and not reserved. |
||
439 | * |
||
440 | * @throws \InvalidArgumentException |
||
441 | * @param string $name |
||
442 | * @param string $className |
||
443 | */ |
||
444 | 71 | public function checkFieldName($name, $className) |
|
461 | } |
||
462 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..