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 |
||
17 | class FormHelper |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var View |
||
22 | */ |
||
23 | protected $view; |
||
24 | |||
25 | /** |
||
26 | * @var TranslatorInterface |
||
27 | */ |
||
28 | protected $translator; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $config; |
||
34 | |||
35 | /** |
||
36 | * @var FormBuilder |
||
37 | */ |
||
38 | protected $formBuilder; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected static $reservedFieldNames = [ |
||
44 | 'save' |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * All available field types |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected static $availableFieldTypes = [ |
||
53 | 'text' => 'InputType', |
||
54 | 'email' => 'InputType', |
||
55 | 'url' => 'InputType', |
||
56 | 'tel' => 'InputType', |
||
57 | 'search' => 'InputType', |
||
58 | 'password' => 'InputType', |
||
59 | 'hidden' => 'InputType', |
||
60 | 'number' => 'InputType', |
||
61 | 'date' => 'InputType', |
||
62 | 'file' => 'InputType', |
||
63 | 'image' => 'InputType', |
||
64 | 'color' => 'InputType', |
||
65 | 'datetime-local' => 'InputType', |
||
66 | 'month' => 'InputType', |
||
67 | 'range' => 'InputType', |
||
68 | 'time' => 'InputType', |
||
69 | 'week' => 'InputType', |
||
70 | 'select' => 'SelectType', |
||
71 | 'textarea' => 'TextareaType', |
||
72 | 'button' => 'ButtonType', |
||
73 | 'buttongroup' => 'ButtonGroupType', |
||
74 | 'submit' => 'ButtonType', |
||
75 | 'reset' => 'ButtonType', |
||
76 | 'radio' => 'CheckableType', |
||
77 | 'checkbox' => 'CheckableType', |
||
78 | 'choice' => 'ChoiceType', |
||
79 | 'form' => 'ChildFormType', |
||
80 | 'entity' => 'EntityType', |
||
81 | 'collection' => 'CollectionType', |
||
82 | 'repeated' => 'RepeatedType', |
||
83 | 'static' => 'StaticType' |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * Custom types |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | private $customTypes = []; |
||
92 | |||
93 | /** |
||
94 | * @param View $view |
||
95 | * @param Translator $translator |
||
96 | * @param array $config |
||
97 | 134 | */ |
|
98 | public function __construct(View $view, Translator $translator, array $config = []) |
||
105 | |||
106 | /** |
||
107 | * @param string $key |
||
108 | * @param string $default |
||
109 | * @param array $customConfig |
||
110 | * @return mixed |
||
111 | 134 | */ |
|
112 | public function getConfig($key = null, $default = null, $customConfig = []) |
||
122 | |||
123 | /** |
||
124 | * @return View |
||
125 | 38 | */ |
|
126 | public function getView() |
||
130 | |||
131 | /** |
||
132 | * Merge options array. |
||
133 | * |
||
134 | * @param array $targetOptions |
||
135 | * @param array $sourceOptions |
||
136 | * @return array |
||
137 | 134 | */ |
|
138 | public function mergeOptions(array $targetOptions, array $sourceOptions) |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Get proper class for field type. |
||
146 | * |
||
147 | * @param $type |
||
148 | * @return string |
||
149 | 91 | */ |
|
150 | public function getFieldType($type) |
||
176 | |||
177 | /** |
||
178 | * Convert array of attributes to html attributes. |
||
179 | * |
||
180 | * @param $options |
||
181 | * @return string |
||
182 | 107 | */ |
|
183 | public function prepareAttributes($options) |
||
200 | |||
201 | /** |
||
202 | * Add custom field. |
||
203 | * |
||
204 | * @param $name |
||
205 | * @param $class |
||
206 | 4 | */ |
|
207 | public function addCustomField($name, $class) |
||
215 | |||
216 | /** |
||
217 | * Load custom field types from config file. |
||
218 | 134 | */ |
|
219 | private function loadCustomTypes() |
||
229 | |||
230 | /** |
||
231 | * Check if custom field with provided name exists |
||
232 | * @param string $name |
||
233 | * @return boolean |
||
234 | 91 | */ |
|
235 | public function hasCustomField($name) |
||
239 | |||
240 | /** |
||
241 | * @param object $model |
||
242 | * @return object|null |
||
243 | 5 | */ |
|
244 | public function convertModelToArray($model) |
||
260 | |||
261 | /** |
||
262 | * Format the label to the proper format. |
||
263 | * |
||
264 | * @param $name |
||
265 | * @return string |
||
266 | 105 | */ |
|
267 | public function formatLabel($name) |
||
283 | |||
284 | /** |
||
285 | * @param FormField $field |
||
286 | * @return RulesParser |
||
287 | 106 | */ |
|
288 | public function createRulesParser(FormField $field) |
||
292 | |||
293 | /** |
||
294 | * @param FormField $field |
||
295 | * @return array |
||
296 | 10 | */ |
|
297 | public function getFieldValidationRules(FormField $field) |
||
310 | |||
311 | /** |
||
312 | * @param FormField[] $fields |
||
313 | * @return array |
||
314 | 10 | */ |
|
315 | public function mergeFieldsRules($fields) |
||
325 | |||
326 | /** |
||
327 | * @param array $fields |
||
328 | * @return array |
||
329 | 3 | */ |
|
330 | public function mergeAttributes(array $fields) |
||
339 | |||
340 | /** |
||
341 | * Get a form's checkbox fields' names. |
||
342 | * |
||
343 | * @param Form $form |
||
344 | * @return array |
||
345 | */ |
||
346 | 3 | public function getBoolableFields(Form $form) |
|
357 | |||
358 | /** |
||
359 | * Turn checkbox fields into bools. |
||
360 | 3 | * |
|
361 | 3 | * @param Form $form |
|
362 | * @param array $values |
||
363 | * @return void |
||
364 | */ |
||
365 | public function alterFieldValuesBools(Form $form, array &$values) |
||
376 | 1 | ||
377 | /** |
||
378 | * Alter a form's values recursively according to its fields. |
||
379 | * |
||
380 | 10 | * @param Form $form |
|
381 | 10 | * @param array $values |
|
382 | 10 | * @return void |
|
383 | */ |
||
384 | public function alterFieldValues(Form $form, array &$values) |
||
402 | |||
403 | 1 | /** |
|
404 | * Alter a form's validity recursively, and add messages with nested form prefix. |
||
405 | * |
||
406 | * @return void |
||
407 | */ |
||
408 | public function alterValid(Form $form, Form $mainForm, &$isValid) |
||
426 | 7 | ||
427 | /** |
||
428 | * Add unprefixed messages with prefix to a MessageBag. |
||
429 | * |
||
430 | * @return void |
||
431 | */ |
||
432 | 3 | public function appendMessagesWithPrefix(MessageBag $messageBag, $prefix, array $keyedMessages) |
|
444 | 74 | ||
445 | /** |
||
446 | 74 | * @param string $string |
|
447 | 2 | * @return string |
|
448 | 2 | */ |
|
449 | public function transformToDotSyntax($string) |
||
453 | 2 | ||
454 | 2 | /** |
|
455 | 2 | * @param string $string |
|
456 | * @return string |
||
457 | */ |
||
458 | public function transformToBracketSyntax($string) |
||
468 | |||
469 | /** |
||
470 | * @return TranslatorInterface |
||
471 | */ |
||
472 | public function getTranslator() |
||
476 | |||
477 | /** |
||
478 | * Check if field name is valid and not reserved. |
||
479 | * |
||
480 | * @throws \InvalidArgumentException |
||
481 | * @param string $name |
||
482 | * @param string $className |
||
483 | */ |
||
484 | public function checkFieldName($name, $className) |
||
501 | } |
||
502 |
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..